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