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_ShortReal.hxx>
26 #include <BinObjMgt_PChar.hxx>
27 #include <BinObjMgt_PByte.hxx>
28 #include <BinObjMgt_PExtChar.hxx>
29 #include <BinObjMgt_PInteger.hxx>
30 #include <BinObjMgt_PReal.hxx>
31 #include <BinObjMgt_PShortReal.hxx>
32 #include <BinObjMgt_Position.hxx>
33 #include <Standard_OStream.hxx>
34 #include <Standard_IStream.hxx>
35 class TCollection_AsciiString;
36 class TCollection_ExtendedString;
42 //! Binary persistent representation of an object.
43 //! Really it is used as a buffer for read/write an object.
45 //! It takes care of Little/Big endian by inversing bytes
46 //! in objects of standard types (see FSD_FileHeader.hxx
47 //! for the default value of DO_INVERSE).
48 class BinObjMgt_Persistent
56 Standard_EXPORT BinObjMgt_Persistent();
58 Standard_EXPORT BinObjMgt_Persistent& PutCharacter (const Standard_Character theValue);
59 BinObjMgt_Persistent& operator << (const Standard_Character theValue)
61 return PutCharacter(theValue);
64 Standard_EXPORT BinObjMgt_Persistent& PutByte (const Standard_Byte theValue);
65 BinObjMgt_Persistent& operator << (const Standard_Byte theValue)
67 return PutByte(theValue);
70 Standard_EXPORT BinObjMgt_Persistent& PutExtCharacter (const Standard_ExtCharacter theValue);
71 BinObjMgt_Persistent& operator << (const Standard_ExtCharacter theValue)
73 return PutExtCharacter(theValue);
76 Standard_EXPORT BinObjMgt_Persistent& PutInteger (const Standard_Integer theValue);
77 BinObjMgt_Persistent& operator << (const Standard_Integer theValue)
79 return PutInteger(theValue);
82 BinObjMgt_Persistent& PutBoolean (const Standard_Boolean theValue);
83 BinObjMgt_Persistent& operator << (const Standard_Boolean theValue)
85 return PutBoolean(theValue);
88 Standard_EXPORT BinObjMgt_Persistent& PutReal (const Standard_Real theValue);
89 BinObjMgt_Persistent& operator << (const Standard_Real theValue)
91 return PutReal(theValue);
94 Standard_EXPORT BinObjMgt_Persistent& PutShortReal (const Standard_ShortReal theValue);
95 BinObjMgt_Persistent& operator << (const Standard_ShortReal theValue)
97 return PutShortReal(theValue);
100 //! Offset in output buffer is not aligned
101 Standard_EXPORT BinObjMgt_Persistent& PutCString (const Standard_CString theValue);
102 BinObjMgt_Persistent& operator << (const Standard_CString theValue)
104 return PutCString(theValue);
107 //! Offset in output buffer is word-aligned
108 Standard_EXPORT BinObjMgt_Persistent& PutAsciiString (const TCollection_AsciiString& theValue);
109 BinObjMgt_Persistent& operator << (const TCollection_AsciiString& theValue)
111 return PutAsciiString(theValue);
114 //! Offset in output buffer is word-aligned
115 Standard_EXPORT BinObjMgt_Persistent& PutExtendedString (const TCollection_ExtendedString& theValue);
116 BinObjMgt_Persistent& operator << (const TCollection_ExtendedString& theValue)
118 return PutExtendedString(theValue);
121 Standard_EXPORT BinObjMgt_Persistent& PutLabel (const TDF_Label& theValue);
122 BinObjMgt_Persistent& operator << (const TDF_Label& theValue)
124 return PutLabel(theValue);
127 Standard_EXPORT BinObjMgt_Persistent& PutGUID (const Standard_GUID& theValue);
128 BinObjMgt_Persistent& operator << (const Standard_GUID& theValue)
130 return PutGUID(theValue);
133 //! Put C array of char, theLength is the number of elements
134 Standard_EXPORT BinObjMgt_Persistent& PutCharArray (const BinObjMgt_PChar theArray, const Standard_Integer theLength);
136 //! Put C array of unsigned chars, theLength is the number of elements
137 Standard_EXPORT BinObjMgt_Persistent& PutByteArray (const BinObjMgt_PByte theArray, const Standard_Integer theLength);
139 //! Put C array of ExtCharacter, theLength is the number of elements
140 Standard_EXPORT BinObjMgt_Persistent& PutExtCharArray (const BinObjMgt_PExtChar theArray, const Standard_Integer theLength);
142 //! Put C array of int, theLength is the number of elements
143 Standard_EXPORT BinObjMgt_Persistent& PutIntArray (const BinObjMgt_PInteger theArray, const Standard_Integer theLength);
145 //! Put C array of double, theLength is the number of elements
146 Standard_EXPORT BinObjMgt_Persistent& PutRealArray (const BinObjMgt_PReal theArray, const Standard_Integer theLength);
148 //! Put C array of float, theLength is the number of elements
149 Standard_EXPORT BinObjMgt_Persistent& PutShortRealArray (const BinObjMgt_PShortReal theArray, const Standard_Integer theLength);
151 Standard_EXPORT const BinObjMgt_Persistent& GetCharacter (Standard_Character& theValue) const;
152 const BinObjMgt_Persistent& operator >> (Standard_Character& theValue) const
154 return GetCharacter(theValue);
157 Standard_EXPORT const BinObjMgt_Persistent& GetByte (Standard_Byte& theValue) const;
158 const BinObjMgt_Persistent& operator >> (Standard_Byte& theValue) const
160 return GetByte(theValue);
163 Standard_EXPORT const BinObjMgt_Persistent& GetExtCharacter (Standard_ExtCharacter& theValue) const;
164 const BinObjMgt_Persistent& operator >> (Standard_ExtCharacter& theValue) const
166 return GetExtCharacter(theValue);
169 Standard_EXPORT const BinObjMgt_Persistent& GetInteger (Standard_Integer& theValue) const;
170 const BinObjMgt_Persistent& operator >> (Standard_Integer& theValue) const
172 return GetInteger(theValue);
175 const BinObjMgt_Persistent& GetBoolean (Standard_Boolean& theValue) const;
176 const BinObjMgt_Persistent& operator >> (Standard_Boolean& theValue) const
178 return GetBoolean(theValue);
181 Standard_EXPORT const BinObjMgt_Persistent& GetReal (Standard_Real& theValue) const;
182 const BinObjMgt_Persistent& operator >> (Standard_Real& theValue) const
184 return GetReal(theValue);
187 Standard_EXPORT const BinObjMgt_Persistent& GetShortReal (Standard_ShortReal& theValue) const;
188 const BinObjMgt_Persistent& operator >> (Standard_ShortReal& theValue) const
190 return GetShortReal(theValue);
193 Standard_EXPORT const BinObjMgt_Persistent& GetAsciiString (TCollection_AsciiString& theValue) const;
194 const BinObjMgt_Persistent& operator >> (TCollection_AsciiString& theValue) const
196 return GetAsciiString(theValue);
199 Standard_EXPORT const BinObjMgt_Persistent& GetExtendedString (TCollection_ExtendedString& theValue) const;
200 const BinObjMgt_Persistent& operator >> (TCollection_ExtendedString& theValue) const
202 return GetExtendedString(theValue);
205 Standard_EXPORT const BinObjMgt_Persistent& GetLabel (const Handle(TDF_Data)& theDS, TDF_Label& theValue) const;
207 Standard_EXPORT const BinObjMgt_Persistent& GetGUID (Standard_GUID& theValue) const;
208 const BinObjMgt_Persistent& operator >> (Standard_GUID& theValue) const
210 return GetGUID(theValue);
213 //! Get C array of char, theLength is the number of elements;
214 //! theArray must point to a
215 //! space enough to place theLength elements
216 Standard_EXPORT const BinObjMgt_Persistent& GetCharArray (const BinObjMgt_PChar theArray, const Standard_Integer theLength) const;
218 //! Get C array of unsigned chars, theLength is the number of elements;
219 //! theArray must point to a
220 //! space enough to place theLength elements
221 Standard_EXPORT const BinObjMgt_Persistent& GetByteArray (const BinObjMgt_PByte theArray, const Standard_Integer theLength) const;
223 //! Get C array of ExtCharacter, theLength is the number of elements;
224 //! theArray must point to a
225 //! space enough to place theLength elements
226 Standard_EXPORT const BinObjMgt_Persistent& GetExtCharArray (const BinObjMgt_PExtChar theArray, const Standard_Integer theLength) const;
228 //! Get C array of int, theLength is the number of elements;
229 //! theArray must point to a
230 //! space enough to place theLength elements
231 Standard_EXPORT const BinObjMgt_Persistent& GetIntArray (const BinObjMgt_PInteger theArray, const Standard_Integer theLength) const;
233 //! Get C array of double, theLength is the number of elements;
234 //! theArray must point to a
235 //! space enough to place theLength elements
236 Standard_EXPORT const BinObjMgt_Persistent& GetRealArray (const BinObjMgt_PReal theArray, const Standard_Integer theLength) const;
238 //! Get C array of float, theLength is the number of elements;
239 //! theArray must point to a
240 //! space enough to place theLength elements
241 Standard_EXPORT const BinObjMgt_Persistent& GetShortRealArray (const BinObjMgt_PShortReal theArray, const Standard_Integer theLength) const;
243 //! Tells the current position for get/put
244 Standard_Integer Position() const;
246 //! Sets the current position for get/put.
247 //! Resets an error state depending on the validity of thePos.
248 //! Returns the new state (value of IsOK())
249 Standard_Boolean SetPosition (const Standard_Integer thePos) const;
251 //! Truncates the buffer by current position,
252 //! i.e. updates mySize
255 //! Indicates an error after Get methods or SetPosition
256 Standard_Boolean IsError() const;
257 Standard_Boolean operator !() const
262 //! Indicates a good state after Get methods or SetPosition
263 Standard_Boolean IsOK() const;
264 operator Standard_Boolean () const { return IsOK(); }
266 //! Initializes me to reuse again
267 Standard_EXPORT void Init();
269 //! Sets the Id of the object
270 void SetId (const Standard_Integer theId);
272 //! Sets the Id of the type of the object
273 void SetTypeId (const Standard_Integer theId);
275 //! Returns the Id of the object
276 Standard_Integer Id() const;
278 //! Returns the Id of the type of the object
279 Standard_Integer TypeId() const;
281 //! Returns the length of data
282 Standard_Integer Length() const;
284 //! Stores <me> to the stream.
285 //! inline Standard_OStream& operator<< (Standard_OStream&,
286 //! BinObjMgt_Persistent&) is also available.
287 //! If theDirectStream is true, after this data the direct stream data is stored.
288 Standard_EXPORT Standard_OStream& Write (Standard_OStream& theOS, const Standard_Boolean theDirectStream = Standard_False);
290 //! Retrieves <me> from the stream.
291 //! inline Standard_IStream& operator>> (Standard_IStream&,
292 //! BinObjMgt_Persistent&) is also available
293 Standard_EXPORT Standard_IStream& Read (Standard_IStream& theIS);
295 //! Frees the allocated memory;
296 //! This object can be reused after call to Init
297 Standard_EXPORT void Destroy();
298 ~BinObjMgt_Persistent()
303 //! Sets the stream for direct writing
304 Standard_EXPORT void SetOStream (Standard_OStream& theStream) { myOStream = &theStream; }
305 //! Sets the stream for direct reading
306 Standard_EXPORT void SetIStream (Standard_IStream& theStream) { myIStream = &theStream; }
307 //! Gets the stream for and enables direct writing
308 Standard_EXPORT Standard_OStream* GetOStream();
309 //! Gets the stream for and enables direct reading
310 Standard_EXPORT Standard_IStream* GetIStream();
311 //! Returns true if after this record a direct writing to the stream is performed.
312 Standard_EXPORT Standard_Boolean IsDirect() { return myDirectWritingIsEnabled; }
313 //! Returns the start position of the direct writing in the stream
314 Standard_EXPORT Handle(BinObjMgt_Position) StreamStart() { return myStreamStart; }
319 //! Aligns myOffset to the given size;
320 //! enters the next piece if the end of the current one is reached;
321 //! toClear==true means to fill unused space by 0
322 void alignOffset (const Standard_Integer theSize, const Standard_Boolean toClear = Standard_False) const;
324 //! Prepares the room for theSize bytes;
325 //! returns the number of pieces except for the current one
326 //! are to be occupied
327 Standard_Integer prepareForPut (const Standard_Integer theSize);
329 //! Allocates theNbPieces more pieces
330 Standard_EXPORT void incrementData (const Standard_Integer theNbPieces);
332 //! Checks if there is no more data of the given size starting
333 //! from the current position in myData
334 Standard_Boolean noMoreData (const Standard_Integer theSize) const;
336 //! Puts theLength bytes from theArray
337 Standard_EXPORT void putArray (const Standard_Address theArray, const Standard_Integer theSize);
339 //! Gets theLength bytes into theArray
340 Standard_EXPORT void getArray (const Standard_Address theArray, const Standard_Integer theSize) const;
342 //! Inverses bytes in the data addressed by the given values
343 Standard_EXPORT void inverseExtCharData (const Standard_Integer theIndex, const Standard_Integer theOffset, const Standard_Integer theSize);
345 //! Inverses bytes in the data addressed by the given values
346 Standard_EXPORT void inverseIntData (const Standard_Integer theIndex, const Standard_Integer theOffset, const Standard_Integer theSize);
348 //! Inverses bytes in the data addressed by the given values
349 Standard_EXPORT void inverseRealData (const Standard_Integer theIndex, const Standard_Integer theOffset, const Standard_Integer theSize);
351 //! Inverses bytes in the data addressed by the given values
352 Standard_EXPORT void inverseShortRealData (const Standard_Integer theIndex, const Standard_Integer theOffset, const Standard_Integer theSize);
355 TColStd_SequenceOfAddress myData;
356 Standard_Integer myIndex;
357 Standard_Integer myOffset;
358 Standard_Integer mySize;
359 Standard_Boolean myIsError;
360 Standard_OStream* myOStream; ///< stream to write in case direct writing is enabled
361 Standard_IStream* myIStream; ///< stream to write in case direct reading is enabled
362 Standard_Boolean myDirectWritingIsEnabled;
363 Handle(BinObjMgt_Position) myStreamStart; ///< position where the direct writing to the script is started
367 #include <BinObjMgt_Persistent.lxx>
373 #endif // _BinObjMgt_Persistent_HeaderFile