X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fcpp%2FDataBoardTest%2FDataBoard%2FRecordValue.cpp;fp=bundles%2Forg.simantics.databoard%2Fcpp%2FDataBoardTest%2FDataBoard%2FRecordValue.cpp;h=0fda4ad431d90fa10327997b463df68ed9dfb253;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/RecordValue.cpp b/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/RecordValue.cpp new file mode 100644 index 000000000..0fda4ad43 --- /dev/null +++ b/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/RecordValue.cpp @@ -0,0 +1,213 @@ +#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; + } + } +}