1 // Copyright (C) 2006-2016 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, or (at your option) any later version.
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
20 #ifndef __YACSANY_HXX__
21 #define __YACSANY_HXX__
23 #include "YACSlibEngineExport.hxx"
24 #include "RefCounter.hxx"
25 #include "Exception.hxx"
26 #include "SharedPtr.hxx"
43 typedef void (*Deallocator)(void *);
45 class YACSLIBENGINE_EXPORT StringOnHeap
49 friend class ArrayAny;
51 StringOnHeap(const char *val);
52 StringOnHeap(const char *val, std::size_t len);
53 StringOnHeap(const std::string& val);
54 StringOnHeap(char *val, Deallocator deAlloc);
55 bool operator ==(const StringOnHeap& other) const;
56 StringOnHeap *deepCopy() const;
57 const char *cStr() const { return _str; }
58 std::size_t size() const { return _len; }
66 typedef SharedPtr<Any> AnyPtr;
69 * \brief: Interface for management of storage of data formated dynamically in its TypeCode.
70 * Warning virtual inheritance on Any daughter classes NOT supported.
72 class YACSLIBENGINE_EXPORT Any : public RefCounter
74 friend class SeqAlloc;
75 friend class ArrayAny;
76 friend class StructAny;
77 friend class SequenceAny;
79 const TypeCode *getType() const { return _type; }
80 //for convenience methods
81 virtual Any *clone() const = 0;
82 virtual AnyPtr operator[](int i) const throw(Exception) = 0;
83 virtual AnyPtr operator[](const char *key) const throw(Exception) = 0;
84 virtual bool operator ==(const Any& other) const = 0;
85 virtual int getIntValue() const throw(Exception) = 0;
86 virtual bool getBoolValue() const throw(Exception) = 0;
87 virtual double getDoubleValue() const throw(Exception) = 0;
88 virtual std::string getStringValue() const throw(Exception) = 0;
95 Any(const Any& other);
96 virtual void putMyReprAtPlace(char *data) const = 0;
97 //static AnyPtr buildAnyFromCoarseData(char *data, TypeCode* type); //Factory Method
98 static bool IsNull(char *data);
103 typedef SharedPtr<AtomAny> AtomAnyPtr;
105 class YACSLIBENGINE_EXPORT AtomAny : public Any
107 friend class TypeCode;
119 static AtomAny *New(T val) { return new AtomAny(val); }
120 static AtomAny *New(char *val, Deallocator dealloc);
121 AnyPtr operator[](int i) const throw(Exception);
122 AnyPtr operator[](const char *key) const throw(Exception);
123 bool operator ==(const Any& other) const;
124 int getIntValue() const throw(Exception);
125 bool getBoolValue() const throw(Exception);
126 double getDoubleValue() const throw(Exception);
127 std::string getStringValue() const throw(Exception);
128 const char *getBytesValue(std::size_t& len) const;
130 void putMyReprAtPlace(char *data) const;
131 static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
132 static void destroyReprAtPlace(char *data, const TypeCode *type);
133 static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
134 static bool takeInChargeStorageOf(TypeCode *type);
140 AtomAny(const char *val);
141 AtomAny(const std::string& val);
142 AtomAny(const AtomAny& other);
143 AtomAny(char *data, TypeCode* type);
144 AtomAny(char *val, Deallocator deAlloc);
146 ValueContainer _value;
149 class YACSLIBENGINE_EXPORT SeqAlloc
151 friend class SequenceAny;
156 Deallocator _notStdDeAlloc;
157 const unsigned int _sizeOf1Elm;
159 SeqAlloc(const SeqAlloc& other);
160 SeqAlloc(unsigned int sizeOf1Elm);
163 void initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc);
164 void construct(char *pt, const Any *val);
165 void construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy);
166 char *allocate(unsigned int nbOfByte);
167 void destroy(char *pt, const TypeCode *tc);
168 void deallocate(char *pt);
169 unsigned int size() const;
170 std::vector<unsigned int> getSetItems() const;
172 static const char DFT_CHAR_VAR;
175 class YACSLIBENGINE_EXPORT ComposedAny : public Any
178 virtual void setEltAtRank(int i, const Any *elem) throw(Exception) = 0;
179 AnyPtr operator[](const char *key) const throw(Exception);
181 ComposedAny(const ComposedAny& other);
182 ComposedAny(TypeCode* type, bool isNew=true);
184 void checkTypeOf(const Any *elem) const throw(Exception);
185 private://error methods called during incorrect runtime extraction
186 int getIntValue() const throw(Exception);
187 bool getBoolValue() const throw(Exception);
188 double getDoubleValue() const throw(Exception);
189 std::string getStringValue() const throw(Exception);
192 typedef SharedPtr<SequenceAny> SequenceAnyPtr;
194 class YACSLIBENGINE_EXPORT SequenceAny : public ComposedAny
196 friend class TypeCodeSeq;
200 unsigned int size() const { return _alloc.size(); }
201 void pushBack(const Any *elem);
202 bool operator ==(const Any& other) const;
203 void setEltAtRank(int i, const Any *elem) throw(Exception);
204 AnyPtr operator[](int i) const throw(Exception);
207 static SequenceAny *New(const std::vector<T>& vec);
208 static SequenceAny *New(const TypeCode *typeOfContent);
209 static SequenceAny *New(const TypeCode *typeOfContent, unsigned lgth);
211 static SequenceAny *New(T *val, unsigned int lgth, Deallocator deAlloc);
212 std::vector<unsigned int> getSetItems() const { return _alloc.getSetItems(); }
213 SequenceAny *removeUnsetItemsFromThis() const;
215 void putMyReprAtPlace(char *data) const;
216 static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
217 static void destroyReprAtPlace(char *data, const TypeCode *type);
218 static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
219 static bool takeInChargeStorageOf(TypeCode *type);
222 SequenceAny(const SequenceAny& other);
223 SequenceAny(const TypeCode *typeOfContent);
224 SequenceAny(const TypeCode *typeOfContent, unsigned lgth);
225 SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc);
226 SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc);
227 SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc);
228 SequenceAny(const std::vector<int>& val);
229 SequenceAny(const std::vector<bool>& val);
230 SequenceAny(const std::vector<double>& val);
231 SequenceAny(const std::vector<std::string>& val);
232 void realloc(char *endOfCurrentAllocated, const Any *elem);
233 char *performCpy(char *srcStart, char *srcFinish, char *destStart);
238 typedef SharedPtr<ArrayAny> ArrayAnyPtr;
240 class YACSLIBENGINE_EXPORT ArrayAny : public ComposedAny
242 friend class TypeCodeArray;
244 void setEltAtRank(int i, const Any *elem) throw(Exception);
245 bool operator ==(const Any& other) const;
246 AnyPtr operator[](int i) const throw(Exception);
247 unsigned int size() const;
250 static ArrayAny *New(const std::vector<T>& vec);
252 static ArrayAny *New(const T *val, unsigned int lgth);
253 static ArrayAny *New(const TypeCode *typeOfContent, unsigned int lgth);
255 void putMyReprAtPlace(char *data) const;
256 static void putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy);
257 static void destroyReprAtPlace(char *data, const TypeCodeArray *type);
258 static AnyPtr getOrBuildFromData(char *data, const TypeCodeArray *type);
259 static bool takeInChargeStorageOf(TypeCode *type);
262 ArrayAny(const TypeCode *typeOfContent, unsigned int lgth);
263 ArrayAny(char *data, TypeCodeArray * type);
264 ArrayAny(const ArrayAny& other);
265 ArrayAny(const int *val, unsigned int lgth);
266 ArrayAny(const bool *val, unsigned int lgth);
267 ArrayAny(const double *val, unsigned int lgth);
268 ArrayAny(const std::vector<int>& val);
269 ArrayAny(const std::vector<double>& val);
270 ArrayAny(const std::vector<std::string>& val);
275 typedef SharedPtr<StructAny> StructAnyPtr;
277 class YACSLIBENGINE_EXPORT StructAny : public ComposedAny
279 friend class TypeCodeStruct;
282 bool operator ==(const Any& other) const;
283 static StructAny *New(TypeCodeStruct *type);
284 AnyPtr operator[](int i) const throw(Exception);
285 AnyPtr operator[](const char *key) const throw(Exception);
286 void setEltAtRank(int i, const Any *elem) throw(Exception);
287 void setEltAtRank(const char *key, const Any *elem) throw(Exception);
289 void putMyReprAtPlace(char *data) const;
290 static void putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy);
291 static void destroyReprAtPlace(char *data, const TypeCodeStruct *type);
292 static AnyPtr getOrBuildFromData(char *data, const TypeCodeStruct *type);
295 StructAny(TypeCodeStruct *type);
296 StructAny(const StructAny& other);
297 StructAny(char *data, TypeCodeStruct * type);
303 SequenceAny *SequenceAny::New(T *val, unsigned int lgth, Deallocator deAlloc)
305 return new SequenceAny(val,lgth,deAlloc);
309 SequenceAny *SequenceAny::New(const std::vector<T>& vec)
311 return new SequenceAny(vec);
315 ArrayAny *ArrayAny::New(const std::vector<T>& vec)
317 return new ArrayAny(vec);
321 ArrayAny *ArrayAny::New(const T *val, unsigned int lgth)
323 return new ArrayAny(val,lgth);