X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=bundles%2Forg.simantics.databoard%2Fcpp%2FDataBoardTest%2FDataBoard%2FRecordValue.cpp;h=a9ee811da4e53b0e7c5ee54dd6aee5718e5469ce;hb=refs%2Fchanges%2F38%2F238%2F2;hp=0fda4ad431d90fa10327997b463df68ed9dfb253;hpb=24e2b34260f219f0d1644ca7a138894980e25b14;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 index 0fda4ad43..a9ee811da 100644 --- a/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/RecordValue.cpp +++ b/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/RecordValue.cpp @@ -1,213 +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; - } - } -} +#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; + } + } +}