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