0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / BinObjMgt / BinObjMgt_Persistent.hxx
1 // Created on: 2002-10-30
2 // Created by: Michael SAZONOV
3 // Copyright (c) 2002-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
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.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16 #ifndef _BinObjMgt_Persistent_HeaderFile
17 #define _BinObjMgt_Persistent_HeaderFile
18
19 #include <Standard.hxx>
20 #include <Standard_DefineAlloc.hxx>
21 #include <Standard_Handle.hxx>
22
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;
37 class TDF_Label;
38 class Standard_GUID;
39 class TDF_Data;
40
41
42 //! Binary persistent representation of an object.
43 //! Really it is used as a buffer for read/write an object.
44 //!
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 
49 {
50 public:
51
52   DEFINE_STANDARD_ALLOC
53
54   
55   //! Empty constructor
56   Standard_EXPORT BinObjMgt_Persistent();
57   
58   Standard_EXPORT BinObjMgt_Persistent& PutCharacter (const Standard_Character theValue);
59 BinObjMgt_Persistent& operator << (const Standard_Character theValue)
60 {
61   return PutCharacter(theValue);
62 }
63   
64   Standard_EXPORT BinObjMgt_Persistent& PutByte (const Standard_Byte theValue);
65 BinObjMgt_Persistent& operator << (const Standard_Byte theValue)
66 {
67   return PutByte(theValue);
68 }
69   
70   Standard_EXPORT BinObjMgt_Persistent& PutExtCharacter (const Standard_ExtCharacter theValue);
71 BinObjMgt_Persistent& operator << (const Standard_ExtCharacter theValue)
72 {
73   return PutExtCharacter(theValue);
74 }
75   
76   Standard_EXPORT BinObjMgt_Persistent& PutInteger (const Standard_Integer theValue);
77 BinObjMgt_Persistent& operator << (const Standard_Integer theValue)
78 {
79   return PutInteger(theValue);
80 }
81   
82     BinObjMgt_Persistent& PutBoolean (const Standard_Boolean theValue);
83   BinObjMgt_Persistent& operator << (const Standard_Boolean theValue)
84 {
85   return PutBoolean(theValue);
86 }
87   
88   Standard_EXPORT BinObjMgt_Persistent& PutReal (const Standard_Real theValue);
89 BinObjMgt_Persistent& operator << (const Standard_Real theValue)
90 {
91   return PutReal(theValue);
92 }
93   
94   Standard_EXPORT BinObjMgt_Persistent& PutShortReal (const Standard_ShortReal theValue);
95 BinObjMgt_Persistent& operator << (const Standard_ShortReal theValue)
96 {
97   return PutShortReal(theValue);
98 }
99   
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)
103 {
104   return PutCString(theValue);
105 }
106   
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)
110 {
111   return PutAsciiString(theValue);
112 }
113   
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)
117 {
118   return PutExtendedString(theValue);
119 }
120   
121   Standard_EXPORT BinObjMgt_Persistent& PutLabel (const TDF_Label& theValue);
122 BinObjMgt_Persistent& operator << (const TDF_Label& theValue)
123 {
124   return PutLabel(theValue);
125 }
126   
127   Standard_EXPORT BinObjMgt_Persistent& PutGUID (const Standard_GUID& theValue);
128 BinObjMgt_Persistent& operator << (const Standard_GUID& theValue)
129 {
130   return PutGUID(theValue);
131 }
132   
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);
135   
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);
138   
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);
141   
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);
144   
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);
147   
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);
150   
151   Standard_EXPORT const BinObjMgt_Persistent& GetCharacter (Standard_Character& theValue) const;
152 const BinObjMgt_Persistent& operator >> (Standard_Character& theValue) const
153 {
154   return GetCharacter(theValue);
155 }
156   
157   Standard_EXPORT const BinObjMgt_Persistent& GetByte (Standard_Byte& theValue) const;
158 const BinObjMgt_Persistent& operator >> (Standard_Byte& theValue) const
159 {
160   return GetByte(theValue);
161 }
162   
163   Standard_EXPORT const BinObjMgt_Persistent& GetExtCharacter (Standard_ExtCharacter& theValue) const;
164 const BinObjMgt_Persistent& operator >> (Standard_ExtCharacter& theValue) const
165 {
166   return GetExtCharacter(theValue);
167 }
168   
169   Standard_EXPORT const BinObjMgt_Persistent& GetInteger (Standard_Integer& theValue) const;
170 const BinObjMgt_Persistent& operator >> (Standard_Integer& theValue) const
171 {
172   return GetInteger(theValue);
173 }
174   
175     const BinObjMgt_Persistent& GetBoolean (Standard_Boolean& theValue) const;
176   const BinObjMgt_Persistent& operator >> (Standard_Boolean& theValue) const
177 {
178   return GetBoolean(theValue);
179 }
180   
181   Standard_EXPORT const BinObjMgt_Persistent& GetReal (Standard_Real& theValue) const;
182 const BinObjMgt_Persistent& operator >> (Standard_Real& theValue) const
183 {
184   return GetReal(theValue);
185 }
186   
187   Standard_EXPORT const BinObjMgt_Persistent& GetShortReal (Standard_ShortReal& theValue) const;
188 const BinObjMgt_Persistent& operator >> (Standard_ShortReal& theValue) const
189 {
190   return GetShortReal(theValue);
191 }
192   
193   Standard_EXPORT const BinObjMgt_Persistent& GetAsciiString (TCollection_AsciiString& theValue) const;
194 const BinObjMgt_Persistent& operator >> (TCollection_AsciiString& theValue) const
195 {
196   return GetAsciiString(theValue);
197 }
198   
199   Standard_EXPORT const BinObjMgt_Persistent& GetExtendedString (TCollection_ExtendedString& theValue) const;
200 const BinObjMgt_Persistent& operator >> (TCollection_ExtendedString& theValue) const
201 {
202   return GetExtendedString(theValue);
203 }
204   
205   Standard_EXPORT const BinObjMgt_Persistent& GetLabel (const Handle(TDF_Data)& theDS, TDF_Label& theValue) const;
206   
207   Standard_EXPORT const BinObjMgt_Persistent& GetGUID (Standard_GUID& theValue) const;
208 const BinObjMgt_Persistent& operator >> (Standard_GUID& theValue) const
209 {
210   return GetGUID(theValue);
211 }
212   
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;
217   
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;
222   
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;
227   
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;
232   
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;
237   
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;
242   
243   //! Tells the current position for get/put
244     Standard_Integer Position() const;
245   
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;
250   
251   //! Truncates the buffer by current position,
252   //! i.e. updates mySize
253     void Truncate();
254   
255   //! Indicates an error after Get methods or SetPosition
256     Standard_Boolean IsError() const;
257   Standard_Boolean operator !() const
258   {
259     return IsError();
260   }
261   
262   //! Indicates a good state after Get methods or SetPosition
263     Standard_Boolean IsOK() const;
264   operator Standard_Boolean () const { return IsOK(); }
265   
266   //! Initializes me to reuse again
267   Standard_EXPORT void Init();
268   
269   //! Sets the Id of the object
270     void SetId (const Standard_Integer theId);
271   
272   //! Sets the Id of the type of the object
273     void SetTypeId (const Standard_Integer theId);
274   
275   //! Returns the Id of the object
276     Standard_Integer Id() const;
277   
278   //! Returns the Id of the type of the object
279     Standard_Integer TypeId() const;
280   
281   //! Returns the length of data
282     Standard_Integer Length() const;
283   
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);
289   
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);
294   
295   //! Frees the allocated memory;
296   //! This object can be reused after call to Init
297   Standard_EXPORT void Destroy();
298   ~BinObjMgt_Persistent()
299   {
300     Destroy();
301   }
302
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; }
315
316 private:
317
318   
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;
323   
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);
328   
329   //! Allocates theNbPieces more pieces
330   Standard_EXPORT void incrementData (const Standard_Integer theNbPieces);
331   
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;
335   
336   //! Puts theLength bytes from theArray
337   Standard_EXPORT void putArray (const Standard_Address theArray, const Standard_Integer theSize);
338   
339   //! Gets theLength bytes into theArray
340   Standard_EXPORT void getArray (const Standard_Address theArray, const Standard_Integer theSize) const;
341   
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);
344   
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);
347   
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);
350   
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);
353
354
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
364 };
365
366
367 #include <BinObjMgt_Persistent.lxx>
368
369
370
371
372
373 #endif // _BinObjMgt_Persistent_HeaderFile