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