Integration of OCCT 6.5.0 from SVN
[occt.git] / src / BinObjMgt / BinObjMgt_Persistent.lxx
1 // File:      BinObjMgt_Persistent.lxx
2 // Created:   30.10.02 10:20:14
3 // Author:    Michael SAZONOV
4 // Copyright: Open CASCADE 2002
5
6 #define BP_HEADSIZE        ((Standard_Integer)(3 * sizeof(Standard_Integer)))
7 #define BP_PIECESIZE       102400
8
9 //=======================================================================
10 //function : SetId
11 //purpose  : Sets the Id of the object
12 //=======================================================================
13
14 inline void BinObjMgt_Persistent::SetId (const Standard_Integer theId)
15 {
16   ((Standard_Integer*) myData(1)) [1] = theId;
17 }
18
19 //=======================================================================
20 //function : SetTypeId
21 //purpose  : Sets the Id of the type of the object
22 //=======================================================================
23
24 inline void BinObjMgt_Persistent::SetTypeId (const Standard_Integer theTypeId)
25 {
26   ((Standard_Integer*) myData(1)) [0] = theTypeId;
27 }
28
29 //=======================================================================
30 //function : Id
31 //purpose  : Returns the Id of the object
32 //=======================================================================
33
34 inline Standard_Integer BinObjMgt_Persistent::Id () const
35 {
36   return ((Standard_Integer*) myData(1)) [1];
37 }
38
39 //=======================================================================
40 //function : TypeId
41 //purpose  : Returns the Id of the type of the object
42 //=======================================================================
43
44 inline Standard_Integer BinObjMgt_Persistent::TypeId () const
45 {
46   return ((Standard_Integer*) myData(1)) [0];
47 }
48
49 //=======================================================================
50 //function : Length
51 //purpose  : Returns the length of data
52 //=======================================================================
53
54 inline Standard_Integer BinObjMgt_Persistent::Length () const
55 {
56   return mySize - BP_HEADSIZE;
57 }
58
59 //=======================================================================
60 //function : operator <<
61 //purpose  : 
62 //=======================================================================
63
64 inline Standard_OStream& operator << (Standard_OStream& theOS,
65                                       BinObjMgt_Persistent& theObj)
66 {
67   return theObj.Write (theOS);
68 }
69
70 //=======================================================================
71 //function : operator >>
72 //purpose  : 
73 //=======================================================================
74
75 inline Standard_IStream& operator >> (Standard_IStream& theIS,
76                                       BinObjMgt_Persistent& theObj)
77 {
78   return theObj.Read (theIS);
79 }
80
81 //=======================================================================
82 //function : Position
83 //purpose  : Tells the current position for get/put
84 //=======================================================================
85
86 inline Standard_Integer BinObjMgt_Persistent::Position() const
87 {
88   return (myIndex-1) * BP_PIECESIZE + myOffset;
89 }
90
91 //=======================================================================
92 //function : SetPosition
93 //purpose  : Sets the current position for get/put.
94 //           Resets an error state depending on the validity of thePos.
95 //           Returns the new state (value of IsOK())
96 //=======================================================================
97
98 inline Standard_Boolean BinObjMgt_Persistent::SetPosition
99   (const Standard_Integer thePos) const
100 {
101   ((BinObjMgt_Persistent*)this)->myIndex = thePos / BP_PIECESIZE + 1;
102   ((BinObjMgt_Persistent*)this)->myOffset = thePos % BP_PIECESIZE;
103   ((BinObjMgt_Persistent*)this)->myIsError = thePos > mySize || thePos < BP_HEADSIZE;
104   return !myIsError;
105 }
106
107 //=======================================================================
108 //function : Truncate
109 //purpose  : Truncates the buffer by current position,
110 //           i.e. updates mySize
111 //=======================================================================
112
113 inline void BinObjMgt_Persistent::Truncate()
114 {
115   mySize = Position();
116 }
117
118 //=======================================================================
119 //function : IsError
120 //purpose  : Indicates an error after Get methods or SetPosition
121 //=======================================================================
122
123 inline Standard_Boolean BinObjMgt_Persistent::IsError() const
124 {
125   return myIsError;
126 }
127
128 //=======================================================================
129 //function : IsOK
130 //purpose  : Indicates a good state after Get methods or SetPosition
131 //=======================================================================
132
133 inline Standard_Boolean BinObjMgt_Persistent::IsOK() const
134 {
135   return !myIsError;
136 }
137
138 //=======================================================================
139 //function : alignOffset
140 //purpose  : Aligns myOffset to the given size;
141 //           enters the next piece if the end of the current one is reached;
142 //           toClear==true means to fill unused space by 0
143 //=======================================================================
144
145 inline void BinObjMgt_Persistent::alignOffset
146   (const Standard_Integer theSize,
147    const Standard_Boolean toClear) const
148 {
149   unsigned alignMask = theSize - 1;
150   Standard_Integer anOffset = (myOffset + alignMask) & ~alignMask;
151
152   if (anOffset > myOffset) {
153     if (toClear && anOffset <= BP_PIECESIZE)
154       memset ( ((char*)myData(myIndex)) + myOffset, 0, anOffset - myOffset);
155     ((BinObjMgt_Persistent*)this)->myOffset = anOffset;
156   }
157
158   // ensure there is a room for at least one item in the current piece
159   if (myOffset >= BP_PIECESIZE) {
160     ((BinObjMgt_Persistent*)this)->myIndex++;
161     ((BinObjMgt_Persistent*)this)->myOffset = 0;
162   }
163 }
164
165 //=======================================================================
166 //function : prepareForPut
167 //purpose  : Prepares the room for theSize bytes;
168 //           returns the number of pieces except for the current one
169 //           are to be occupied
170 //=======================================================================
171
172 inline Standard_Integer BinObjMgt_Persistent::prepareForPut
173   (const Standard_Integer theSize)
174 {
175   Standard_Integer nbPieces = (myOffset + theSize - 1) / BP_PIECESIZE;
176   Standard_Integer nbToAdd = myIndex + nbPieces - myData.Length();
177   if (nbToAdd > 0)
178     // create needed pieces
179     incrementData (nbToAdd);
180   Standard_Integer aNewPosition = Position() + theSize;
181   if (aNewPosition > mySize) mySize = aNewPosition;
182   return nbPieces;
183 }
184
185 //=======================================================================
186 //function : noMoreData
187 //purpose  : Checks if there is no more data of the given size starting
188 //           from the current position in myData
189 //=======================================================================
190
191 inline Standard_Boolean BinObjMgt_Persistent::noMoreData
192   (const Standard_Integer theSize) const
193 {
194   ((BinObjMgt_Persistent*)this)->myIsError = Position() + theSize > mySize;
195   return myIsError;
196 }
197
198 //=======================================================================
199 //function : PutBoolean
200 //purpose  : 
201 //=======================================================================
202
203 inline BinObjMgt_Persistent& BinObjMgt_Persistent::PutBoolean
204   (const Standard_Boolean theValue)
205 {
206   return PutInteger ((Standard_Integer) theValue);
207 }
208
209 //=======================================================================
210 //function : GetBoolean
211 //purpose  : 
212 //=======================================================================
213
214 inline const BinObjMgt_Persistent& BinObjMgt_Persistent::GetBoolean
215   (Standard_Boolean& theValue) const
216 {
217 //  Standard_Integer anIntVal = (Standard_Integer) theValue;
218   Standard_Integer anIntVal;
219   GetInteger (anIntVal);
220   theValue = (Standard_Boolean) anIntVal;
221   return *this;
222 }