Salome HOME
Merge remote-tracking branch 'remotes/origin/master' into BR_coding_rules
[modules/shaper.git] / src / Model / Model_Expression.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        Model_Expression.cpp
4 // Created:     5 Aug 2015
5 // Author:      Sergey POKHODENKO
6
7 #include "Model_Expression.h"
8
9 #include <TCollection_AsciiString.hxx>
10 #include <TCollection_ExtendedString.hxx>
11 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
12 #include <TDataStd_UAttribute.hxx>
13
14 #include <TDataStd_RealArray.hxx>
15 #include <TDataStd_ExtStringArray.hxx>
16
17 #include <limits>
18
19 static Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
20
21
22 Model_Expression::Model_Expression(TDF_Label& theLabel)
23 {
24   if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
25     myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
26   }
27   if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myError)) {
28     myError = TDataStd_Comment::Set(theLabel, TCollection_ExtendedString());
29   }
30   if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myUsedParameters)) {
31     myUsedParameters = TDataStd_ExtStringList::Set(theLabel);
32   }
33 }
34
35 void Model_Expression::setText(const std::string& theValue)
36 {
37   if (text() != theValue) {
38     myText->Set(TCollection_ExtendedString(theValue.c_str()));
39     myIsInitialized = true; // the value will be set very soon
40   }
41
42   setError(text().empty() ? "" : "Not a double value.");
43 }
44
45 std::string Model_Expression::text() const
46 {
47   return TCollection_AsciiString(myText->Get()).ToCString();
48 }
49
50 void Model_Expression::setError(const std::string& theError)
51 {
52   if (error() != theError)
53     myError->Set(TCollection_ExtendedString(theError.c_str()));
54 }
55
56 std::string Model_Expression::error()
57 {
58   return TCollection_AsciiString(myError->Get()).ToCString();
59 }
60
61 void Model_Expression::setUsedParameters(const std::set<std::string>& theUsedParameters)
62 {
63   myUsedParameters->Clear();
64   std::set<std::string>::const_iterator anIt = theUsedParameters.begin();
65   for (; anIt != theUsedParameters.end(); ++anIt)
66     myUsedParameters->Append(TCollection_ExtendedString(anIt->c_str()));
67 }
68
69 std::set<std::string> Model_Expression::usedParameters() const
70 {
71   std::set<std::string> aResult;
72   TDataStd_ListIteratorOfListOfExtendedString aIt;
73   for (aIt.Initialize(myUsedParameters->List()); aIt.More(); aIt.Next())
74     aResult.insert(TCollection_AsciiString(aIt.Value()).ToCString());
75   return aResult;
76 }
77
78 ///////////////// Model_ExpressionDouble /////////////
79 Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel)
80     : Model_Expression(theLabel)
81 {
82   myLab = theLabel;
83   reinit();
84 }
85
86 void Model_ExpressionDouble::reinit()
87 {
88   if (!myLab.FindAttribute(TDataStd_Real::GetID(), myReal)) {
89     myIsInitialized = false;
90   } else {
91     myIsInitialized = true;
92   }
93 }
94
95 void Model_ExpressionDouble::setValue(const double theValue)
96 {
97   if (!myIsInitialized || myReal.IsNull()) {
98     myReal = TDataStd_Real::Set(myText->Label(), theValue);
99     myIsInitialized = true;
100   } else if (value() != theValue) {
101     myReal->Set(theValue);
102   }
103 }
104
105 double Model_ExpressionDouble::value()
106 {
107   if (myIsInitialized)
108     return myReal->Get();
109   return std::numeric_limits<double>::max(); // error
110 }
111
112 void Model_ExpressionDouble::setInvalid(const bool theFlag)
113 {
114   if (theFlag) {
115     TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
116   } else {
117     myText->Label().ForgetAttribute(kInvalidGUID);
118   }
119 }
120
121 bool Model_ExpressionDouble::isInvalid()
122 {
123   return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
124 }
125
126 ///////////////// Model_ExpressionInteger /////////////
127 Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel)
128     : Model_Expression(theLabel)
129 {
130   myLab = theLabel;
131   reinit();
132 }
133
134 void Model_ExpressionInteger::reinit()
135 {
136   if (!myLab.FindAttribute(TDataStd_Integer::GetID(), myInteger)) {
137     myIsInitialized = false;
138   } else {
139     myIsInitialized = true;
140   }
141 }
142
143 void Model_ExpressionInteger::setValue(const int theValue)
144 {
145   if (!myIsInitialized || myInteger.IsNull()) {
146     myInteger = TDataStd_Integer::Set(myText->Label(), theValue);
147     myIsInitialized = true;
148   } else if (value() != theValue) {
149     myInteger->Set(theValue);
150   }
151 }
152
153 int Model_ExpressionInteger::value()
154 {
155   if (myIsInitialized)
156     return myInteger->Get();
157   return std::numeric_limits<int>::max(); // error
158 }
159
160 void Model_ExpressionInteger::setInvalid(const bool theFlag)
161 {
162   if (theFlag) {
163     TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
164   } else {
165     myText->Label().ForgetAttribute(kInvalidGUID);
166   }
167 }
168
169 bool Model_ExpressionInteger::isInvalid()
170 {
171   return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
172 }