1 // Created on: 2002-10-30
2 // Created by: Michael SAZONOV
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
5 // This file is part of Open CASCADE Technology software library.
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
16 #ifndef _BinObjMgt_Persistent_HeaderFile
17 #define _BinObjMgt_Persistent_HeaderFile
19 #include <Standard.hxx>
20 #include <Standard_DefineAlloc.hxx>
21 #include <Standard_Handle.hxx>
23 #include <TColStd_SequenceOfAddress.hxx>
24 #include <Standard_Integer.hxx>
25 #include <Standard_Boolean.hxx>
26 #include <Standard_Character.hxx>
27 #include <Standard_Byte.hxx>
28 #include <Standard_ExtCharacter.hxx>
29 #include <Standard_Real.hxx>
30 #include <Standard_ShortReal.hxx>
31 #include <Standard_CString.hxx>
32 #include <BinObjMgt_PChar.hxx>
33 #include <BinObjMgt_PByte.hxx>
34 #include <BinObjMgt_PExtChar.hxx>
35 #include <BinObjMgt_PInteger.hxx>
36 #include <BinObjMgt_PReal.hxx>
37 #include <BinObjMgt_PShortReal.hxx>
38 #include <Standard_OStream.hxx>
39 #include <Standard_IStream.hxx>
40 #include <Standard_Address.hxx>
41 class TCollection_AsciiString;
42 class TCollection_ExtendedString;
48 //! Binary persistent representation of an object.
49 //! Really it is used as a buffer for read/write an object.
51 //! It takes care of Little/Big endian by inversing bytes
52 //! in objects of standard types (see FSD_FileHeader.hxx
53 //! for the default value of DO_INVERSE).
54 class BinObjMgt_Persistent
62 Standard_EXPORT BinObjMgt_Persistent();
64 Standard_EXPORT BinObjMgt_Persistent& PutCharacter (const Standard_Character theValue);
65 BinObjMgt_Persistent& operator << (const Standard_Character theValue)
67 return PutCharacter(theValue);
70 Standard_EXPORT BinObjMgt_Persistent& PutByte (const Standard_Byte theValue);
71 BinObjMgt_Persistent& operator << (const Standard_Byte theValue)
73 return PutByte(theValue);
76 Standard_EXPORT BinObjMgt_Persistent& PutExtCharacter (const Standard_ExtCharacter theValue);
77 BinObjMgt_Persistent& operator << (const Standard_ExtCharacter theValue)
79 return PutExtCharacter(theValue);
82 Standard_EXPORT BinObjMgt_Persistent& PutInteger (const Standard_Integer theValue);
83 BinObjMgt_Persistent& operator << (const Standard_Integer theValue)
85 return PutInteger(theValue);
88 BinObjMgt_Persistent& PutBoolean (const Standard_Boolean theValue);
89 BinObjMgt_Persistent& operator << (const Standard_Boolean theValue)
91 return PutBoolean(theValue);
94 Standard_EXPORT BinObjMgt_Persistent& PutReal (const Standard_Real theValue);
95 BinObjMgt_Persistent& operator << (const Standard_Real theValue)
97 return PutReal(theValue);
100 Standard_EXPORT BinObjMgt_Persistent& PutShortReal (const Standard_ShortReal theValue);
101 BinObjMgt_Persistent& operator << (const Standard_ShortReal theValue)
103 return PutShortReal(theValue);
106 //! Offset in output buffer is not aligned
107 Standard_EXPORT BinObjMgt_Persistent& PutCString (const Standard_CString theValue);
108 BinObjMgt_Persistent& operator << (const Standard_CString theValue)
110 return PutCString(theValue);
113 //! Offset in output buffer is word-aligned
114 Standard_EXPORT BinObjMgt_Persistent& PutAsciiString (const TCollection_AsciiString& theValue);
115 BinObjMgt_Persistent& operator << (const TCollection_AsciiString& theValue)
117 return PutAsciiString(theValue);
120 //! Offset in output buffer is word-aligned
121 Standard_EXPORT BinObjMgt_Persistent& PutExtendedString (const TCollection_ExtendedString& theValue);
122 BinObjMgt_Persistent& operator << (const TCollection_ExtendedString& theValue)
124 return PutExtendedString(theValue);
127 Standard_EXPORT BinObjMgt_Persistent& PutLabel (const TDF_Label& theValue);
128 BinObjMgt_Persistent& operator << (const TDF_Label& theValue)
130 return PutLabel(theValue);
133 Standard_EXPORT BinObjMgt_Persistent& PutGUID (const Standard_GUID& theValue);
134 BinObjMgt_Persistent& operator << (const Standard_GUID& theValue)
136 return PutGUID(theValue);
139 //! Put C array of char, theLength is the number of elements
140 Standard_EXPORT BinObjMgt_Persistent& PutCharArray (const BinObjMgt_PChar theArray, const Standard_Integer theLength);
142 //! Put C array of unsigned chars, theLength is the number of elements
143 Standard_EXPORT BinObjMgt_Persistent& PutByteArray (const BinObjMgt_PByte theArray, const Standard_Integer theLength);
145 //! Put C array of ExtCharacter, theLength is the number of elements
146 Standard_EXPORT BinObjMgt_Persistent& PutExtCharArray (const BinObjMgt_PExtChar theArray, const Standard_Integer theLength);
148 //! Put C array of int, theLength is the number of elements
149 Standard_EXPORT BinObjMgt_Persistent& PutIntArray (const BinObjMgt_PInteger theArray, const Standard_Integer theLength);
151 //! Put C array of double, theLength is the number of elements
152 Standard_EXPORT BinObjMgt_Persistent& PutRealArray (const BinObjMgt_PReal theArray, const Standard_Integer theLength);
154 //! Put C array of float, theLength is the number of elements
155 Standard_EXPORT BinObjMgt_Persistent& PutShortRealArray (const BinObjMgt_PShortReal theArray, const Standard_Integer theLength);
157 Standard_EXPORT const BinObjMgt_Persistent& GetCharacter (Standard_Character& theValue) const;
158 const BinObjMgt_Persistent& operator >> (Standard_Character& theValue) const
160 return GetCharacter(theValue);
163 Standard_EXPORT const BinObjMgt_Persistent& GetByte (Standard_Byte& theValue) const;
164 const BinObjMgt_Persistent& operator >> (Standard_Byte& theValue) const
166 return GetByte(theValue);
169 Standard_EXPORT const BinObjMgt_Persistent& GetExtCharacter (Standard_ExtCharacter& theValue) const;
170 const BinObjMgt_Persistent& operator >> (Standard_ExtCharacter& theValue) const
172 return GetExtCharacter(theValue);
175 Standard_EXPORT const BinObjMgt_Persistent& GetInteger (Standard_Integer& theValue) const;
176 const BinObjMgt_Persistent& operator >> (Standard_Integer& theValue) const
178 return GetInteger(theValue);
181 const BinObjMgt_Persistent& GetBoolean (Standard_Boolean& theValue) const;
182 const BinObjMgt_Persistent& operator >> (Standard_Boolean& theValue) const
184 return GetBoolean(theValue);
187 Standard_EXPORT const BinObjMgt_Persistent& GetReal (Standard_Real& theValue) const;
188 const BinObjMgt_Persistent& operator >> (Standard_Real& theValue) const
190 return GetReal(theValue);
193 Standard_EXPORT const BinObjMgt_Persistent& GetShortReal (Standard_ShortReal& theValue) const;
194 const BinObjMgt_Persistent& operator >> (Standard_ShortReal& theValue) const
196 return GetShortReal(theValue);
199 Standard_EXPORT const BinObjMgt_Persistent& GetAsciiString (TCollection_AsciiString& theValue) const;
200 const BinObjMgt_Persistent& operator >> (TCollection_AsciiString& theValue) const
202 return GetAsciiString(theValue);
205 Standard_EXPORT const BinObjMgt_Persistent& GetExtendedString (TCollection_ExtendedString& theValue) const;
206 const BinObjMgt_Persistent& operator >> (TCollection_ExtendedString& theValue) const
208 return GetExtendedString(theValue);
211 Standard_EXPORT const BinObjMgt_Persistent& GetLabel (const Handle(TDF_Data)& theDS, TDF_Label& theValue) const;
213 Standard_EXPORT const BinObjMgt_Persistent& GetGUID (Standard_GUID& theValue) const;
214 const BinObjMgt_Persistent& operator >> (Standard_GUID& theValue) const
216 return GetGUID(theValue);
219 //! Get C array of char, theLength is the number of elements;
220 //! theArray must point to a
221 //! space enough to place theLength elements
222 Standard_EXPORT const BinObjMgt_Persistent& GetCharArray (const BinObjMgt_PChar theArray, const Standard_Integer theLength) const;
224 //! Get C array of unsigned chars, theLength is the number of elements;
225 //! theArray must point to a
226 //! space enough to place theLength elements
227 Standard_EXPORT const BinObjMgt_Persistent& GetByteArray (const BinObjMgt_PByte theArray, const Standard_Integer theLength) const;
229 //! Get C array of ExtCharacter, theLength is the number of elements;
230 //! theArray must point to a
231 //! space enough to place theLength elements
232 Standard_EXPORT const BinObjMgt_Persistent& GetExtCharArray (const BinObjMgt_PExtChar theArray, const Standard_Integer theLength) const;
234 //! Get C array of int, theLength is the number of elements;
235 //! theArray must point to a
236 //! space enough to place theLength elements
237 Standard_EXPORT const BinObjMgt_Persistent& GetIntArray (const BinObjMgt_PInteger theArray, const Standard_Integer theLength) const;
239 //! Get C array of double, theLength is the number of elements;
240 //! theArray must point to a
241 //! space enough to place theLength elements
242 Standard_EXPORT const BinObjMgt_Persistent& GetRealArray (const BinObjMgt_PReal theArray, const Standard_Integer theLength) const;
244 //! Get C array of float, theLength is the number of elements;
245 //! theArray must point to a
246 //! space enough to place theLength elements
247 Standard_EXPORT const BinObjMgt_Persistent& GetShortRealArray (const BinObjMgt_PShortReal theArray, const Standard_Integer theLength) const;
249 //! Tells the current position for get/put
250 Standard_Integer Position() const;
252 //! Sets the current position for get/put.
253 //! Resets an error state depending on the validity of thePos.
254 //! Returns the new state (value of IsOK())
255 Standard_Boolean SetPosition (const Standard_Integer thePos) const;
257 //! Truncates the buffer by current position,
258 //! i.e. updates mySize
261 //! Indicates an error after Get methods or SetPosition
262 Standard_Boolean IsError() const;
263 Standard_Boolean operator !() const
268 //! Indicates a good state after Get methods or SetPosition
269 Standard_Boolean IsOK() const;
270 operator Standard_Boolean () const { return IsOK(); }
272 //! Initializes me to reuse again
273 Standard_EXPORT void Init();
275 //! Sets the Id of the object
276 void SetId (const Standard_Integer theId);
278 //! Sets the Id of the type of the object
279 void SetTypeId (const Standard_Integer theId);
281 //! Returns the Id of the object
282 Standard_Integer Id() const;
284 //! Returns the Id of the type of the object
285 Standard_Integer TypeId() const;
287 //! Returns the length of data
288 Standard_Integer Length() const;
290 //! Stores <me> to the stream.
291 //! inline Standard_OStream& operator<< (Standard_OStream&,
292 //! BinObjMgt_Persistent&) is also available
293 Standard_EXPORT Standard_OStream& Write (Standard_OStream& theOS);
295 //! Retrieves <me> from the stream.
296 //! inline Standard_IStream& operator>> (Standard_IStream&,
297 //! BinObjMgt_Persistent&) is also available
298 Standard_EXPORT Standard_IStream& Read (Standard_IStream& theIS);
300 //! Frees the allocated memory;
301 //! This object can be reused after call to Init
302 Standard_EXPORT void Destroy();
303 ~BinObjMgt_Persistent()
320 //! Aligns myOffset to the given size;
321 //! enters the next piece if the end of the current one is reached;
322 //! toClear==true means to fill unused space by 0
323 void alignOffset (const Standard_Integer theSize, const Standard_Boolean toClear = Standard_False) const;
325 //! Prepares the room for theSize bytes;
326 //! returns the number of pieces except for the current one
327 //! are to be occupied
328 Standard_Integer prepareForPut (const Standard_Integer theSize);
330 //! Allocates theNbPieces more pieces
331 Standard_EXPORT void incrementData (const Standard_Integer theNbPieces);
333 //! Checks if there is no more data of the given size starting
334 //! from the current position in myData
335 Standard_Boolean noMoreData (const Standard_Integer theSize) const;
337 //! Puts theLength bytes from theArray
338 Standard_EXPORT void putArray (const Standard_Address theArray, const Standard_Integer theSize);
340 //! Gets theLength bytes into theArray
341 Standard_EXPORT void getArray (const Standard_Address theArray, const Standard_Integer theSize) const;
343 //! Inverses bytes in the data addressed by the given values
344 Standard_EXPORT void inverseExtCharData (const Standard_Integer theIndex, const Standard_Integer theOffset, const Standard_Integer theSize);
346 //! Inverses bytes in the data addressed by the given values
347 Standard_EXPORT void inverseIntData (const Standard_Integer theIndex, const Standard_Integer theOffset, const Standard_Integer theSize);
349 //! Inverses bytes in the data addressed by the given values
350 Standard_EXPORT void inverseRealData (const Standard_Integer theIndex, const Standard_Integer theOffset, const Standard_Integer theSize);
352 //! Inverses bytes in the data addressed by the given values
353 Standard_EXPORT void inverseShortRealData (const Standard_Integer theIndex, const Standard_Integer theOffset, const Standard_Integer theSize);
356 TColStd_SequenceOfAddress myData;
357 Standard_Integer myIndex;
358 Standard_Integer myOffset;
359 Standard_Integer mySize;
360 Standard_Boolean myIsError;
366 #include <BinObjMgt_Persistent.lxx>
372 #endif // _BinObjMgt_Persistent_HeaderFile