1 // Copyright (C) 2014-2020 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 email : webmaster.salome@opencascade.com
20 #include "Model_Expression.h"
22 #include <Locale_Convert.h>
24 #include <TCollection_AsciiString.hxx>
25 #include <TCollection_ExtendedString.hxx>
26 #include <TDataStd_ListIteratorOfListOfExtendedString.hxx>
27 #include <TDataStd_UAttribute.hxx>
29 #include <TDataStd_RealArray.hxx>
30 #include <TDataStd_ExtStringArray.hxx>
34 static Standard_GUID kInvalidGUID("caee5ce4-34b1-4b29-abcb-685287d18096");
37 Model_Expression::Model_Expression(TDF_Label& theLabel)
39 if (!theLabel.FindAttribute(TDataStd_Name::GetID(), myText)) {
40 myText = TDataStd_Name::Set(theLabel, TCollection_ExtendedString());
42 if (!theLabel.FindAttribute(TDataStd_Comment::GetID(), myError)) {
43 myError = TDataStd_Comment::Set(theLabel, TCollection_ExtendedString());
45 if (!theLabel.FindAttribute(TDataStd_ExtStringList::GetID(), myUsedParameters)) {
46 myUsedParameters = TDataStd_ExtStringList::Set(theLabel);
50 void Model_Expression::setText(const std::wstring& theValue)
52 if (text() != theValue) {
53 myText->Set(TCollection_ExtendedString(theValue.c_str()));
54 myIsInitialized = true; // the value will be set very soon
57 setError(text().empty() ? "" : "Not a double value.");
60 std::wstring Model_Expression::text() const
62 return Locale::Convert::toWString(myText->Get().ToExtString());
65 void Model_Expression::setError(const std::string& theError)
67 if (error() != theError)
68 myError->Set(TCollection_ExtendedString(theError.c_str()));
71 std::string Model_Expression::error()
73 return TCollection_AsciiString(myError->Get()).ToCString();
76 void Model_Expression::setUsedParameters(const std::set<std::wstring>& theUsedParameters)
78 myUsedParameters->Clear();
79 std::set<std::wstring>::const_iterator anIt = theUsedParameters.begin();
80 for (; anIt != theUsedParameters.end(); ++anIt)
81 myUsedParameters->Append(TCollection_ExtendedString(anIt->c_str()));
84 std::set<std::wstring> Model_Expression::usedParameters() const
86 std::set<std::wstring> aResult;
87 TDataStd_ListIteratorOfListOfExtendedString aIt;
88 for (aIt.Initialize(myUsedParameters->List()); aIt.More(); aIt.Next())
89 aResult.insert(Locale::Convert::toWString(aIt.Value().ToExtString()));
93 ///////////////// Model_ExpressionDouble /////////////
94 Model_ExpressionDouble::Model_ExpressionDouble(TDF_Label& theLabel)
95 : Model_Expression(theLabel)
101 void Model_ExpressionDouble::reinit()
103 if (!myLab.FindAttribute(TDataStd_Real::GetID(), myReal)) {
104 myIsInitialized = false;
106 myIsInitialized = true;
110 void Model_ExpressionDouble::setValue(const double theValue)
112 if (!myIsInitialized || myReal.IsNull()) {
113 myReal = TDataStd_Real::Set(myText->Label(), theValue);
114 myIsInitialized = true;
115 } else if (value() != theValue) {
116 myReal->Set(theValue);
120 double Model_ExpressionDouble::value()
122 if (myIsInitialized && !myReal.IsNull())
123 return myReal->Get();
124 return std::numeric_limits<double>::max(); // error
127 void Model_ExpressionDouble::setInvalid(const bool theFlag)
130 TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
132 myText->Label().ForgetAttribute(kInvalidGUID);
136 bool Model_ExpressionDouble::isInvalid()
138 return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;
141 ///////////////// Model_ExpressionInteger /////////////
142 Model_ExpressionInteger::Model_ExpressionInteger(TDF_Label& theLabel)
143 : Model_Expression(theLabel)
149 void Model_ExpressionInteger::reinit()
151 if (!myLab.FindAttribute(TDataStd_Integer::GetID(), myInteger)) {
152 myIsInitialized = false;
154 myIsInitialized = true;
158 void Model_ExpressionInteger::setValue(const int theValue)
160 if (!myIsInitialized || myInteger.IsNull()) {
161 myInteger = TDataStd_Integer::Set(myText->Label(), theValue);
162 myIsInitialized = true;
163 } else if (value() != theValue) {
164 myInteger->Set(theValue);
168 int Model_ExpressionInteger::value()
170 if (myIsInitialized && !myInteger.IsNull())
171 return myInteger->Get();
172 return std::numeric_limits<int>::max(); // error
175 void Model_ExpressionInteger::setInvalid(const bool theFlag)
178 TDataStd_UAttribute::Set(myText->Label(), kInvalidGUID);
180 myText->Label().ForgetAttribute(kInvalidGUID);
184 bool Model_ExpressionInteger::isInvalid()
186 return myText->Label().IsAttribute(kInvalidGUID) == Standard_True;