#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; } } }