1 // File: BinObjMgt_Persistent.lxx
2 // Created: 30.10.02 10:20:14
3 // Author: Michael SAZONOV
4 // Copyright: Open CASCADE 2002
6 #define BP_HEADSIZE ((Standard_Integer)(3 * sizeof(Standard_Integer)))
7 #define BP_PIECESIZE 102400
9 //=======================================================================
11 //purpose : Sets the Id of the object
12 //=======================================================================
14 inline void BinObjMgt_Persistent::SetId (const Standard_Integer theId)
16 ((Standard_Integer*) myData(1)) [1] = theId;
19 //=======================================================================
20 //function : SetTypeId
21 //purpose : Sets the Id of the type of the object
22 //=======================================================================
24 inline void BinObjMgt_Persistent::SetTypeId (const Standard_Integer theTypeId)
26 ((Standard_Integer*) myData(1)) [0] = theTypeId;
29 //=======================================================================
31 //purpose : Returns the Id of the object
32 //=======================================================================
34 inline Standard_Integer BinObjMgt_Persistent::Id () const
36 return ((Standard_Integer*) myData(1)) [1];
39 //=======================================================================
41 //purpose : Returns the Id of the type of the object
42 //=======================================================================
44 inline Standard_Integer BinObjMgt_Persistent::TypeId () const
46 return ((Standard_Integer*) myData(1)) [0];
49 //=======================================================================
51 //purpose : Returns the length of data
52 //=======================================================================
54 inline Standard_Integer BinObjMgt_Persistent::Length () const
56 return mySize - BP_HEADSIZE;
59 //=======================================================================
60 //function : operator <<
62 //=======================================================================
64 inline Standard_OStream& operator << (Standard_OStream& theOS,
65 BinObjMgt_Persistent& theObj)
67 return theObj.Write (theOS);
70 //=======================================================================
71 //function : operator >>
73 //=======================================================================
75 inline Standard_IStream& operator >> (Standard_IStream& theIS,
76 BinObjMgt_Persistent& theObj)
78 return theObj.Read (theIS);
81 //=======================================================================
83 //purpose : Tells the current position for get/put
84 //=======================================================================
86 inline Standard_Integer BinObjMgt_Persistent::Position() const
88 return (myIndex-1) * BP_PIECESIZE + myOffset;
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 //=======================================================================
98 inline Standard_Boolean BinObjMgt_Persistent::SetPosition
99 (const Standard_Integer thePos) const
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;
107 //=======================================================================
108 //function : Truncate
109 //purpose : Truncates the buffer by current position,
110 // i.e. updates mySize
111 //=======================================================================
113 inline void BinObjMgt_Persistent::Truncate()
118 //=======================================================================
120 //purpose : Indicates an error after Get methods or SetPosition
121 //=======================================================================
123 inline Standard_Boolean BinObjMgt_Persistent::IsError() const
128 //=======================================================================
130 //purpose : Indicates a good state after Get methods or SetPosition
131 //=======================================================================
133 inline Standard_Boolean BinObjMgt_Persistent::IsOK() const
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 //=======================================================================
145 inline void BinObjMgt_Persistent::alignOffset
146 (const Standard_Integer theSize,
147 const Standard_Boolean toClear) const
149 unsigned alignMask = theSize - 1;
150 Standard_Integer anOffset = (myOffset + alignMask) & ~alignMask;
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;
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;
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 //=======================================================================
172 inline Standard_Integer BinObjMgt_Persistent::prepareForPut
173 (const Standard_Integer theSize)
175 Standard_Integer nbPieces = (myOffset + theSize - 1) / BP_PIECESIZE;
176 Standard_Integer nbToAdd = myIndex + nbPieces - myData.Length();
178 // create needed pieces
179 incrementData (nbToAdd);
180 Standard_Integer aNewPosition = Position() + theSize;
181 if (aNewPosition > mySize) mySize = aNewPosition;
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 //=======================================================================
191 inline Standard_Boolean BinObjMgt_Persistent::noMoreData
192 (const Standard_Integer theSize) const
194 ((BinObjMgt_Persistent*)this)->myIsError = Position() + theSize > mySize;
198 //=======================================================================
199 //function : PutBoolean
201 //=======================================================================
203 inline BinObjMgt_Persistent& BinObjMgt_Persistent::PutBoolean
204 (const Standard_Boolean theValue)
206 return PutInteger ((Standard_Integer) theValue);
209 //=======================================================================
210 //function : GetBoolean
212 //=======================================================================
214 inline const BinObjMgt_Persistent& BinObjMgt_Persistent::GetBoolean
215 (Standard_Boolean& theValue) const
217 // Standard_Integer anIntVal = (Standard_Integer) theValue;
218 Standard_Integer anIntVal;
219 GetInteger (anIntVal);
220 theValue = (Standard_Boolean) anIntVal;