1 // Copyright (C) 2006-2019 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 *);
46 YACSLIBENGINE_EXPORT std::string ToBase64(const std::string& bytes);
47 YACSLIBENGINE_EXPORT std::string FromBase64(const std::string& bytes);
50 class YACSLIBENGINE_EXPORT StringOnHeap
54 friend class ArrayAny;
56 StringOnHeap(const char *val);
57 StringOnHeap(const char *val, std::size_t len);
58 StringOnHeap(const std::string& val);
59 StringOnHeap(char *val, Deallocator deAlloc);
60 bool operator ==(const StringOnHeap& other) const;
61 StringOnHeap *deepCopy() const;
62 const char *cStr() const { return _str; }
63 std::size_t size() const { return _len; }
71 typedef SharedPtr<Any> AnyPtr;
74 * \brief: Interface for management of storage of data formated dynamically in its TypeCode.
75 * Warning virtual inheritance on Any daughter classes NOT supported.
77 class YACSLIBENGINE_EXPORT Any : public RefCounter
79 friend class SeqAlloc;
80 friend class ArrayAny;
81 friend class StructAny;
82 friend class SequenceAny;
84 const TypeCode *getType() const { return _type; }
85 //for convenience methods
86 virtual Any *clone() const = 0;
87 virtual AnyPtr operator[](int i) const throw(Exception) = 0;
88 virtual AnyPtr operator[](const char *key) const throw(Exception) = 0;
89 virtual bool operator ==(const Any& other) const = 0;
90 virtual int getIntValue() const throw(Exception) = 0;
91 virtual bool getBoolValue() const throw(Exception) = 0;
92 virtual double getDoubleValue() const throw(Exception) = 0;
93 virtual std::string getStringValue() const throw(Exception) = 0;
100 Any(const Any& other);
101 virtual void putMyReprAtPlace(char *data) const = 0;
102 //static AnyPtr buildAnyFromCoarseData(char *data, TypeCode* type); //Factory Method
103 static bool IsNull(char *data);
108 typedef SharedPtr<AtomAny> AtomAnyPtr;
110 class YACSLIBENGINE_EXPORT AtomAny : public Any
112 friend class TypeCode;
124 static AtomAny *New(T val) { return new AtomAny(val); }
125 static AtomAny *New(char *val, Deallocator dealloc);
126 AnyPtr operator[](int i) const throw(Exception);
127 AnyPtr operator[](const char *key) const throw(Exception);
128 bool operator ==(const Any& other) const;
129 int getIntValue() const throw(Exception);
130 bool getBoolValue() const throw(Exception);
131 double getDoubleValue() const throw(Exception);
132 std::string getStringValue() const throw(Exception);
133 const char *getBytesValue(std::size_t& len) const;
135 void putMyReprAtPlace(char *data) const;
136 static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
137 static void destroyReprAtPlace(char *data, const TypeCode *type);
138 static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
139 static bool takeInChargeStorageOf(TypeCode *type);
145 AtomAny(const char *val);
146 AtomAny(const std::string& val);
147 AtomAny(const AtomAny& other);
148 AtomAny(char *data, TypeCode* type);
149 AtomAny(char *val, Deallocator deAlloc);
151 ValueContainer _value;
154 class YACSLIBENGINE_EXPORT SeqAlloc
156 friend class SequenceAny;
161 Deallocator _notStdDeAlloc;
162 const unsigned int _sizeOf1Elm;
164 SeqAlloc(const SeqAlloc& other);
165 SeqAlloc(unsigned int sizeOf1Elm);
168 void initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc);
169 void construct(char *pt, const Any *val);
170 void construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy);
171 char *allocate(unsigned int nbOfByte);
172 void destroy(char *pt, const TypeCode *tc);
173 void deallocate(char *pt);
174 unsigned int size() const;
175 std::vector<unsigned int> getSetItems() const;
177 static const char DFT_CHAR_VAR;
180 class YACSLIBENGINE_EXPORT ComposedAny : public Any
183 virtual void setEltAtRank(int i, const Any *elem) throw(Exception) = 0;
184 AnyPtr operator[](const char *key) const throw(Exception);
186 ComposedAny(const ComposedAny& other);
187 ComposedAny(TypeCode* type, bool isNew=true);
189 void checkTypeOf(const Any *elem) const throw(Exception);
190 private://error methods called during incorrect runtime extraction
191 int getIntValue() const throw(Exception);
192 bool getBoolValue() const throw(Exception);
193 double getDoubleValue() const throw(Exception);
194 std::string getStringValue() const throw(Exception);
197 typedef SharedPtr<SequenceAny> SequenceAnyPtr;
199 class YACSLIBENGINE_EXPORT SequenceAny : public ComposedAny
201 friend class TypeCodeSeq;
205 unsigned int size() const { return _alloc.size(); }
206 void pushBack(const Any *elem);
207 bool operator ==(const Any& other) const;
208 void setEltAtRank(int i, const Any *elem) throw(Exception);
209 AnyPtr operator[](int i) const throw(Exception);
212 static SequenceAny *New(const std::vector<T>& vec);
213 static SequenceAny *New(const TypeCode *typeOfContent);
214 static SequenceAny *New(const TypeCode *typeOfContent, unsigned lgth);
216 static SequenceAny *New(T *val, unsigned int lgth, Deallocator deAlloc);
217 std::vector<unsigned int> getSetItems() const { return _alloc.getSetItems(); }
218 SequenceAny *removeUnsetItemsFromThis() const;
220 void putMyReprAtPlace(char *data) const;
221 static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
222 static void destroyReprAtPlace(char *data, const TypeCode *type);
223 static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
224 static bool takeInChargeStorageOf(TypeCode *type);
227 SequenceAny(const SequenceAny& other);
228 SequenceAny(const TypeCode *typeOfContent);
229 SequenceAny(const TypeCode *typeOfContent, unsigned lgth);
230 SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc);
231 SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc);
232 SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc);
233 SequenceAny(const std::vector<int>& val);
234 SequenceAny(const std::vector<bool>& val);
235 SequenceAny(const std::vector<double>& val);
236 SequenceAny(const std::vector<std::string>& val);
237 void realloc(char *endOfCurrentAllocated, const Any *elem);
238 char *performCpy(char *srcStart, char *srcFinish, char *destStart);
243 typedef SharedPtr<ArrayAny> ArrayAnyPtr;
245 class YACSLIBENGINE_EXPORT ArrayAny : public ComposedAny
247 friend class TypeCodeArray;
249 void setEltAtRank(int i, const Any *elem) throw(Exception);
250 bool operator ==(const Any& other) const;
251 AnyPtr operator[](int i) const throw(Exception);
252 unsigned int size() const;
255 static ArrayAny *New(const std::vector<T>& vec);
257 static ArrayAny *New(const T *val, unsigned int lgth);
258 static ArrayAny *New(const TypeCode *typeOfContent, unsigned int lgth);
260 void putMyReprAtPlace(char *data) const;
261 static void putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy);
262 static void destroyReprAtPlace(char *data, const TypeCodeArray *type);
263 static AnyPtr getOrBuildFromData(char *data, const TypeCodeArray *type);
264 static bool takeInChargeStorageOf(TypeCode *type);
267 ArrayAny(const TypeCode *typeOfContent, unsigned int lgth);
268 ArrayAny(char *data, TypeCodeArray * type);
269 ArrayAny(const ArrayAny& other);
270 ArrayAny(const int *val, unsigned int lgth);
271 ArrayAny(const bool *val, unsigned int lgth);
272 ArrayAny(const double *val, unsigned int lgth);
273 ArrayAny(const std::vector<int>& val);
274 ArrayAny(const std::vector<double>& val);
275 ArrayAny(const std::vector<std::string>& val);
280 typedef SharedPtr<StructAny> StructAnyPtr;
282 class YACSLIBENGINE_EXPORT StructAny : public ComposedAny
284 friend class TypeCodeStruct;
287 bool operator ==(const Any& other) const;
288 static StructAny *New(TypeCodeStruct *type);
289 AnyPtr operator[](int i) const throw(Exception);
290 AnyPtr operator[](const char *key) const throw(Exception);
291 void setEltAtRank(int i, const Any *elem) throw(Exception);
292 void setEltAtRank(const char *key, const Any *elem) throw(Exception);
294 void putMyReprAtPlace(char *data) const;
295 static void putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy);
296 static void destroyReprAtPlace(char *data, const TypeCodeStruct *type);
297 static AnyPtr getOrBuildFromData(char *data, const TypeCodeStruct *type);
300 StructAny(TypeCodeStruct *type);
301 StructAny(const StructAny& other);
302 StructAny(char *data, TypeCodeStruct * type);
308 SequenceAny *SequenceAny::New(T *val, unsigned int lgth, Deallocator deAlloc)
310 return new SequenceAny(val,lgth,deAlloc);
314 SequenceAny *SequenceAny::New(const std::vector<T>& vec)
316 return new SequenceAny(vec);
320 ArrayAny *ArrayAny::New(const std::vector<T>& vec)
322 return new ArrayAny(vec);
326 ArrayAny *ArrayAny::New(const T *val, unsigned int lgth)
328 return new ArrayAny(val,lgth);