]> SALOME platform Git repositories - modules/kernel.git/blob - src/Notebook/SALOME_EvalVariant.hxx
Salome HOME
Base implementation of Notebook
[modules/kernel.git] / src / Notebook / SALOME_EvalVariant.hxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22 //  File   : SALOME_EvalVariant.hxx
23 //  Author : Peter KURNEV
24 //  Module : SALOME
25
26 #ifndef SALOME_EvalVariant_Header_File
27 #define SALOME_EvalVariant_Header_File
28 //
29 #ifdef WNT
30 #pragma warning(disable : 4786)
31 #endif
32 //
33 #include <list>
34 #include <set>
35 #include <string>
36 #include <SALOME_Eval.hxx>
37 //
38 using namespace std;
39 //
40 typedef unsigned int uint;
41 //
42
43 class SALOME_EvalVariant;
44 class SALOME_EvalVariantData;
45 typedef list<SALOME_EvalVariant> SALOME_ListOfEvalVariant;
46 //
47 //  SALOME_EvalVariantType
48 enum SALOME_EvalVariantType {
49   SALOME_EvalVariant_Invalid,
50   SALOME_EvalVariant_Boolean,
51   SALOME_EvalVariant_Int,
52   SALOME_EvalVariant_UInt,
53   SALOME_EvalVariant_Double,
54   SALOME_EvalVariant_String,
55   SALOME_EvalVariant_List 
56 };
57 //
58 typedef list<SALOME_EvalVariantType> SALOME_ListOfEvalVariantType;
59 //
60 typedef set<SALOME_EvalVariantType> SALOME_SetOfEvalVariantType;
61 typedef SALOME_SetOfEvalVariantType::iterator SALOME_SetIteratorOfSetOfEvalVariantType;
62 typedef pair<SALOME_SetIteratorOfSetOfEvalVariantType, bool> SALOME_SetPairOfEvalVariantType;
63 //
64 //=======================================================================
65 //union   : SALOME_EvalVariantValue
66 //purpose  : 
67 //=======================================================================
68 union SALOME_EvalVariantValue {
69   bool   myBoolean;
70         int    myInt;
71         uint   myUInt;
72         double myDouble;
73         void*  myPtr;
74 };
75 //=======================================================================
76 //class : SALOME_EvalVariantData
77 //purpose  : 
78 //=======================================================================
79 class SALOME_EvalVariantData {
80   public:
81     SALOME_EvalVariantData(){
82       myType=SALOME_EvalVariant_Invalid;
83       myValue.myBoolean=false;
84       myValue.myInt=0;
85       myValue.myUInt=0;
86       myValue.myDouble=0.;
87       myValue.myPtr=NULL;
88     };
89     //
90     ~SALOME_EvalVariantData(){
91       clear();
92     };
93     //
94     SALOME_EvalVariantData(const SALOME_EvalVariantData& other); 
95     SALOME_EvalVariantData& operator=(const SALOME_EvalVariantData& other); 
96     bool operator==(const SALOME_EvalVariantData& other) const; 
97     //
98     void clear(){
99       if (myType==SALOME_EvalVariant_String) {
100         if (myValue.myPtr) {
101           SALOME_String *pStr=(SALOME_String *)myValue.myPtr;
102           delete pStr;
103           myValue.myPtr=NULL;
104         }
105         
106       }
107       else if (myType==SALOME_EvalVariant_List) {
108         if (myValue.myPtr) {
109           SALOME_ListOfEvalVariant *p=(SALOME_ListOfEvalVariant *)myValue.myPtr;
110           delete p;
111           myValue.myPtr=NULL;
112         }
113       }
114       else {
115         myValue.myBoolean=false;
116         myValue.myInt=0;
117         myValue.myUInt=0;
118         myValue.myDouble=0.;
119         myValue.myPtr=NULL;
120       }
121     };
122     //
123     SALOME_EvalVariantType   myType;
124     SALOME_EvalVariantValue  myValue;
125 };
126 //=======================================================================
127 //class : SALOME_EvalVariant
128 //purpose  : 
129 //=======================================================================
130 class SALOME_EvalVariant
131 {
132 public:  
133   SALOME_EvalVariant()
134   {
135   }; 
136   
137   //
138   ~SALOME_EvalVariant()
139   {
140     myData.clear();
141   };
142
143   //
144   SALOME_EvalVariant(const SALOME_EvalVariant& other) : myData(other.myData)
145   {
146   };
147     //  
148     SALOME_EvalVariant& operator=(const SALOME_EvalVariant& other) {
149       myData=other.myData;
150       return *this;
151     };
152     //
153     bool operator==(const SALOME_EvalVariant& theOther) const {
154       return myData==theOther.myData;;
155     };
156     //
157     ////  
158   void operator=(const SALOME_String& aString); 
159     void operator=(const bool theValue); 
160     void operator=(const int theValue); 
161     void operator=(const uint theValue); 
162     void operator=(const double theValue); 
163     void operator=(const char* theValue); 
164     ////
165     SALOME_EvalVariant(bool b);
166     SALOME_EvalVariant(int i);
167     SALOME_EvalVariant(uint ui);
168     SALOME_EvalVariant(double d);
169     SALOME_EvalVariant(const SALOME_String& s);
170     SALOME_EvalVariant(const SALOME_ListOfEvalVariant& s);
171     //
172     SALOME_EvalVariantType type() const {
173       return DataType();
174     };
175     //
176     bool isValid() const {
177       return type()!=SALOME_EvalVariant_Invalid;
178     }
179     //
180     bool isNull() const {
181       return !isValid();
182     };
183     //
184     void clear() {
185       myData.clear();
186     };
187     //
188     bool toBool() const;
189     int  toInt(bool *ok) const;
190     uint toUInt(bool *ok) const;
191     double toDouble(bool *ok) const;
192     SALOME_String toString() const;
193     SALOME_ListOfEvalVariant toList() const; 
194     //
195     const SALOME_EvalVariantData& Data() const{
196       return myData;
197     };
198     //
199     static bool contains(const SALOME_ListOfEvalVariant& aL,
200                          const SALOME_EvalVariant& aV);
201     //
202   protected:
203     SALOME_EvalVariantType& ChangeDataType() {
204       return ChangeData().myType;
205     };
206     //
207     SALOME_EvalVariantData& ChangeData() {
208       return myData;
209     };
210     //
211     SALOME_EvalVariantValue& ChangeDataValue() {
212       return ChangeData().myValue;
213     };
214     //
215     bool& ChangeDataValueBoolean() {
216       return ChangeDataValue().myBoolean;
217     };
218     //
219     int& ChangeDataValueInt() {
220       return ChangeDataValue().myInt;
221     };
222     //
223     uint& ChangeDataValueUInt() {
224       return ChangeDataValue().myUInt;
225     };
226     //
227     double& ChangeDataValueDouble() {
228       return ChangeDataValue().myDouble;
229     };
230     //
231     const SALOME_EvalVariantType& DataType() const{
232       return Data().myType;
233     };
234     //
235     const SALOME_EvalVariantValue& DataValue() const{
236       return Data().myValue;
237     };
238     //
239     bool DataValueBoolean() const {
240       return DataValue().myBoolean;
241     };
242     //
243     int DataValueInt() const {
244       return DataValue().myInt;
245     };
246     //
247     uint DataValueUInt() const {
248       return DataValue().myUInt;
249     };
250     //
251     double DataValueDouble() const {
252       return DataValue().myDouble;
253     };
254     //
255   const SALOME_String& DataValueString() const {
256       return *((string *)(DataValue().myPtr));
257     };
258     //
259     const SALOME_ListOfEvalVariant& DataValueList() const {
260       return *((SALOME_ListOfEvalVariant *)(DataValue().myPtr));
261     };
262     //
263   protected:
264     SALOME_EvalVariantData myData;// data itself
265 };
266
267 #endif