1 #ifndef __YACSANY_HXX__
2 #define __YACSANY_HXX__
4 #include "RefCounter.hxx"
5 #include "Exception.hxx"
6 #include "SharedPtr.hxx"
21 typedef void (*Deallocator)(void *);
27 friend class ArrayAny;
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; }
41 typedef SharedPtr<Any> AnyPtr;
44 * \brief: Interface for management of storage of data formated dynamically in its TypeCode.
45 * Warning virtual inheritance on Any daughter classes NOT supported.
47 class Any : public RefCounter
49 friend class SeqAlloc;
50 friend class SequenceAny;
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;
65 Any(const Any& other);
66 virtual void putMyReprAtPlace(char *data) const = 0;
67 static AnyPtr buildAnyFromCoarseData(char *data, TypeCode* type); //Factory Method
72 typedef SharedPtr<AtomAny> AtomAnyPtr;
74 class AtomAny : public Any
76 friend class TypeCode;
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);
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);
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);
113 ValueContainer _value;
118 friend class SequenceAny;
123 Deallocator _notStdDeAlloc;
124 const unsigned int _sizeOf1Elm;
126 SeqAlloc(const SeqAlloc& other);
127 SeqAlloc(unsigned int sizeOf1Elm);
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;
139 class ComposedAny : public Any
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);
151 typedef SharedPtr<SequenceAny> SequenceAnyPtr;
153 class SequenceAny : public ComposedAny
155 friend class TypeCodeSeq;
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;
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);
170 static SequenceAny *New(T *val, unsigned int lgth, Deallocator deAlloc);
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);
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);
195 typedef SharedPtr<ArrayAny> ArrayAnyPtr;
197 class ArrayAny : public ComposedAny
199 friend class TypeCodeArray;
201 bool operator ==(const Any& other) const;
202 AnyPtr operator[](int i) const;
205 static ArrayAny *New(const std::vector<T>& vec);
207 static ArrayAny *New(const T *val, unsigned int lgth);
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);
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);
229 SequenceAny *SequenceAny::New(T *val, unsigned int lgth, Deallocator deAlloc)
231 return new SequenceAny(val,lgth,deAlloc);
235 SequenceAny *SequenceAny::New(const std::vector<T>& vec)
237 return new SequenceAny(vec);
241 ArrayAny *ArrayAny::New(const std::vector<T>& vec)
243 return new ArrayAny(vec);
247 ArrayAny *ArrayAny::New(const T *val, unsigned int lgth)
249 return new ArrayAny(val,lgth);