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