Salome HOME
f78a39d482b3f2316ff3c7f191e8da425855a4c3
[modules/yacs.git] / src / engine / Any.hxx
1 #ifndef __YACSANY_HXX__
2 #define __YACSANY_HXX__
3
4 #include "RefCounter.hxx"
5 #include "Exception.hxx"
6 #include "SharedPtr.hxx"
7
8 #include <vector>
9
10 namespace YACS
11 {
12   namespace ENGINE
13   {
14     class Any;
15     class AtomAny;
16     class TypeCode;
17     class SeqAlloc;
18     class ArrayAny;
19     class StructAny;
20     class SequenceAny;
21     class TypeCodeArray;
22     class TypeCodeStruct;
23     typedef void (*Deallocator)(void *);
24
25     class StringOnHeap
26     {
27       friend class Any;
28       friend class AtomAny;
29       friend class ArrayAny;
30     private:
31       StringOnHeap(const char *val);
32       StringOnHeap(const std::string& val);
33       StringOnHeap(char *val, Deallocator deAlloc);
34       bool operator ==(const StringOnHeap& other) const;
35       StringOnHeap *deepCopy() const;
36       const char *cStr() const { return _str; }
37       ~StringOnHeap();
38     private:
39       char *_str;
40       Deallocator _dealloc;
41     };
42
43     typedef SharedPtr<Any> AnyPtr;
44     
45     /*!
46      * \brief: Interface for management of storage of data formated dynamically in its TypeCode.
47      *         Warning virtual inheritance on Any daughter classes NOT supported.
48      */
49     class Any : public RefCounter
50     {
51       friend class SeqAlloc;
52       friend class ArrayAny;
53       friend class StructAny;
54       friend class SequenceAny;
55     public:
56       const TypeCode *getType() const { return _type; }
57       //for convenience methods
58       virtual Any *clone() const = 0;
59       virtual AnyPtr operator[](int i) const throw(Exception) = 0;
60       virtual AnyPtr operator[](const char *key) const throw(Exception) = 0;
61       virtual bool operator ==(const Any& other) const = 0;
62       virtual int getIntValue() const throw(Exception) = 0;
63       virtual bool getBoolValue() const throw(Exception) = 0;
64       virtual double getDoubleValue() const throw(Exception) = 0;
65       virtual std::string getStringValue() const throw(Exception) = 0;
66       //
67     protected:
68       virtual ~Any();
69       Any(TypeCode* type);
70       Any(const Any& other);
71       virtual void putMyReprAtPlace(char *data) const = 0;
72       static AnyPtr buildAnyFromCoarseData(char *data, TypeCode* type); //Factory Method
73     protected:
74       TypeCode* _type;
75     };
76
77     typedef SharedPtr<AtomAny> AtomAnyPtr;
78
79     class AtomAny : public Any
80     {
81       friend class TypeCode;
82
83       union ValueContainer
84       {
85         int _i;
86         bool _b;
87         double _d;
88         StringOnHeap *_s;
89       };
90     public:
91       Any *clone() const;
92       template<class T>
93       static AtomAny *New(T val) { return new AtomAny(val); }
94       static AtomAny *New(char *val, Deallocator dealloc);
95       AnyPtr operator[](int i) const throw(Exception);
96       AnyPtr operator[](const char *key) const throw(Exception);
97       bool operator ==(const Any& other) const;
98       int getIntValue() const throw(Exception);
99       bool getBoolValue() const throw(Exception);
100       double getDoubleValue() const throw(Exception);
101       std::string getStringValue() const throw(Exception);
102     protected:
103       void putMyReprAtPlace(char *data) const;
104       static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
105       static void destroyReprAtPlace(char *data, const TypeCode *type);
106       static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
107       static bool takeInChargeStorageOf(TypeCode *type);
108     private:
109       ~AtomAny();
110       AtomAny(int val);
111       AtomAny(bool val);
112       AtomAny(double val);
113       AtomAny(const char *val);
114       AtomAny(const std::string& val);
115       AtomAny(const AtomAny& other);
116       AtomAny(char *data, TypeCode* type);
117       AtomAny(char *val, Deallocator deAlloc);
118     protected:
119       ValueContainer _value;
120     };
121     
122     class SeqAlloc 
123     {
124       friend class SequenceAny;
125       
126       char *_start;
127       char *_finish;
128       char *_endOfStorage;
129       Deallocator _notStdDeAlloc;
130       const unsigned int _sizeOf1Elm;
131     private:
132       SeqAlloc(const SeqAlloc& other);
133       SeqAlloc(unsigned int sizeOf1Elm);
134       ~SeqAlloc();
135       void clear();
136       void initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc);
137       void construct(char *pt, const Any *val);
138       void construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy);
139       char *allocate(unsigned int nbOfByte);
140       void destroy(char *pt, const TypeCode *tc);
141       void deallocate(char *pt);
142       unsigned int size() const;
143     };
144     
145     class ComposedAny : public Any
146     {
147     public:
148       virtual void setEltAtRank(int i, const Any *elem) throw(Exception) = 0;
149       AnyPtr operator[](const char *key) const throw(Exception);
150     protected:
151       ComposedAny(const ComposedAny& other);
152       ComposedAny(TypeCode* type, bool isNew=true);
153     protected:
154       void checkTypeOf(const Any *elem) const throw(Exception);
155     private://error methods called during incorrect runtime extraction
156       int getIntValue() const throw(Exception);
157       bool getBoolValue() const throw(Exception);
158       double getDoubleValue() const throw(Exception);
159       std::string getStringValue() const throw(Exception);
160     };
161
162     typedef SharedPtr<SequenceAny> SequenceAnyPtr;
163     
164     class SequenceAny : public ComposedAny
165     {
166       friend class TypeCodeSeq;
167     public:
168       void clear();
169       void popBack();
170       unsigned int size() const { return _alloc.size(); }
171       void pushBack(const Any *elem);
172       bool operator ==(const Any& other) const;
173       void setEltAtRank(int i, const Any *elem) throw(Exception);
174       AnyPtr operator[](int i) const throw(Exception);
175       Any *clone() const;
176       template<class T>
177       static SequenceAny *New(const std::vector<T>& vec);
178       static SequenceAny *New(const TypeCode *typeOfContent);
179       static SequenceAny *New(const TypeCode *typeOfContent, unsigned lgth);
180       template<class T>
181       static SequenceAny *New(T *val, unsigned int lgth, Deallocator deAlloc);
182     protected:
183       void putMyReprAtPlace(char *data) const;
184       static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
185       static void destroyReprAtPlace(char *data, const TypeCode *type);
186       static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
187       static bool takeInChargeStorageOf(TypeCode *type);
188     private:
189       ~SequenceAny();
190       SequenceAny(const SequenceAny& other);
191       SequenceAny(const TypeCode *typeOfContent);
192       SequenceAny(const TypeCode *typeOfContent, unsigned lgth);
193       SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc);
194       SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc);
195       SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc);
196       SequenceAny(const std::vector<int>& val);
197       SequenceAny(const std::vector<bool>& val);
198       SequenceAny(const std::vector<double>& val);
199       SequenceAny(const std::vector<std::string>& val);
200       void realloc(char *endOfCurrentAllocated, const Any *elem);
201       char *performCpy(char *srcStart, char *srcFinish, char *destStart);
202     protected:
203       SeqAlloc _alloc;
204     };
205     
206     typedef SharedPtr<ArrayAny> ArrayAnyPtr;
207
208     class ArrayAny : public ComposedAny
209     {
210       friend class TypeCodeArray;
211     public:
212       void setEltAtRank(int i, const Any *elem) throw(Exception);
213       bool operator ==(const Any& other) const;
214       AnyPtr operator[](int i) const throw(Exception);
215       unsigned int size() const;
216       Any *clone() const;
217       template<class T>
218       static ArrayAny *New(const std::vector<T>& vec);
219       template<class T>
220       static ArrayAny *New(const T *val, unsigned int lgth);
221       static ArrayAny *New(const TypeCode *typeOfContent, unsigned int lgth);
222     protected:
223       void putMyReprAtPlace(char *data) const;
224       static void putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy);
225       static void destroyReprAtPlace(char *data, const TypeCodeArray *type);
226       static AnyPtr getOrBuildFromData(char *data, const TypeCodeArray *type);
227       static bool takeInChargeStorageOf(TypeCode *type);
228     private:
229       ~ArrayAny();
230       ArrayAny(const TypeCode *typeOfContent, unsigned int lgth);
231       ArrayAny(char *data, TypeCodeArray * type);
232       ArrayAny(const ArrayAny& other);
233       ArrayAny(const int *val, unsigned int lgth);
234       ArrayAny(const bool *val, unsigned int lgth);
235       ArrayAny(const double *val, unsigned int lgth);
236       ArrayAny(const std::vector<int>& val);
237       ArrayAny(const std::vector<double>& val);
238       ArrayAny(const std::vector<std::string>& val);
239     protected:
240       char *_data;
241     };
242
243     typedef SharedPtr<StructAny> StructAnyPtr;
244
245     class StructAny : public ComposedAny
246     {
247       friend class TypeCodeStruct;
248     public:
249       Any *clone() const;
250       bool operator ==(const Any& other) const;
251       static StructAny *New(TypeCodeStruct *type);
252       AnyPtr operator[](int i) const throw(Exception);
253       AnyPtr operator[](const char *key) const throw(Exception);
254       void setEltAtRank(int i, const Any *elem) throw(Exception);
255       void setEltAtRank(const char *key, const Any *elem) throw(Exception);
256     protected:
257       void putMyReprAtPlace(char *data) const;
258       static void putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy);
259       static void destroyReprAtPlace(char *data, const TypeCodeStruct *type);
260       static AnyPtr getOrBuildFromData(char *data, const TypeCodeStruct *type);
261     private:
262       ~StructAny();
263       StructAny(TypeCodeStruct *type);
264       StructAny(const StructAny& other);
265       StructAny(char *data, TypeCodeStruct * type);
266     private:
267       char *_data;
268     };
269
270     template<class T>
271     SequenceAny *SequenceAny::New(T *val, unsigned int lgth, Deallocator deAlloc)
272     {
273       return new SequenceAny(val,lgth,deAlloc);
274     }
275
276     template<class T>
277     SequenceAny *SequenceAny::New(const std::vector<T>& vec)
278     {
279       return new SequenceAny(vec);
280     }
281
282     template<class T>
283     ArrayAny *ArrayAny::New(const std::vector<T>& vec)
284     {
285       return new ArrayAny(vec);
286     }
287
288     template<class T>
289     ArrayAny *ArrayAny::New(const T *val, unsigned int lgth)
290     {
291       return new ArrayAny(val,lgth);
292     }
293   }
294 }
295
296 #endif