]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/ArrayValue.cpp
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / cpp / DataBoardTest / DataBoard / ArrayValue.cpp
1 #include "ArrayValue.h"
2
3 #include "ArrayType.h"
4 #include "Range.h"
5 #include "Limit.h"
6
7 #include "Constants.h"
8
9 namespace Databoard {
10         namespace Value {
11                 ArrayValue::ArrayValue(Databoard::Type::DataType* dataBoard) : Value(dataBoard)
12                 {
13                 }
14
15                 ArrayValue::~ArrayValue()
16                 {
17                         clear();
18                 }
19
20                 void ArrayValue::add(Value* value)
21                 {
22                         values.push_back(value);
23                 }
24
25                 //void insert(Value* value, int index);
26                 int ArrayValue::count()
27                 {
28                         return (int)values.size();
29                 }
30
31                 void ArrayValue::remove(int index, int count)
32                 {
33                         if(index >= 0 && (index+count) <= (int)values.size())
34                         {
35                                 for(int i = index; i < count; ++i)
36                                 {
37                                         delete values.at(i);
38                                 }
39
40                                 values.erase(values.begin(), values.begin() + count);
41                         }
42                 }
43
44                 void ArrayValue::clear()
45                 {
46                         remove(0, count());
47                 }
48                 
49                 Value* ArrayValue::get(int index)
50                 {
51                         return values.at(index);
52                 }
53
54                 std::vector<Value*> ArrayValue::getAll()
55                 {
56                         std::vector<Value*> v;
57
58                         for(int i = 0; i < (int)values.size(); ++i)
59                         {
60                                 v.push_back(values.at(i));
61                         }
62
63                         return v;
64                 }
65
66
67                 std::string ArrayValue::writeOut(int /*indent*/)
68                 {
69                         std::string s;
70
71                         s += "[";
72
73                         for(int i = 0; i < (int)values.size(); ++i)
74                         {
75                                 s += values.at(i)->writeOut();
76
77                                 if(i != ((int)values.size()-1))
78                                 {
79                                         s += ",";
80                                 }
81                         }
82
83                         s += "]";
84
85                         return s;
86                 }
87
88                 bool ArrayValue::equals(const Value* other)
89                 {
90                         ArrayValue* o = (ArrayValue*)other;
91
92                         if(this->count() < o->count())
93                         {
94                                 return true;
95                         }
96                         else if(this->count() > o->count())
97                         {
98                                 return false;
99                         }
100
101                         //for(int i = 0; i <(int)this->count(); ++i)
102                         //{
103                         //      if(this->getField(i) < o->getField(i))
104                         //      {
105                         //              return true;
106                         //      }
107                         //}
108
109                         return true;//(this->value < o->value);
110
111                 }
112
113                 std::string ArrayValue::isValid()
114                 {
115                         Databoard::Type::ArrayType* arrayType = dynamic_cast<Databoard::Type::ArrayType*>(dataBoard);
116
117                         if(arrayType == NULL)
118                         {
119                                 return "ArrayValue: type is not an array.";
120                         }
121
122                         Range* r = arrayType->getLength();
123
124                         int countMin = r->getLower()->getInteger(INT_MIN);
125                         int countMax = r->getUpper()->getInteger(INT_MAX);
126
127                         int count = (int)values.size();
128
129                         if(count < countMin)
130                         {
131                                 return "ArrayValue: too few.";
132                         }
133                         else if(count > countMax)
134                         {
135                                 return "ArrayValue: too many.";
136                         }
137
138                         return STR_EMPTY;
139                 }
140         }
141 }