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