X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fcpp%2FDataBoardTest%2FDataBoard%2FArrayValue.cpp;fp=bundles%2Forg.simantics.databoard%2Fcpp%2FDataBoardTest%2FDataBoard%2FArrayValue.cpp;h=2fc0e5df53b0b5a4b50cd5347ebfefbc3c28d32c;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/ArrayValue.cpp b/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/ArrayValue.cpp new file mode 100644 index 000000000..2fc0e5df5 --- /dev/null +++ b/bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/ArrayValue.cpp @@ -0,0 +1,141 @@ +#include "ArrayValue.h" + +#include "ArrayType.h" +#include "Range.h" +#include "Limit.h" + +#include "Constants.h" + +namespace Databoard { + namespace Value { + ArrayValue::ArrayValue(Databoard::Type::DataType* dataBoard) : Value(dataBoard) + { + } + + ArrayValue::~ArrayValue() + { + clear(); + } + + void ArrayValue::add(Value* value) + { + values.push_back(value); + } + + //void insert(Value* value, int index); + int ArrayValue::count() + { + return (int)values.size(); + } + + void ArrayValue::remove(int index, int count) + { + if(index >= 0 && (index+count) <= (int)values.size()) + { + for(int i = index; i < count; ++i) + { + delete values.at(i); + } + + values.erase(values.begin(), values.begin() + count); + } + } + + void ArrayValue::clear() + { + remove(0, count()); + } + + Value* ArrayValue::get(int index) + { + return values.at(index); + } + + std::vector ArrayValue::getAll() + { + std::vector v; + + for(int i = 0; i < (int)values.size(); ++i) + { + v.push_back(values.at(i)); + } + + return v; + } + + + std::string ArrayValue::writeOut(int /*indent*/) + { + std::string s; + + s += "["; + + for(int i = 0; i < (int)values.size(); ++i) + { + s += values.at(i)->writeOut(); + + if(i != ((int)values.size()-1)) + { + s += ","; + } + } + + s += "]"; + + return s; + } + + bool ArrayValue::equals(const Value* other) + { + ArrayValue* o = (ArrayValue*)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;//(this->value < o->value); + + } + + std::string ArrayValue::isValid() + { + Databoard::Type::ArrayType* arrayType = dynamic_cast(dataBoard); + + if(arrayType == NULL) + { + return "ArrayValue: type is not an array."; + } + + Range* r = arrayType->getLength(); + + int countMin = r->getLower()->getInteger(INT_MIN); + int countMax = r->getUpper()->getInteger(INT_MAX); + + int count = (int)values.size(); + + if(count < countMin) + { + return "ArrayValue: too few."; + } + else if(count > countMax) + { + return "ArrayValue: too many."; + } + + return STR_EMPTY; + } + } +}