1 // Copyright (C) 2006-2014 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 std::string& val);
53 StringOnHeap(char *val, Deallocator deAlloc);
54 bool operator ==(const StringOnHeap& other) const;
55 StringOnHeap *deepCopy() const;
56 const char *cStr() const { return _str; }
63 typedef SharedPtr<Any> AnyPtr;
66 * \brief: Interface for management of storage of data formated dynamically in its TypeCode.
67 * Warning virtual inheritance on Any daughter classes NOT supported.
69 class YACSLIBENGINE_EXPORT Any : public RefCounter
71 friend class SeqAlloc;
72 friend class ArrayAny;
73 friend class StructAny;
74 friend class SequenceAny;
76 const TypeCode *getType() const { return _type; }
77 //for convenience methods
78 virtual Any *clone() const = 0;
79 virtual AnyPtr operator[](int i) const throw(Exception) = 0;
80 virtual AnyPtr operator[](const char *key) const throw(Exception) = 0;
81 virtual bool operator ==(const Any& other) const = 0;
82 virtual int getIntValue() const throw(Exception) = 0;
83 virtual bool getBoolValue() const throw(Exception) = 0;
84 virtual double getDoubleValue() const throw(Exception) = 0;
85 virtual std::string getStringValue() const throw(Exception) = 0;
92 Any(const Any& other);
93 virtual void putMyReprAtPlace(char *data) const = 0;
94 static AnyPtr buildAnyFromCoarseData(char *data, TypeCode* type); //Factory Method
99 typedef SharedPtr<AtomAny> AtomAnyPtr;
101 class YACSLIBENGINE_EXPORT AtomAny : public Any
103 friend class TypeCode;
115 static AtomAny *New(T val) { return new AtomAny(val); }
116 static AtomAny *New(char *val, Deallocator dealloc);
117 AnyPtr operator[](int i) const throw(Exception);
118 AnyPtr operator[](const char *key) const throw(Exception);
119 bool operator ==(const Any& other) const;
120 int getIntValue() const throw(Exception);
121 bool getBoolValue() const throw(Exception);
122 double getDoubleValue() const throw(Exception);
123 std::string getStringValue() const throw(Exception);
125 void putMyReprAtPlace(char *data) const;
126 static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
127 static void destroyReprAtPlace(char *data, const TypeCode *type);
128 static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
129 static bool takeInChargeStorageOf(TypeCode *type);
135 AtomAny(const char *val);
136 AtomAny(const std::string& val);
137 AtomAny(const AtomAny& other);
138 AtomAny(char *data, TypeCode* type);
139 AtomAny(char *val, Deallocator deAlloc);
141 ValueContainer _value;
144 class YACSLIBENGINE_EXPORT SeqAlloc
146 friend class SequenceAny;
151 Deallocator _notStdDeAlloc;
152 const unsigned int _sizeOf1Elm;
154 SeqAlloc(const SeqAlloc& other);
155 SeqAlloc(unsigned int sizeOf1Elm);
158 void initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc);
159 void construct(char *pt, const Any *val);
160 void construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy);
161 char *allocate(unsigned int nbOfByte);
162 void destroy(char *pt, const TypeCode *tc);
163 void deallocate(char *pt);
164 unsigned int size() const;
165 std::vector<unsigned int> getSetItems() const;
166 static const char DFT_CHAR_VAR;
169 class YACSLIBENGINE_EXPORT ComposedAny : public Any
172 virtual void setEltAtRank(int i, const Any *elem) throw(Exception) = 0;
173 AnyPtr operator[](const char *key) const throw(Exception);
175 ComposedAny(const ComposedAny& other);
176 ComposedAny(TypeCode* type, bool isNew=true);
178 void checkTypeOf(const Any *elem) const throw(Exception);
179 private://error methods called during incorrect runtime extraction
180 int getIntValue() const throw(Exception);
181 bool getBoolValue() const throw(Exception);
182 double getDoubleValue() const throw(Exception);
183 std::string getStringValue() const throw(Exception);
186 typedef SharedPtr<SequenceAny> SequenceAnyPtr;
188 class YACSLIBENGINE_EXPORT SequenceAny : public ComposedAny
190 friend class TypeCodeSeq;
194 unsigned int size() const { return _alloc.size(); }
195 void pushBack(const Any *elem);
196 bool operator ==(const Any& other) const;
197 void setEltAtRank(int i, const Any *elem) throw(Exception);
198 AnyPtr operator[](int i) const throw(Exception);
201 static SequenceAny *New(const std::vector<T>& vec);
202 static SequenceAny *New(const TypeCode *typeOfContent);
203 static SequenceAny *New(const TypeCode *typeOfContent, unsigned lgth);
205 static SequenceAny *New(T *val, unsigned int lgth, Deallocator deAlloc);
206 std::vector<unsigned int> getSetItems() const { return _alloc.getSetItems(); }
207 SequenceAny *removeUnsetItemsFromThis() const;
209 void putMyReprAtPlace(char *data) const;
210 static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
211 static void destroyReprAtPlace(char *data, const TypeCode *type);
212 static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
213 static bool takeInChargeStorageOf(TypeCode *type);
216 SequenceAny(const SequenceAny& other);
217 SequenceAny(const TypeCode *typeOfContent);
218 SequenceAny(const TypeCode *typeOfContent, unsigned lgth);
219 SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc);
220 SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc);
221 SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc);
222 SequenceAny(const std::vector<int>& val);
223 SequenceAny(const std::vector<bool>& val);
224 SequenceAny(const std::vector<double>& val);
225 SequenceAny(const std::vector<std::string>& val);
226 void realloc(char *endOfCurrentAllocated, const Any *elem);
227 char *performCpy(char *srcStart, char *srcFinish, char *destStart);
232 typedef SharedPtr<ArrayAny> ArrayAnyPtr;
234 class YACSLIBENGINE_EXPORT ArrayAny : public ComposedAny
236 friend class TypeCodeArray;
238 void setEltAtRank(int i, const Any *elem) throw(Exception);
239 bool operator ==(const Any& other) const;
240 AnyPtr operator[](int i) const throw(Exception);
241 unsigned int size() const;
244 static ArrayAny *New(const std::vector<T>& vec);
246 static ArrayAny *New(const T *val, unsigned int lgth);
247 static ArrayAny *New(const TypeCode *typeOfContent, unsigned int lgth);
249 void putMyReprAtPlace(char *data) const;
250 static void putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy);
251 static void destroyReprAtPlace(char *data, const TypeCodeArray *type);
252 static AnyPtr getOrBuildFromData(char *data, const TypeCodeArray *type);
253 static bool takeInChargeStorageOf(TypeCode *type);
256 ArrayAny(const TypeCode *typeOfContent, unsigned int lgth);
257 ArrayAny(char *data, TypeCodeArray * type);
258 ArrayAny(const ArrayAny& other);
259 ArrayAny(const int *val, unsigned int lgth);
260 ArrayAny(const bool *val, unsigned int lgth);
261 ArrayAny(const double *val, unsigned int lgth);
262 ArrayAny(const std::vector<int>& val);
263 ArrayAny(const std::vector<double>& val);
264 ArrayAny(const std::vector<std::string>& val);
269 typedef SharedPtr<StructAny> StructAnyPtr;
271 class YACSLIBENGINE_EXPORT StructAny : public ComposedAny
273 friend class TypeCodeStruct;
276 bool operator ==(const Any& other) const;
277 static StructAny *New(TypeCodeStruct *type);
278 AnyPtr operator[](int i) const throw(Exception);
279 AnyPtr operator[](const char *key) const throw(Exception);
280 void setEltAtRank(int i, const Any *elem) throw(Exception);
281 void setEltAtRank(const char *key, const Any *elem) throw(Exception);
283 void putMyReprAtPlace(char *data) const;
284 static void putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy);
285 static void destroyReprAtPlace(char *data, const TypeCodeStruct *type);
286 static AnyPtr getOrBuildFromData(char *data, const TypeCodeStruct *type);
289 StructAny(TypeCodeStruct *type);
290 StructAny(const StructAny& other);
291 StructAny(char *data, TypeCodeStruct * type);
297 SequenceAny *SequenceAny::New(T *val, unsigned int lgth, Deallocator deAlloc)
299 return new SequenceAny(val,lgth,deAlloc);
303 SequenceAny *SequenceAny::New(const std::vector<T>& vec)
305 return new SequenceAny(vec);
309 ArrayAny *ArrayAny::New(const std::vector<T>& vec)
311 return new ArrayAny(vec);
315 ArrayAny *ArrayAny::New(const T *val, unsigned int lgth)
317 return new ArrayAny(val,lgth);