1 // Copyright (C) 2006-2008 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #ifndef __YACSANY_HXX__
20 #define __YACSANY_HXX__
22 #include "RefCounter.hxx"
23 #include "Exception.hxx"
24 #include "SharedPtr.hxx"
41 typedef void (*Deallocator)(void *);
47 friend class ArrayAny;
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; }
61 typedef SharedPtr<Any> AnyPtr;
64 * \brief: Interface for management of storage of data formated dynamically in its TypeCode.
65 * Warning virtual inheritance on Any daughter classes NOT supported.
67 class Any : public RefCounter
69 friend class SeqAlloc;
70 friend class ArrayAny;
71 friend class StructAny;
72 friend class SequenceAny;
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;
88 Any(const Any& other);
89 virtual void putMyReprAtPlace(char *data) const = 0;
90 static AnyPtr buildAnyFromCoarseData(char *data, TypeCode* type); //Factory Method
95 typedef SharedPtr<AtomAny> AtomAnyPtr;
97 class AtomAny : public Any
99 friend class TypeCode;
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);
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);
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);
137 ValueContainer _value;
142 friend class SequenceAny;
147 Deallocator _notStdDeAlloc;
148 const unsigned int _sizeOf1Elm;
150 SeqAlloc(const SeqAlloc& other);
151 SeqAlloc(unsigned int sizeOf1Elm);
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;
163 class ComposedAny : public Any
166 virtual void setEltAtRank(int i, const Any *elem) throw(Exception) = 0;
167 AnyPtr operator[](const char *key) const throw(Exception);
169 ComposedAny(const ComposedAny& other);
170 ComposedAny(TypeCode* type, bool isNew=true);
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);
180 typedef SharedPtr<SequenceAny> SequenceAnyPtr;
182 class SequenceAny : public ComposedAny
184 friend class TypeCodeSeq;
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);
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);
199 static SequenceAny *New(T *val, unsigned int lgth, Deallocator deAlloc);
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);
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);
224 typedef SharedPtr<ArrayAny> ArrayAnyPtr;
226 class ArrayAny : public ComposedAny
228 friend class TypeCodeArray;
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;
236 static ArrayAny *New(const std::vector<T>& vec);
238 static ArrayAny *New(const T *val, unsigned int lgth);
239 static ArrayAny *New(const TypeCode *typeOfContent, unsigned int lgth);
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);
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);
261 typedef SharedPtr<StructAny> StructAnyPtr;
263 class StructAny : public ComposedAny
265 friend class TypeCodeStruct;
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);
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);
281 StructAny(TypeCodeStruct *type);
282 StructAny(const StructAny& other);
283 StructAny(char *data, TypeCodeStruct * type);
289 SequenceAny *SequenceAny::New(T *val, unsigned int lgth, Deallocator deAlloc)
291 return new SequenceAny(val,lgth,deAlloc);
295 SequenceAny *SequenceAny::New(const std::vector<T>& vec)
297 return new SequenceAny(vec);
301 ArrayAny *ArrayAny::New(const std::vector<T>& vec)
303 return new ArrayAny(vec);
307 ArrayAny *ArrayAny::New(const T *val, unsigned int lgth)
309 return new ArrayAny(val,lgth);