Salome HOME
Bug correction EDF12462 (3/3)
[modules/yacs.git] / src / engine / Any.hxx
1 // Copyright (C) 2006-2015  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #ifndef __YACSANY_HXX__
21 #define __YACSANY_HXX__
22
23 #include "YACSlibEngineExport.hxx"
24 #include "RefCounter.hxx"
25 #include "Exception.hxx"
26 #include "SharedPtr.hxx"
27
28 #include <vector>
29
30 namespace YACS
31 {
32   namespace ENGINE
33   {
34     class Any;
35     class AtomAny;
36     class TypeCode;
37     class SeqAlloc;
38     class ArrayAny;
39     class StructAny;
40     class SequenceAny;
41     class TypeCodeArray;
42     class TypeCodeStruct;
43     typedef void (*Deallocator)(void *);
44
45     class YACSLIBENGINE_EXPORT StringOnHeap
46     {
47       friend class Any;
48       friend class AtomAny;
49       friend class ArrayAny;
50     private:
51       StringOnHeap(const char *val);
52       StringOnHeap(const std::string& val);
53       StringOnHeap(char *val, Deallocator deAlloc);
54       bool operator ==(const StringOnHeap& other) const;
55       StringOnHeap *deepCopy() const;
56       const char *cStr() const { return _str; }
57       ~StringOnHeap();
58     private:
59       char *_str;
60       Deallocator _dealloc;
61     };
62
63     typedef SharedPtr<Any> AnyPtr;
64     
65     /*!
66      * \brief: Interface for management of storage of data formated dynamically in its TypeCode.
67      *         Warning virtual inheritance on Any daughter classes NOT supported.
68      */
69     class YACSLIBENGINE_EXPORT Any : public RefCounter
70     {
71       friend class SeqAlloc;
72       friend class ArrayAny;
73       friend class StructAny;
74       friend class SequenceAny;
75     public:
76       const TypeCode *getType() const { return _type; }
77       //for convenience methods
78       virtual Any *clone() const = 0;
79       virtual AnyPtr operator[](int i) const throw(Exception) = 0;
80       virtual AnyPtr operator[](const char *key) const throw(Exception) = 0;
81       virtual bool operator ==(const Any& other) const = 0;
82       virtual int getIntValue() const throw(Exception) = 0;
83       virtual bool getBoolValue() const throw(Exception) = 0;
84       virtual double getDoubleValue() const throw(Exception) = 0;
85       virtual std::string getStringValue() const throw(Exception) = 0;
86       //
87     protected:
88 #ifndef SWIG
89       virtual ~Any();
90 #endif
91       Any(TypeCode* type);
92       Any(const Any& other);
93       virtual void putMyReprAtPlace(char *data) const = 0;
94       //static AnyPtr buildAnyFromCoarseData(char *data, TypeCode* type); //Factory Method
95       static bool IsNull(char *data);
96     protected:
97       TypeCode* _type;
98     };
99
100     typedef SharedPtr<AtomAny> AtomAnyPtr;
101
102     class YACSLIBENGINE_EXPORT AtomAny : public Any
103     {
104       friend class TypeCode;
105
106       union ValueContainer
107       {
108         int _i;
109         bool _b;
110         double _d;
111         StringOnHeap *_s;
112       };
113     public:
114       Any *clone() const;
115       template<class T>
116       static AtomAny *New(T val) { return new AtomAny(val); }
117       static AtomAny *New(char *val, Deallocator dealloc);
118       AnyPtr operator[](int i) const throw(Exception);
119       AnyPtr operator[](const char *key) const throw(Exception);
120       bool operator ==(const Any& other) const;
121       int getIntValue() const throw(Exception);
122       bool getBoolValue() const throw(Exception);
123       double getDoubleValue() const throw(Exception);
124       std::string getStringValue() const throw(Exception);
125     protected:
126       void putMyReprAtPlace(char *data) const;
127       static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
128       static void destroyReprAtPlace(char *data, const TypeCode *type);
129       static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
130       static bool takeInChargeStorageOf(TypeCode *type);
131     private:
132       ~AtomAny();
133       AtomAny(int val);
134       AtomAny(bool val);
135       AtomAny(double val);
136       AtomAny(const char *val);
137       AtomAny(const std::string& val);
138       AtomAny(const AtomAny& other);
139       AtomAny(char *data, TypeCode* type);
140       AtomAny(char *val, Deallocator deAlloc);
141     protected:
142       ValueContainer _value;
143     };
144     
145     class YACSLIBENGINE_EXPORT SeqAlloc 
146     {
147       friend class SequenceAny;
148       
149       char *_start;
150       char *_finish;
151       char *_endOfStorage;
152       Deallocator _notStdDeAlloc;
153       const unsigned int _sizeOf1Elm;
154     private:
155       SeqAlloc(const SeqAlloc& other);
156       SeqAlloc(unsigned int sizeOf1Elm);
157       ~SeqAlloc();
158       void clear();
159       void initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc);
160       void construct(char *pt, const Any *val);
161       void construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy);
162       char *allocate(unsigned int nbOfByte);
163       void destroy(char *pt, const TypeCode *tc);
164       void deallocate(char *pt);
165       unsigned int size() const;
166       std::vector<unsigned int> getSetItems() const;
167     public:
168       static const char DFT_CHAR_VAR;
169     };
170     
171     class YACSLIBENGINE_EXPORT ComposedAny : public Any
172     {
173     public:
174       virtual void setEltAtRank(int i, const Any *elem) throw(Exception) = 0;
175       AnyPtr operator[](const char *key) const throw(Exception);
176     protected:
177       ComposedAny(const ComposedAny& other);
178       ComposedAny(TypeCode* type, bool isNew=true);
179     protected:
180       void checkTypeOf(const Any *elem) const throw(Exception);
181     private://error methods called during incorrect runtime extraction
182       int getIntValue() const throw(Exception);
183       bool getBoolValue() const throw(Exception);
184       double getDoubleValue() const throw(Exception);
185       std::string getStringValue() const throw(Exception);
186     };
187
188     typedef SharedPtr<SequenceAny> SequenceAnyPtr;
189     
190     class YACSLIBENGINE_EXPORT SequenceAny : public ComposedAny
191     {
192       friend class TypeCodeSeq;
193     public:
194       void clear();
195       void popBack();
196       unsigned int size() const { return _alloc.size(); }
197       void pushBack(const Any *elem);
198       bool operator ==(const Any& other) const;
199       void setEltAtRank(int i, const Any *elem) throw(Exception);
200       AnyPtr operator[](int i) const throw(Exception);
201       Any *clone() const;
202       template<class T>
203       static SequenceAny *New(const std::vector<T>& vec);
204       static SequenceAny *New(const TypeCode *typeOfContent);
205       static SequenceAny *New(const TypeCode *typeOfContent, unsigned lgth);
206       template<class T>
207       static SequenceAny *New(T *val, unsigned int lgth, Deallocator deAlloc);
208       std::vector<unsigned int> getSetItems() const { return _alloc.getSetItems(); }
209       SequenceAny *removeUnsetItemsFromThis() const;
210     protected:
211       void putMyReprAtPlace(char *data) const;
212       static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
213       static void destroyReprAtPlace(char *data, const TypeCode *type);
214       static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
215       static bool takeInChargeStorageOf(TypeCode *type);
216     private:
217       ~SequenceAny();
218       SequenceAny(const SequenceAny& other);
219       SequenceAny(const TypeCode *typeOfContent);
220       SequenceAny(const TypeCode *typeOfContent, unsigned lgth);
221       SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc);
222       SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc);
223       SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc);
224       SequenceAny(const std::vector<int>& val);
225       SequenceAny(const std::vector<bool>& val);
226       SequenceAny(const std::vector<double>& val);
227       SequenceAny(const std::vector<std::string>& val);
228       void realloc(char *endOfCurrentAllocated, const Any *elem);
229       char *performCpy(char *srcStart, char *srcFinish, char *destStart);
230     protected:
231       SeqAlloc _alloc;
232     };
233     
234     typedef SharedPtr<ArrayAny> ArrayAnyPtr;
235
236     class YACSLIBENGINE_EXPORT ArrayAny : public ComposedAny
237     {
238       friend class TypeCodeArray;
239     public:
240       void setEltAtRank(int i, const Any *elem) throw(Exception);
241       bool operator ==(const Any& other) const;
242       AnyPtr operator[](int i) const throw(Exception);
243       unsigned int size() const;
244       Any *clone() const;
245       template<class T>
246       static ArrayAny *New(const std::vector<T>& vec);
247       template<class T>
248       static ArrayAny *New(const T *val, unsigned int lgth);
249       static ArrayAny *New(const TypeCode *typeOfContent, unsigned int lgth);
250     protected:
251       void putMyReprAtPlace(char *data) const;
252       static void putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy);
253       static void destroyReprAtPlace(char *data, const TypeCodeArray *type);
254       static AnyPtr getOrBuildFromData(char *data, const TypeCodeArray *type);
255       static bool takeInChargeStorageOf(TypeCode *type);
256     private:
257       ~ArrayAny();
258       ArrayAny(const TypeCode *typeOfContent, unsigned int lgth);
259       ArrayAny(char *data, TypeCodeArray * type);
260       ArrayAny(const ArrayAny& other);
261       ArrayAny(const int *val, unsigned int lgth);
262       ArrayAny(const bool *val, unsigned int lgth);
263       ArrayAny(const double *val, unsigned int lgth);
264       ArrayAny(const std::vector<int>& val);
265       ArrayAny(const std::vector<double>& val);
266       ArrayAny(const std::vector<std::string>& val);
267     protected:
268       char *_data;
269     };
270
271     typedef SharedPtr<StructAny> StructAnyPtr;
272
273     class YACSLIBENGINE_EXPORT StructAny : public ComposedAny
274     {
275       friend class TypeCodeStruct;
276     public:
277       Any *clone() const;
278       bool operator ==(const Any& other) const;
279       static StructAny *New(TypeCodeStruct *type);
280       AnyPtr operator[](int i) const throw(Exception);
281       AnyPtr operator[](const char *key) const throw(Exception);
282       void setEltAtRank(int i, const Any *elem) throw(Exception);
283       void setEltAtRank(const char *key, const Any *elem) throw(Exception);
284     protected:
285       void putMyReprAtPlace(char *data) const;
286       static void putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy);
287       static void destroyReprAtPlace(char *data, const TypeCodeStruct *type);
288       static AnyPtr getOrBuildFromData(char *data, const TypeCodeStruct *type);
289     private:
290       ~StructAny();
291       StructAny(TypeCodeStruct *type);
292       StructAny(const StructAny& other);
293       StructAny(char *data, TypeCodeStruct * type);
294     private:
295       char *_data;
296     };
297
298     template<class T>
299     SequenceAny *SequenceAny::New(T *val, unsigned int lgth, Deallocator deAlloc)
300     {
301       return new SequenceAny(val,lgth,deAlloc);
302     }
303
304     template<class T>
305     SequenceAny *SequenceAny::New(const std::vector<T>& vec)
306     {
307       return new SequenceAny(vec);
308     }
309
310     template<class T>
311     ArrayAny *ArrayAny::New(const std::vector<T>& vec)
312     {
313       return new ArrayAny(vec);
314     }
315
316     template<class T>
317     ArrayAny *ArrayAny::New(const T *val, unsigned int lgth)
318     {
319       return new ArrayAny(val,lgth);
320     }
321   }
322 }
323
324 #endif