1 #include "RecordValue.h"
3 #include "RecordType.h"
4 #include "OptionalType.h"
14 RecordValue::RecordValue(Databoard::Type::DataType* dataBoard) : Value(dataBoard), fieldCount(0)
16 Databoard::Type::RecordType* recordType = dynamic_cast<Databoard::Type::RecordType*>(dataBoard);
18 if(recordType != NULL)
20 fieldCount = recordType->count();
24 fields = new Value*[fieldCount];
26 for(int i = 0; i < fieldCount; ++i)
34 RecordValue::~RecordValue()
36 for(int i = 0; i < fieldCount; ++i)
44 void RecordValue::setField(int fieldIndex, Value* value)
46 if(fieldIndex >= 0 && fieldIndex < fieldCount)
48 fields[fieldIndex] = value;
52 int RecordValue::count()
57 Value* RecordValue::getField(int fieldIndex)
59 if(fieldIndex >= 0 && fieldIndex < fieldCount)
61 return fields[fieldIndex];
69 std::string RecordValue::writeOut(int indent)
71 Databoard::Type::RecordType* recordType = dynamic_cast<Databoard::Type::RecordType*>(dataBoard);
75 for(int i = 0; i < (int)recordType->count(); ++i)
77 std::istringstream oss(recordType->getComponent(i)->getName());
104 for(int i = 0; i < fieldCount; ++i)
108 s.append(indent * 2, ' ');
110 s += recordType->getComponent(i)->getName() + " = ";
113 if(fields[i] != NULL)
115 s += fields[i]->writeOut(indent);
117 if(i != (fieldCount-1))
137 s.append(indent * 2, ' ');
149 bool RecordValue::equals(const Value* other)
151 RecordValue* o = (RecordValue*)other;
153 if(this->count() < o->count())
157 else if(this->count() > o->count())
162 for(int i = 0; i <(int)this->count(); ++i)
164 if(this->getField(i) < o->getField(i))
174 std::string RecordValue::isValid()
176 Databoard::Type::RecordType* recordType = dynamic_cast<Databoard::Type::RecordType*>(dataBoard);
178 if(recordType == NULL)
180 return "RecordValue: Type is not record.";
183 if(fieldCount != recordType->count())
185 return "RecordValue: field count mismatch.";
188 for(int i = 0; i < fieldCount; ++i)
190 if(fields[i] == NULL)
192 Databoard::Type::OptionalType* optionalType = dynamic_cast<Databoard::Type::OptionalType*>(recordType->getComponent(i)->getDataBoard());
194 if(optionalType == NULL)
196 return "RecordValue: Required field is missing.";
201 std::string fieldValidity = fields[i]->isValid();
203 if(fieldValidity.length() > 0)
205 return fieldValidity;