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