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 #define BP_HEADSIZE ((Standard_Integer)(3 * sizeof(Standard_Integer)))
17 #define BP_PIECESIZE 102400
19 //=======================================================================
21 //purpose : Sets the Id of the object
22 //=======================================================================
24 inline void BinObjMgt_Persistent::SetId (const Standard_Integer theId)
26 ((Standard_Integer*) myData(1)) [1] = theId;
29 //=======================================================================
30 //function : SetTypeId
31 //purpose : Sets the Id of the type of the object
32 //=======================================================================
34 inline void BinObjMgt_Persistent::SetTypeId (const Standard_Integer theTypeId)
36 ((Standard_Integer*) myData(1)) [0] = theTypeId;
39 //=======================================================================
41 //purpose : Returns the Id of the object
42 //=======================================================================
44 inline Standard_Integer BinObjMgt_Persistent::Id () const
46 return ((Standard_Integer*) myData(1)) [1];
49 //=======================================================================
51 //purpose : Returns the Id of the type of the object
52 //=======================================================================
54 inline Standard_Integer BinObjMgt_Persistent::TypeId () const
56 return ((Standard_Integer*) myData(1)) [0];
59 //=======================================================================
61 //purpose : Returns the length of data
62 //=======================================================================
64 inline Standard_Integer BinObjMgt_Persistent::Length () const
66 return mySize - BP_HEADSIZE;
69 //=======================================================================
70 //function : operator <<
72 //=======================================================================
74 inline Standard_OStream& operator << (Standard_OStream& theOS,
75 BinObjMgt_Persistent& theObj)
77 return theObj.Write (theOS);
80 //=======================================================================
81 //function : operator >>
83 //=======================================================================
85 inline Standard_IStream& operator >> (Standard_IStream& theIS,
86 BinObjMgt_Persistent& theObj)
88 return theObj.Read (theIS);
91 //=======================================================================
93 //purpose : Tells the current position for get/put
94 //=======================================================================
96 inline Standard_Integer BinObjMgt_Persistent::Position() const
98 return (myIndex-1) * BP_PIECESIZE + myOffset;
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 //=======================================================================
108 inline Standard_Boolean BinObjMgt_Persistent::SetPosition
109 (const Standard_Integer thePos) const
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;
117 //=======================================================================
118 //function : Truncate
119 //purpose : Truncates the buffer by current position,
120 // i.e. updates mySize
121 //=======================================================================
123 inline void BinObjMgt_Persistent::Truncate()
128 //=======================================================================
130 //purpose : Indicates an error after Get methods or SetPosition
131 //=======================================================================
133 inline Standard_Boolean BinObjMgt_Persistent::IsError() const
138 //=======================================================================
140 //purpose : Indicates a good state after Get methods or SetPosition
141 //=======================================================================
143 inline Standard_Boolean BinObjMgt_Persistent::IsOK() const
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 //=======================================================================
155 inline void BinObjMgt_Persistent::alignOffset
156 (const Standard_Integer theSize,
157 const Standard_Boolean toClear) const
159 unsigned alignMask = theSize - 1;
160 Standard_Integer anOffset = (myOffset + alignMask) & ~alignMask;
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;
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;
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 //=======================================================================
182 inline Standard_Integer BinObjMgt_Persistent::prepareForPut
183 (const Standard_Integer theSize)
185 Standard_Integer nbPieces = (myOffset + theSize - 1) / BP_PIECESIZE;
186 Standard_Integer nbToAdd = myIndex + nbPieces - myData.Length();
188 // create needed pieces
189 incrementData (nbToAdd);
190 Standard_Integer aNewPosition = Position() + theSize;
191 if (aNewPosition > mySize) mySize = aNewPosition;
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 //=======================================================================
201 inline Standard_Boolean BinObjMgt_Persistent::noMoreData
202 (const Standard_Integer theSize) const
204 ((BinObjMgt_Persistent*)this)->myIsError = Position() + theSize > mySize;
208 //=======================================================================
209 //function : PutBoolean
211 //=======================================================================
213 inline BinObjMgt_Persistent& BinObjMgt_Persistent::PutBoolean
214 (const Standard_Boolean theValue)
216 return PutInteger ((Standard_Integer) theValue);
219 //=======================================================================
220 //function : GetBoolean
222 //=======================================================================
224 inline const BinObjMgt_Persistent& BinObjMgt_Persistent::GetBoolean
225 (Standard_Boolean& theValue) const
227 // Standard_Integer anIntVal = (Standard_Integer) theValue;
228 Standard_Integer anIntVal;
229 GetInteger (anIntVal);
230 theValue = (Standard_Boolean) anIntVal;