1 #ifndef __YACSANY_HXX__
2 #define __YACSANY_HXX__
4 #include "RefCounter.hxx"
5 #include "Exception.hxx"
6 #include "SharedPtr.hxx"
23 typedef void (*Deallocator)(void *);
29 friend class ArrayAny;
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; }
43 typedef SharedPtr<Any> AnyPtr;
46 * \brief: Interface for management of storage of data formated dynamically in its TypeCode.
47 * Warning virtual inheritance on Any daughter classes NOT supported.
49 class Any : public RefCounter
51 friend class SeqAlloc;
52 friend class ArrayAny;
53 friend class StructAny;
54 friend class SequenceAny;
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;
70 Any(const Any& other);
71 virtual void putMyReprAtPlace(char *data) const = 0;
72 static AnyPtr buildAnyFromCoarseData(char *data, TypeCode* type); //Factory Method
77 typedef SharedPtr<AtomAny> AtomAnyPtr;
79 class AtomAny : public Any
81 friend class TypeCode;
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);
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);
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);
119 ValueContainer _value;
124 friend class SequenceAny;
129 Deallocator _notStdDeAlloc;
130 const unsigned int _sizeOf1Elm;
132 SeqAlloc(const SeqAlloc& other);
133 SeqAlloc(unsigned int sizeOf1Elm);
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;
145 class ComposedAny : public Any
148 virtual void setEltAtRank(int i, const Any *elem) throw(Exception) = 0;
149 AnyPtr operator[](const char *key) const throw(Exception);
151 ComposedAny(const ComposedAny& other);
152 ComposedAny(TypeCode* type, bool isNew=true);
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);
162 typedef SharedPtr<SequenceAny> SequenceAnyPtr;
164 class SequenceAny : public ComposedAny
166 friend class TypeCodeSeq;
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);
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);
181 static SequenceAny *New(T *val, unsigned int lgth, Deallocator deAlloc);
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);
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);
206 typedef SharedPtr<ArrayAny> ArrayAnyPtr;
208 class ArrayAny : public ComposedAny
210 friend class TypeCodeArray;
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;
218 static ArrayAny *New(const std::vector<T>& vec);
220 static ArrayAny *New(const T *val, unsigned int lgth);
221 static ArrayAny *New(const TypeCode *typeOfContent, unsigned int lgth);
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);
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);
243 typedef SharedPtr<StructAny> StructAnyPtr;
245 class StructAny : public ComposedAny
247 friend class TypeCodeStruct;
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);
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);
263 StructAny(TypeCodeStruct *type);
264 StructAny(const StructAny& other);
265 StructAny(char *data, TypeCodeStruct * type);
271 SequenceAny *SequenceAny::New(T *val, unsigned int lgth, Deallocator deAlloc)
273 return new SequenceAny(val,lgth,deAlloc);
277 SequenceAny *SequenceAny::New(const std::vector<T>& vec)
279 return new SequenceAny(vec);
283 ArrayAny *ArrayAny::New(const std::vector<T>& vec)
285 return new ArrayAny(vec);
289 ArrayAny *ArrayAny::New(const T *val, unsigned int lgth)
291 return new ArrayAny(val,lgth);