]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/cpp/DataBoardTest/DataBoard/IntegerValue.cpp
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / cpp / DataBoardTest / DataBoard / IntegerValue.cpp
1 #include "IntegerValue.h"
2
3 #include "NumberType.h"
4 #include "IntegerType.h"
5 #include "Range.h"
6 #include "Limit.h"
7
8 #include <sstream>
9 #include "Constants.h"
10
11 namespace Databoard {
12         namespace Value {
13                 IntegerValue::IntegerValue(Databoard::Type::DataType* dataBoard) : Value(dataBoard)
14                 {
15                 }
16
17                 IntegerValue::~IntegerValue()
18                 {
19                 }
20
21                 void IntegerValue::setValue(int value)
22                 {
23                         Databoard::Type::IntegerType* integerType = dynamic_cast<Databoard::Type::IntegerType*>(dataBoard);
24
25                         if(integerType != NULL)
26                         {
27                                 this->value = value;
28                         }
29                 }
30
31                 int IntegerValue::getValue()
32                 {
33                         return value;
34                 }
35
36                 std::string IntegerValue::writeOut(int /*indent*/)
37                 {
38                         std::string s;
39
40                         std::stringstream oss;
41
42                         oss << value;
43
44                         oss >> s;
45
46                         return s;
47                 }
48
49                 bool IntegerValue::equals(const Value* other)
50                 {
51                         IntegerValue* o = (IntegerValue*)other;
52
53                         return (this->value < o->value);
54                 }
55
56                 std::string IntegerValue::isValid()
57                 {
58                         if(dataBoard == NULL)
59                         {
60                                 return "IntegerValue: There is no datatype.";
61                         }
62
63                         Databoard::Type::IntegerType* integerType = (Databoard::Type::IntegerType*)dataBoard;
64
65                         if(integerType->getRange() == NULL)
66                         {
67                                 return STR_EMPTY;
68                         }
69
70                         int minValue = integerType->minValue();
71                         int maxValue = integerType->maxValue();
72
73                         if(integerType->getRange()->getLower()->getInclusive() == true)
74                         {
75                                 if(value < minValue)
76                                 {
77                                         return std::string("IntegerValue: ") + STR_ERROR_VALUE_SMALL;
78                                 }
79                         }
80                         else //if(integerType->getRange()->getLower()->getInclusive() == false)
81                         {
82                                 if(value <= minValue)
83                                 {
84                                         return std::string("IntegerValue: ") + STR_ERROR_VALUE_SMALL;
85                                 }
86                         }
87
88                         if(integerType->getRange()->getUpper()->getInclusive() == true)
89                         {
90                                 if(value > maxValue)
91                                 {
92                                         return std::string("IntegerValue: ") + STR_ERROR_VALUE_LARGE;
93                                 }
94                         }
95                         else //if(integerType->getRange()->getUpper()->getInclusive() == false)
96                         {
97                                 if(value >= maxValue)
98                                 {
99                                         return std::string("IntegerValue: ") + STR_ERROR_VALUE_LARGE;
100                                 }
101                         }
102
103                         return STR_EMPTY;
104                 }
105         }
106 }