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