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