]> SALOME platform Git repositories - modules/yacs.git/blob - src/engine/Any.hxx
Salome HOME
Error management in py2yacs.
[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     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 throw(Exception) = 0;
89       virtual AnyPtr operator[](const char *key) const throw(Exception) = 0;
90       virtual bool operator ==(const Any& other) const = 0;
91       virtual int getIntValue() const throw(Exception) = 0;
92       virtual bool getBoolValue() const throw(Exception) = 0;
93       virtual double getDoubleValue() const throw(Exception) = 0;
94       virtual std::string getStringValue() const throw(Exception) = 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
115       union ValueContainer
116       {
117         int _i;
118         bool _b;
119         double _d;
120         StringOnHeap *_s;
121       };
122     public:
123       Any *clone() const;
124       template<class T>
125       static AtomAny *New(T val) { return new AtomAny(val); }
126       static AtomAny *New(char *val, Deallocator dealloc);
127       AnyPtr operator[](int i) const throw(Exception);
128       AnyPtr operator[](const char *key) const throw(Exception);
129       bool operator ==(const Any& other) const;
130       int getIntValue() const throw(Exception);
131       bool getBoolValue() const throw(Exception);
132       double getDoubleValue() const throw(Exception);
133       std::string getStringValue() const throw(Exception);
134       const char *getBytesValue(std::size_t& len) const;
135     protected:
136       void putMyReprAtPlace(char *data) const;
137       static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
138       static void destroyReprAtPlace(char *data, const TypeCode *type);
139       static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
140       static bool takeInChargeStorageOf(TypeCode *type);
141     private:
142       ~AtomAny();
143       AtomAny(int val);
144       AtomAny(bool val);
145       AtomAny(double val);
146       AtomAny(const char *val);
147       AtomAny(const std::string& val);
148       AtomAny(const AtomAny& other);
149       AtomAny(char *data, TypeCode* type);
150       AtomAny(char *val, Deallocator deAlloc);
151     protected:
152       ValueContainer _value;
153     };
154     
155     class YACSLIBENGINE_EXPORT SeqAlloc 
156     {
157       friend class SequenceAny;
158       
159       char *_start;
160       char *_finish;
161       char *_endOfStorage;
162       Deallocator _notStdDeAlloc;
163       const unsigned int _sizeOf1Elm;
164     private:
165       SeqAlloc(const SeqAlloc& other);
166       SeqAlloc(unsigned int sizeOf1Elm);
167       ~SeqAlloc();
168       void clear();
169       void initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc);
170       void construct(char *pt, const Any *val);
171       void construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy);
172       char *allocate(unsigned int nbOfByte);
173       void destroy(char *pt, const TypeCode *tc);
174       void deallocate(char *pt);
175       unsigned int size() const;
176       std::vector<unsigned int> getSetItems() const;
177     public:
178       static const char DFT_CHAR_VAR;
179     };
180     
181     class YACSLIBENGINE_EXPORT ComposedAny : public Any
182     {
183     public:
184       virtual void setEltAtRank(int i, const Any *elem) throw(Exception) = 0;
185       AnyPtr operator[](const char *key) const throw(Exception);
186     protected:
187       ComposedAny(const ComposedAny& other);
188       ComposedAny(TypeCode* type, bool isNew=true);
189     protected:
190       void checkTypeOf(const Any *elem) const throw(Exception);
191     private://error methods called during incorrect runtime extraction
192       int getIntValue() const throw(Exception);
193       bool getBoolValue() const throw(Exception);
194       double getDoubleValue() const throw(Exception);
195       std::string getStringValue() const throw(Exception);
196     };
197
198     typedef SharedPtr<SequenceAny> SequenceAnyPtr;
199     
200     class YACSLIBENGINE_EXPORT SequenceAny : public ComposedAny
201     {
202       friend class TypeCodeSeq;
203     public:
204       void clear();
205       void popBack();
206       unsigned int size() const { return _alloc.size(); }
207       void pushBack(const Any *elem);
208       bool operator ==(const Any& other) const;
209       void setEltAtRank(int i, const Any *elem) throw(Exception);
210       AnyPtr operator[](int i) const throw(Exception);
211       Any *clone() const;
212       template<class T>
213       static SequenceAny *New(const std::vector<T>& vec);
214       static SequenceAny *New(const TypeCode *typeOfContent);
215       static SequenceAny *New(const TypeCode *typeOfContent, unsigned lgth);
216       template<class T>
217       static SequenceAny *New(T *val, unsigned int lgth, Deallocator deAlloc);
218       std::vector<unsigned int> getSetItems() const { return _alloc.getSetItems(); }
219       SequenceAny *removeUnsetItemsFromThis() const;
220     protected:
221       void putMyReprAtPlace(char *data) const;
222       static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
223       static void destroyReprAtPlace(char *data, const TypeCode *type);
224       static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
225       static bool takeInChargeStorageOf(TypeCode *type);
226     private:
227       ~SequenceAny();
228       SequenceAny(const SequenceAny& other);
229       SequenceAny(const TypeCode *typeOfContent);
230       SequenceAny(const TypeCode *typeOfContent, unsigned lgth);
231       SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc);
232       SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc);
233       SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc);
234       SequenceAny(const std::vector<int>& val);
235       SequenceAny(const std::vector<bool>& val);
236       SequenceAny(const std::vector<double>& val);
237       SequenceAny(const std::vector<std::string>& val);
238       void realloc(char *endOfCurrentAllocated, const Any *elem);
239       char *performCpy(char *srcStart, char *srcFinish, char *destStart);
240     protected:
241       SeqAlloc _alloc;
242     };
243     
244     typedef SharedPtr<ArrayAny> ArrayAnyPtr;
245
246     class YACSLIBENGINE_EXPORT ArrayAny : public ComposedAny
247     {
248       friend class TypeCodeArray;
249     public:
250       void setEltAtRank(int i, const Any *elem) throw(Exception);
251       bool operator ==(const Any& other) const;
252       AnyPtr operator[](int i) const throw(Exception);
253       unsigned int size() const;
254       Any *clone() const;
255       template<class T>
256       static ArrayAny *New(const std::vector<T>& vec);
257       template<class T>
258       static ArrayAny *New(const T *val, unsigned int lgth);
259       static ArrayAny *New(const TypeCode *typeOfContent, unsigned int lgth);
260     protected:
261       void putMyReprAtPlace(char *data) const;
262       static void putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy);
263       static void destroyReprAtPlace(char *data, const TypeCodeArray *type);
264       static AnyPtr getOrBuildFromData(char *data, const TypeCodeArray *type);
265       static bool takeInChargeStorageOf(TypeCode *type);
266     private:
267       ~ArrayAny();
268       ArrayAny(const TypeCode *typeOfContent, unsigned int lgth);
269       ArrayAny(char *data, TypeCodeArray * type);
270       ArrayAny(const ArrayAny& other);
271       ArrayAny(const int *val, unsigned int lgth);
272       ArrayAny(const bool *val, unsigned int lgth);
273       ArrayAny(const double *val, unsigned int lgth);
274       ArrayAny(const std::vector<int>& val);
275       ArrayAny(const std::vector<double>& val);
276       ArrayAny(const std::vector<std::string>& val);
277     protected:
278       char *_data;
279     };
280
281     typedef SharedPtr<StructAny> StructAnyPtr;
282
283     class YACSLIBENGINE_EXPORT StructAny : public ComposedAny
284     {
285       friend class TypeCodeStruct;
286     public:
287       Any *clone() const;
288       bool operator ==(const Any& other) const;
289       static StructAny *New(TypeCodeStruct *type);
290       AnyPtr operator[](int i) const throw(Exception);
291       AnyPtr operator[](const char *key) const throw(Exception);
292       void setEltAtRank(int i, const Any *elem) throw(Exception);
293       void setEltAtRank(const char *key, const Any *elem) throw(Exception);
294     protected:
295       void putMyReprAtPlace(char *data) const;
296       static void putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy);
297       static void destroyReprAtPlace(char *data, const TypeCodeStruct *type);
298       static AnyPtr getOrBuildFromData(char *data, const TypeCodeStruct *type);
299     private:
300       ~StructAny();
301       StructAny(TypeCodeStruct *type);
302       StructAny(const StructAny& other);
303       StructAny(char *data, TypeCodeStruct * type);
304     private:
305       char *_data;
306     };
307
308     template<class T>
309     SequenceAny *SequenceAny::New(T *val, unsigned int lgth, Deallocator deAlloc)
310     {
311       return new SequenceAny(val,lgth,deAlloc);
312     }
313
314     template<class T>
315     SequenceAny *SequenceAny::New(const std::vector<T>& vec)
316     {
317       return new SequenceAny(vec);
318     }
319
320     template<class T>
321     ArrayAny *ArrayAny::New(const std::vector<T>& vec)
322     {
323       return new ArrayAny(vec);
324     }
325
326     template<class T>
327     ArrayAny *ArrayAny::New(const T *val, unsigned int lgth)
328     {
329       return new ArrayAny(val,lgth);
330     }
331   }
332 }
333
334 #endif