1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "Model_Expression.h"
23 #include <TCollection_AsciiString.hxx>
24 #include <TCollection_ExtendedString.hxx>
25 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
26 #include <TDataStd_UAttribute.hxx>
28 #include <TDataStd_RealArray.hxx>
29 #include <TDataStd_ExtStringArray.hxx>
33 static Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
36 Model_Expression::Model_Expression(TDF_Label& theLabel)
38 if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
39 myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
41 if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myError)) {
42 myError = TDataStd_Comment::Set(theLabel, TCollection_ExtendedString());
44 if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myUsedParameters)) {
45 myUsedParameters = TDataStd_ExtStringList::Set(theLabel);
49 void Model_Expression::setText(const std::string& theValue)
51 if (text() != theValue) {
52 myText->Set(TCollection_ExtendedString(theValue.c_str()));
53 myIsInitialized = true; // the value will be set very soon
56 setError(text().empty() ? "" : "Not a double value.");
59 std::string Model_Expression::text() const
61 return TCollection_AsciiString(myText->Get()).ToCString();
64 void Model_Expression::setError(const std::string& theError)
66 if (error() != theError)
67 myError->Set(TCollection_ExtendedString(theError.c_str()));
70 std::string Model_Expression::error()
72 return TCollection_AsciiString(myError->Get()).ToCString();
75 void Model_Expression::setUsedParameters(const std::set<std::string>& theUsedParameters)
77 myUsedParameters->Clear();
78 std::set<std::string>::const_iterator anIt = theUsedParameters.begin();
79 for (; anIt != theUsedParameters.end(); ++anIt)
80 myUsedParameters->Append(TCollection_ExtendedString(anIt->c_str()));
83 std::set<std::string> Model_Expression::usedParameters() const
85 std::set<std::string> aResult;
86 TDataStd_ListIteratorOfListOfExtendedString aIt;
87 for (aIt.Initialize(myUsedParameters->List()); aIt.More(); aIt.Next())
88 aResult.insert(TCollection_AsciiString(aIt.Value()).ToCString());
92 ///////////////// Model_ExpressionDouble /////////////
93 Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel)
94 : Model_Expression(theLabel)
100 void Model_ExpressionDouble::reinit()
102 if (!myLab.FindAttribute(TDataStd_Real::GetID(), myReal)) {
103 myIsInitialized = false;
105 myIsInitialized = true;
109 void Model_ExpressionDouble::setValue(const double theValue)
111 if (!myIsInitialized || myReal.IsNull()) {
112 myReal = TDataStd_Real::Set(myText->Label(), theValue);
113 myIsInitialized = true;
114 } else if (value() != theValue) {
115 myReal->Set(theValue);
119 double Model_ExpressionDouble::value()
121 if (myIsInitialized && !myReal.IsNull())
122 return myReal->Get();
123 return std::numeric_limits<double>::max(); // error
126 void Model_ExpressionDouble::setInvalid(const bool theFlag)
129 TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
131 myText->Label().ForgetAttribute(kInvalidGUID);
135 bool Model_ExpressionDouble::isInvalid()
137 return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
140 ///////////////// Model_ExpressionInteger /////////////
141 Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel)
142 : Model_Expression(theLabel)
148 void Model_ExpressionInteger::reinit()
150 if (!myLab.FindAttribute(TDataStd_Integer::GetID(), myInteger)) {
151 myIsInitialized = false;
153 myIsInitialized = true;
157 void Model_ExpressionInteger::setValue(const int theValue)
159 if (!myIsInitialized || myInteger.IsNull()) {
160 myInteger = TDataStd_Integer::Set(myText->Label(), theValue);
161 myIsInitialized = true;
162 } else if (value() != theValue) {
163 myInteger->Set(theValue);
167 int Model_ExpressionInteger::value()
169 if (myIsInitialized && !myInteger.IsNull())
170 return myInteger->Get();
171 return std::numeric_limits<int>::max(); // error
174 void Model_ExpressionInteger::setInvalid(const bool theFlag)
177 TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
179 myText->Label().ForgetAttribute(kInvalidGUID);
183 bool Model_ExpressionInteger::isInvalid()
185 return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;