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