#include "RecordValue.h" #include "RecordType.h" #include "OptionalType.h" #include "Component.h" #include "Constants.h" #include namespace Databoard { namespace Value { RecordValue::RecordValue(Databoard::Type::DataType* dataBoard) : Value(dataBoard), fieldCount(0) { Databoard::Type::RecordType* recordType = dynamic_cast(dataBoard); if(recordType != NULL) { fieldCount = recordType->count(); if(fieldCount > 0) { fields = new Value*[fieldCount]; for(int i = 0; i < fieldCount; ++i) { fields[i] = NULL; } } } } RecordValue::~RecordValue() { for(int i = 0; i < fieldCount; ++i) { delete fields[i]; } delete[] fields; } void RecordValue::setField(int fieldIndex, Value* value) { if(fieldIndex >= 0 && fieldIndex < fieldCount) { fields[fieldIndex] = value; } } int RecordValue::count() { return fieldCount; } Value* RecordValue::getField(int fieldIndex) { if(fieldIndex >= 0 && fieldIndex < fieldCount) { return fields[fieldIndex]; } else { return NULL; } } std::string RecordValue::writeOut(int indent) { Databoard::Type::RecordType* recordType = dynamic_cast(dataBoard); bool isTuple = true; for(int i = 0; i < (int)recordType->count(); ++i) { std::istringstream oss(recordType->getComponent(i)->getName()); int name; oss >> name; if(name != i) { isTuple = false; break; } } std::string s; if(isTuple == false) { s += "{\n"; } else { s += "( "; } indent += 1; for(int i = 0; i < fieldCount; ++i) { if(isTuple == false) { s.append(indent * 2, ' '); s += recordType->getComponent(i)->getName() + " = "; } if(fields[i] != NULL) { s += fields[i]->writeOut(indent); if(i != (fieldCount-1)) { s += ","; } } if(isTuple == false) { s += "\n"; } else { s += " "; } } indent -= 1; if(isTuple == false) { s.append(indent * 2, ' '); s += "}"; } else { s += ")"; } return s; } bool RecordValue::equals(const Value* other) { RecordValue* o = (RecordValue*)other; if(this->count() < o->count()) { return true; } else if(this->count() > o->count()) { return false; } for(int i = 0; i <(int)this->count(); ++i) { if(this->getField(i) < o->getField(i)) { return true; } } return true; } std::string RecordValue::isValid() { Databoard::Type::RecordType* recordType = dynamic_cast(dataBoard); if(recordType == NULL) { return "RecordValue: Type is not record."; } if(fieldCount != recordType->count()) { return "RecordValue: field count mismatch."; } for(int i = 0; i < fieldCount; ++i) { if(fields[i] == NULL) { Databoard::Type::OptionalType* optionalType = dynamic_cast(recordType->getComponent(i)->getDataBoard()); if(optionalType == NULL) { return "RecordValue: Required field is missing."; } } else { std::string fieldValidity = fields[i]->isValid(); if(fieldValidity.length() > 0) { return fieldValidity; } } } return STR_EMPTY; } } }