1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: Model_Expression.cpp
5 // Author: Sergey POKHODENKO
7 #include "Model_Expression.h"
9 #include <TCollection_AsciiString.hxx>
10 #include <TCollection_ExtendedString.hxx>
11 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
12 #include <TDataStd_UAttribute.hxx>
14 #include <TDataStd_RealArray.hxx>
15 #include <TDataStd_ExtStringArray.hxx>
19 static Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
22 Model_Expression::Model_Expression(TDF_Label& theLabel)
24 if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
25 myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
27 if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myError)) {
28 myError = TDataStd_Comment::Set(theLabel, TCollection_ExtendedString());
30 if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myUsedParameters)) {
31 myUsedParameters = TDataStd_ExtStringList::Set(theLabel);
35 void Model_Expression::setText(const std::string& theValue)
37 if (text() != theValue) {
38 myText->Set(TCollection_ExtendedString(theValue.c_str()));
39 myIsInitialized = true; // the value will be set very soon
42 setError(text().empty() ? "" : "Not a double value.");
45 std::string Model_Expression::text() const
47 return TCollection_AsciiString(myText->Get()).ToCString();
50 void Model_Expression::setError(const std::string& theError)
52 if (error() != theError)
53 myError->Set(TCollection_ExtendedString(theError.c_str()));
56 std::string Model_Expression::error()
58 return TCollection_AsciiString(myError->Get()).ToCString();
61 void Model_Expression::setUsedParameters(const std::set<std::string>& theUsedParameters)
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()));
69 std::set<std::string> Model_Expression::usedParameters() const
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());
78 ///////////////// Model_ExpressionDouble /////////////
79 Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel)
80 : Model_Expression(theLabel)
86 void Model_ExpressionDouble::reinit()
88 if (!myLab.FindAttribute(TDataStd_Real::GetID(), myReal)) {
89 myIsInitialized = false;
91 myIsInitialized = true;
95 void Model_ExpressionDouble::setValue(const double theValue)
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);
105 double Model_ExpressionDouble::value()
108 return myReal->Get();
109 return std::numeric_limits<double>::max(); // error
112 void Model_ExpressionDouble::setInvalid(const bool theFlag)
115 TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
117 myText->Label().ForgetAttribute(kInvalidGUID);
121 bool Model_ExpressionDouble::isInvalid()
123 return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
126 ///////////////// Model_ExpressionInteger /////////////
127 Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel)
128 : Model_Expression(theLabel)
134 void Model_ExpressionInteger::reinit()
136 if (!myLab.FindAttribute(TDataStd_Integer::GetID(), myInteger)) {
137 myIsInitialized = false;
139 myIsInitialized = true;
143 void Model_ExpressionInteger::setValue(const int theValue)
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);
153 int Model_ExpressionInteger::value()
156 return myInteger->Get();
157 return std::numeric_limits<int>::max(); // error
160 void Model_ExpressionInteger::setInvalid(const bool theFlag)
163 TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
165 myText->Label().ForgetAttribute(kInvalidGUID);
169 bool Model_ExpressionInteger::isInvalid()
171 return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;