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);
48 YACSLIBENGINE_EXPORT std::string FromBase64Safe(const std::string& bytes);
51 class YACSLIBENGINE_EXPORT StringOnHeap
55 friend class ArrayAny;
57 StringOnHeap(const char *val);
58 StringOnHeap(const char *val, std::size_t len);
59 StringOnHeap(const std::string& val);
60 StringOnHeap(char *val, Deallocator deAlloc);
61 bool operator ==(const StringOnHeap& other) const;
62 StringOnHeap *deepCopy() const;
63 const char *cStr() const { return _str; }
64 std::size_t size() const { return _len; }
72 typedef SharedPtr<Any> AnyPtr;
75 * \brief: Interface for management of storage of data formated dynamically in its TypeCode.
76 * Warning virtual inheritance on Any daughter classes NOT supported.
78 class YACSLIBENGINE_EXPORT Any : public RefCounter
80 friend class SeqAlloc;
81 friend class ArrayAny;
82 friend class StructAny;
83 friend class SequenceAny;
85 const TypeCode *getType() const { return _type; }
86 //for convenience methods
87 virtual Any *clone() const = 0;
88 virtual AnyPtr operator[](int i) const throw(Exception) = 0;
89 virtual AnyPtr operator[](const char *key) const throw(Exception) = 0;
90 virtual bool operator ==(const Any& other) const = 0;
91 virtual int getIntValue() const throw(Exception) = 0;
92 virtual bool getBoolValue() const throw(Exception) = 0;
93 virtual double getDoubleValue() const throw(Exception) = 0;
94 virtual std::string getStringValue() const throw(Exception) = 0;
101 Any(const Any& other);
102 virtual void putMyReprAtPlace(char *data) const = 0;
103 //static AnyPtr buildAnyFromCoarseData(char *data, TypeCode* type); //Factory Method
104 static bool IsNull(char *data);
109 typedef SharedPtr<AtomAny> AtomAnyPtr;
111 class YACSLIBENGINE_EXPORT AtomAny : public Any
113 friend class TypeCode;
114 friend class TypeCodeObjref;
126 static AtomAny *New(T val) { return new AtomAny(val); }
127 static AtomAny *New(char *val, Deallocator dealloc);
128 static AtomAny *New(const std::string& val, TypeCode *type);
129 AnyPtr operator[](int i) const throw(Exception);
130 AnyPtr operator[](const char *key) const throw(Exception);
131 bool operator ==(const Any& other) const;
132 int getIntValue() const throw(Exception);
133 bool getBoolValue() const throw(Exception);
134 double getDoubleValue() const throw(Exception);
135 std::string getStringValue() const throw(Exception);
136 const char *getBytesValue(std::size_t& len) const;
138 void putMyReprAtPlace(char *data) const;
139 static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
140 static void destroyReprAtPlace(char *data, const TypeCode *type);
141 static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
142 static bool takeInChargeStorageOf(TypeCode *type);
148 AtomAny(const char *val);
149 AtomAny(const std::string& val);
150 AtomAny(const std::string& val, TypeCode* type);
151 AtomAny(const AtomAny& other);
152 AtomAny(char *data, TypeCode* type);
153 AtomAny(char *val, Deallocator deAlloc);
155 ValueContainer _value;
158 class YACSLIBENGINE_EXPORT SeqAlloc
160 friend class SequenceAny;
165 Deallocator _notStdDeAlloc;
166 const unsigned int _sizeOf1Elm;
168 SeqAlloc(const SeqAlloc& other);
169 SeqAlloc(unsigned int sizeOf1Elm);
172 void initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc);
173 void construct(char *pt, const Any *val);
174 void construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy);
175 char *allocate(unsigned int nbOfByte);
176 void destroy(char *pt, const TypeCode *tc);
177 void deallocate(char *pt);
178 unsigned int size() const;
179 std::vector<unsigned int> getSetItems() const;
181 static const char DFT_CHAR_VAR;
184 class YACSLIBENGINE_EXPORT ComposedAny : public Any
187 virtual void setEltAtRank(int i, const Any *elem) throw(Exception) = 0;
188 AnyPtr operator[](const char *key) const throw(Exception);
190 ComposedAny(const ComposedAny& other);
191 ComposedAny(TypeCode* type, bool isNew=true);
193 void checkTypeOf(const Any *elem) const throw(Exception);
194 private://error methods called during incorrect runtime extraction
195 int getIntValue() const throw(Exception);
196 bool getBoolValue() const throw(Exception);
197 double getDoubleValue() const throw(Exception);
198 std::string getStringValue() const throw(Exception);
201 typedef SharedPtr<SequenceAny> SequenceAnyPtr;
203 class YACSLIBENGINE_EXPORT SequenceAny : public ComposedAny
205 friend class TypeCodeSeq;
209 unsigned int size() const { return _alloc.size(); }
210 void pushBack(const Any *elem);
211 bool operator ==(const Any& other) const;
212 void setEltAtRank(int i, const Any *elem) throw(Exception);
213 AnyPtr operator[](int i) const throw(Exception);
216 static SequenceAny *New(const std::vector<T>& vec);
217 static SequenceAny *New(const TypeCode *typeOfContent);
218 static SequenceAny *New(const TypeCode *typeOfContent, unsigned lgth);
220 static SequenceAny *New(T *val, unsigned int lgth, Deallocator deAlloc);
221 std::vector<unsigned int> getSetItems() const { return _alloc.getSetItems(); }
222 SequenceAny *removeUnsetItemsFromThis() const;
224 void putMyReprAtPlace(char *data) const;
225 static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
226 static void destroyReprAtPlace(char *data, const TypeCode *type);
227 static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
228 static bool takeInChargeStorageOf(TypeCode *type);
231 SequenceAny(const SequenceAny& other);
232 SequenceAny(const TypeCode *typeOfContent);
233 SequenceAny(const TypeCode *typeOfContent, unsigned lgth);
234 SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc);
235 SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc);
236 SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc);
237 SequenceAny(const std::vector<int>& val);
238 SequenceAny(const std::vector<bool>& val);
239 SequenceAny(const std::vector<double>& val);
240 SequenceAny(const std::vector<std::string>& val);
241 void realloc(char *endOfCurrentAllocated, const Any *elem);
242 char *performCpy(char *srcStart, char *srcFinish, char *destStart);
247 typedef SharedPtr<ArrayAny> ArrayAnyPtr;
249 class YACSLIBENGINE_EXPORT ArrayAny : public ComposedAny
251 friend class TypeCodeArray;
253 void setEltAtRank(int i, const Any *elem) throw(Exception);
254 bool operator ==(const Any& other) const;
255 AnyPtr operator[](int i) const throw(Exception);
256 unsigned int size() const;
259 static ArrayAny *New(const std::vector<T>& vec);
261 static ArrayAny *New(const T *val, unsigned int lgth);
262 static ArrayAny *New(const TypeCode *typeOfContent, unsigned int lgth);
264 void putMyReprAtPlace(char *data) const;
265 static void putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy);
266 static void destroyReprAtPlace(char *data, const TypeCodeArray *type);
267 static AnyPtr getOrBuildFromData(char *data, const TypeCodeArray *type);
268 static bool takeInChargeStorageOf(TypeCode *type);
271 ArrayAny(const TypeCode *typeOfContent, unsigned int lgth);
272 ArrayAny(char *data, TypeCodeArray * type);
273 ArrayAny(const ArrayAny& other);
274 ArrayAny(const int *val, unsigned int lgth);
275 ArrayAny(const bool *val, unsigned int lgth);
276 ArrayAny(const double *val, unsigned int lgth);
277 ArrayAny(const std::vector<int>& val);
278 ArrayAny(const std::vector<double>& val);
279 ArrayAny(const std::vector<std::string>& val);
284 typedef SharedPtr<StructAny> StructAnyPtr;
286 class YACSLIBENGINE_EXPORT StructAny : public ComposedAny
288 friend class TypeCodeStruct;
291 bool operator ==(const Any& other) const;
292 static StructAny *New(TypeCodeStruct *type);
293 AnyPtr operator[](int i) const throw(Exception);
294 AnyPtr operator[](const char *key) const throw(Exception);
295 void setEltAtRank(int i, const Any *elem) throw(Exception);
296 void setEltAtRank(const char *key, const Any *elem) throw(Exception);
298 void putMyReprAtPlace(char *data) const;
299 static void putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy);
300 static void destroyReprAtPlace(char *data, const TypeCodeStruct *type);
301 static AnyPtr getOrBuildFromData(char *data, const TypeCodeStruct *type);
304 StructAny(TypeCodeStruct *type);
305 StructAny(const StructAny& other);
306 StructAny(char *data, TypeCodeStruct * type);
312 SequenceAny *SequenceAny::New(T *val, unsigned int lgth, Deallocator deAlloc)
314 return new SequenceAny(val,lgth,deAlloc);
318 SequenceAny *SequenceAny::New(const std::vector<T>& vec)
320 return new SequenceAny(vec);
324 ArrayAny *ArrayAny::New(const std::vector<T>& vec)
326 return new ArrayAny(vec);
330 ArrayAny *ArrayAny::New(const T *val, unsigned int lgth)
332 return new ArrayAny(val,lgth);