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;
125 static AtomAny *New(T val) { return new AtomAny(val); }
126 static AtomAny *New(char *val, Deallocator dealloc);
127 AnyPtr operator[](int i) const throw(Exception);
128 AnyPtr operator[](const char *key) const throw(Exception);
129 bool operator ==(const Any& other) const;
130 int getIntValue() const throw(Exception);
131 bool getBoolValue() const throw(Exception);
132 double getDoubleValue() const throw(Exception);
133 std::string getStringValue() const throw(Exception);
134 const char *getBytesValue(std::size_t& len) const;
136 void putMyReprAtPlace(char *data) const;
137 static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
138 static void destroyReprAtPlace(char *data, const TypeCode *type);
139 static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
140 static bool takeInChargeStorageOf(TypeCode *type);
146 AtomAny(const char *val);
147 AtomAny(const std::string& val);
148 AtomAny(const AtomAny& other);
149 AtomAny(char *data, TypeCode* type);
150 AtomAny(char *val, Deallocator deAlloc);
152 ValueContainer _value;
155 class YACSLIBENGINE_EXPORT SeqAlloc
157 friend class SequenceAny;
162 Deallocator _notStdDeAlloc;
163 const unsigned int _sizeOf1Elm;
165 SeqAlloc(const SeqAlloc& other);
166 SeqAlloc(unsigned int sizeOf1Elm);
169 void initCoarseMemory(char *mem, unsigned int size, Deallocator dealloc);
170 void construct(char *pt, const Any *val);
171 void construct(char *pt, const char *val, const TypeCode *tc, bool deepCpy);
172 char *allocate(unsigned int nbOfByte);
173 void destroy(char *pt, const TypeCode *tc);
174 void deallocate(char *pt);
175 unsigned int size() const;
176 std::vector<unsigned int> getSetItems() const;
178 static const char DFT_CHAR_VAR;
181 class YACSLIBENGINE_EXPORT ComposedAny : public Any
184 virtual void setEltAtRank(int i, const Any *elem) throw(Exception) = 0;
185 AnyPtr operator[](const char *key) const throw(Exception);
187 ComposedAny(const ComposedAny& other);
188 ComposedAny(TypeCode* type, bool isNew=true);
190 void checkTypeOf(const Any *elem) const throw(Exception);
191 private://error methods called during incorrect runtime extraction
192 int getIntValue() const throw(Exception);
193 bool getBoolValue() const throw(Exception);
194 double getDoubleValue() const throw(Exception);
195 std::string getStringValue() const throw(Exception);
198 typedef SharedPtr<SequenceAny> SequenceAnyPtr;
200 class YACSLIBENGINE_EXPORT SequenceAny : public ComposedAny
202 friend class TypeCodeSeq;
206 unsigned int size() const { return _alloc.size(); }
207 void pushBack(const Any *elem);
208 bool operator ==(const Any& other) const;
209 void setEltAtRank(int i, const Any *elem) throw(Exception);
210 AnyPtr operator[](int i) const throw(Exception);
213 static SequenceAny *New(const std::vector<T>& vec);
214 static SequenceAny *New(const TypeCode *typeOfContent);
215 static SequenceAny *New(const TypeCode *typeOfContent, unsigned lgth);
217 static SequenceAny *New(T *val, unsigned int lgth, Deallocator deAlloc);
218 std::vector<unsigned int> getSetItems() const { return _alloc.getSetItems(); }
219 SequenceAny *removeUnsetItemsFromThis() const;
221 void putMyReprAtPlace(char *data) const;
222 static void putReprAtPlace(char *data, const char *src, const TypeCode *type, bool deepCpy);
223 static void destroyReprAtPlace(char *data, const TypeCode *type);
224 static AnyPtr getOrBuildFromData(char *data, const TypeCode *type);
225 static bool takeInChargeStorageOf(TypeCode *type);
228 SequenceAny(const SequenceAny& other);
229 SequenceAny(const TypeCode *typeOfContent);
230 SequenceAny(const TypeCode *typeOfContent, unsigned lgth);
231 SequenceAny(int *val, unsigned int lgth, Deallocator deAlloc);
232 SequenceAny(bool *val, unsigned int lgth, Deallocator deAlloc);
233 SequenceAny(double *val, unsigned int lgth, Deallocator deAlloc);
234 SequenceAny(const std::vector<int>& val);
235 SequenceAny(const std::vector<bool>& val);
236 SequenceAny(const std::vector<double>& val);
237 SequenceAny(const std::vector<std::string>& val);
238 void realloc(char *endOfCurrentAllocated, const Any *elem);
239 char *performCpy(char *srcStart, char *srcFinish, char *destStart);
244 typedef SharedPtr<ArrayAny> ArrayAnyPtr;
246 class YACSLIBENGINE_EXPORT ArrayAny : public ComposedAny
248 friend class TypeCodeArray;
250 void setEltAtRank(int i, const Any *elem) throw(Exception);
251 bool operator ==(const Any& other) const;
252 AnyPtr operator[](int i) const throw(Exception);
253 unsigned int size() const;
256 static ArrayAny *New(const std::vector<T>& vec);
258 static ArrayAny *New(const T *val, unsigned int lgth);
259 static ArrayAny *New(const TypeCode *typeOfContent, unsigned int lgth);
261 void putMyReprAtPlace(char *data) const;
262 static void putReprAtPlace(char *data, const char *src, const TypeCodeArray *type, bool deepCpy);
263 static void destroyReprAtPlace(char *data, const TypeCodeArray *type);
264 static AnyPtr getOrBuildFromData(char *data, const TypeCodeArray *type);
265 static bool takeInChargeStorageOf(TypeCode *type);
268 ArrayAny(const TypeCode *typeOfContent, unsigned int lgth);
269 ArrayAny(char *data, TypeCodeArray * type);
270 ArrayAny(const ArrayAny& other);
271 ArrayAny(const int *val, unsigned int lgth);
272 ArrayAny(const bool *val, unsigned int lgth);
273 ArrayAny(const double *val, unsigned int lgth);
274 ArrayAny(const std::vector<int>& val);
275 ArrayAny(const std::vector<double>& val);
276 ArrayAny(const std::vector<std::string>& val);
281 typedef SharedPtr<StructAny> StructAnyPtr;
283 class YACSLIBENGINE_EXPORT StructAny : public ComposedAny
285 friend class TypeCodeStruct;
288 bool operator ==(const Any& other) const;
289 static StructAny *New(TypeCodeStruct *type);
290 AnyPtr operator[](int i) const throw(Exception);
291 AnyPtr operator[](const char *key) const throw(Exception);
292 void setEltAtRank(int i, const Any *elem) throw(Exception);
293 void setEltAtRank(const char *key, const Any *elem) throw(Exception);
295 void putMyReprAtPlace(char *data) const;
296 static void putReprAtPlace(char *data, const char *src, const TypeCodeStruct *type, bool deepCpy);
297 static void destroyReprAtPlace(char *data, const TypeCodeStruct *type);
298 static AnyPtr getOrBuildFromData(char *data, const TypeCodeStruct *type);
301 StructAny(TypeCodeStruct *type);
302 StructAny(const StructAny& other);
303 StructAny(char *data, TypeCodeStruct * type);
309 SequenceAny *SequenceAny::New(T *val, unsigned int lgth, Deallocator deAlloc)
311 return new SequenceAny(val,lgth,deAlloc);
315 SequenceAny *SequenceAny::New(const std::vector<T>& vec)
317 return new SequenceAny(vec);
321 ArrayAny *ArrayAny::New(const std::vector<T>& vec)
323 return new ArrayAny(vec);
327 ArrayAny *ArrayAny::New(const T *val, unsigned int lgth)
329 return new ArrayAny(val,lgth);