summaryrefslogtreecommitdiff
path: root/inc/BinObjMgt_Persistent.lxx
blob: e567f292925020db20a88d18a2b014d9e2a20ba4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// File:      BinObjMgt_Persistent.lxx
// Created:   30.10.02 10:20:14
// Author:    Michael SAZONOV
// Copyright: Open CASCADE 2002

#define BP_HEADSIZE        ((Standard_Integer)(3 * sizeof(Standard_Integer)))
#define BP_PIECESIZE       102400

//=======================================================================
//function : SetId
//purpose  : Sets the Id of the object
//=======================================================================

inline void BinObjMgt_Persistent::SetId (const Standard_Integer theId)
{
  ((Standard_Integer*) myData(1)) [1] = theId;
}

//=======================================================================
//function : SetTypeId
//purpose  : Sets the Id of the type of the object
//=======================================================================

inline void BinObjMgt_Persistent::SetTypeId (const Standard_Integer theTypeId)
{
  ((Standard_Integer*) myData(1)) [0] = theTypeId;
}

//=======================================================================
//function : Id
//purpose  : Returns the Id of the object
//=======================================================================

inline Standard_Integer BinObjMgt_Persistent::Id () const
{
  return ((Standard_Integer*) myData(1)) [1];
}

//=======================================================================
//function : TypeId
//purpose  : Returns the Id of the type of the object
//=======================================================================

inline Standard_Integer BinObjMgt_Persistent::TypeId () const
{
  return ((Standard_Integer*) myData(1)) [0];
}

//=======================================================================
//function : Length
//purpose  : Returns the length of data
//=======================================================================

inline Standard_Integer BinObjMgt_Persistent::Length () const
{
  return mySize - BP_HEADSIZE;
}

//=======================================================================
//function : operator <<
//purpose  : 
//=======================================================================

inline Standard_OStream& operator << (Standard_OStream& theOS,
				      BinObjMgt_Persistent& theObj)
{
  return theObj.Write (theOS);
}

//=======================================================================
//function : operator >>
//purpose  : 
//=======================================================================

inline Standard_IStream& operator >> (Standard_IStream& theIS,
				      BinObjMgt_Persistent& theObj)
{
  return theObj.Read (theIS);
}

//=======================================================================
//function : Position
//purpose  : Tells the current position for get/put
//=======================================================================

inline Standard_Integer BinObjMgt_Persistent::Position() const
{
  return (myIndex-1) * BP_PIECESIZE + myOffset;
}

//=======================================================================
//function : SetPosition
//purpose  : Sets the current position for get/put.
//           Resets an error state depending on the validity of thePos.
//           Returns the new state (value of IsOK())
//=======================================================================

inline Standard_Boolean BinObjMgt_Persistent::SetPosition
  (const Standard_Integer thePos) const
{
  ((BinObjMgt_Persistent*)this)->myIndex = thePos / BP_PIECESIZE + 1;
  ((BinObjMgt_Persistent*)this)->myOffset = thePos % BP_PIECESIZE;
  ((BinObjMgt_Persistent*)this)->myIsError = thePos > mySize || thePos < BP_HEADSIZE;
  return !myIsError;
}

//=======================================================================
//function : Truncate
//purpose  : Truncates the buffer by current position,
//           i.e. updates mySize
//=======================================================================

inline void BinObjMgt_Persistent::Truncate()
{
  mySize = Position();
}

//=======================================================================
//function : IsError
//purpose  : Indicates an error after Get methods or SetPosition
//=======================================================================

inline Standard_Boolean BinObjMgt_Persistent::IsError() const
{
  return myIsError;
}

//=======================================================================
//function : IsOK
//purpose  : Indicates a good state after Get methods or SetPosition
//=======================================================================

inline Standard_Boolean BinObjMgt_Persistent::IsOK() const
{
  return !myIsError;
}

//=======================================================================
//function : alignOffset
//purpose  : Aligns myOffset to the given size;
//           enters the next piece if the end of the current one is reached;
//           toClear==true means to fill unused space by 0
//=======================================================================

inline void BinObjMgt_Persistent::alignOffset
  (const Standard_Integer theSize,
   const Standard_Boolean toClear) const
{
  unsigned alignMask = theSize - 1;
  Standard_Integer anOffset = (myOffset + alignMask) & ~alignMask;

  if (anOffset > myOffset) {
    if (toClear && anOffset <= BP_PIECESIZE)
      memset ( ((char*)myData(myIndex)) + myOffset, 0, anOffset - myOffset);
    ((BinObjMgt_Persistent*)this)->myOffset = anOffset;
  }

  // ensure there is a room for at least one item in the current piece
  if (myOffset >= BP_PIECESIZE) {
    ((BinObjMgt_Persistent*)this)->myIndex++;
    ((BinObjMgt_Persistent*)this)->myOffset = 0;
  }
}

//=======================================================================
//function : prepareForPut
//purpose  : Prepares the room for theSize bytes;
//           returns the number of pieces except for the current one
//           are to be occupied
//=======================================================================

inline Standard_Integer BinObjMgt_Persistent::prepareForPut
  (const Standard_Integer theSize)
{
  Standard_Integer nbPieces = (myOffset + theSize - 1) / BP_PIECESIZE;
  Standard_Integer nbToAdd = myIndex + nbPieces - myData.Length();
  if (nbToAdd > 0)
    // create needed pieces
    incrementData (nbToAdd);
  Standard_Integer aNewPosition = Position() + theSize;
  if (aNewPosition > mySize) mySize = aNewPosition;
  return nbPieces;
}

//=======================================================================
//function : noMoreData
//purpose  : Checks if there is no more data of the given size starting
//           from the current position in myData
//=======================================================================

inline Standard_Boolean BinObjMgt_Persistent::noMoreData
  (const Standard_Integer theSize) const
{
  ((BinObjMgt_Persistent*)this)->myIsError = Position() + theSize > mySize;
  return myIsError;
}

//=======================================================================
//function : PutBoolean
//purpose  : 
//=======================================================================

inline BinObjMgt_Persistent& BinObjMgt_Persistent::PutBoolean
  (const Standard_Boolean theValue)
{
  return PutInteger ((Standard_Integer) theValue);
}

//=======================================================================
//function : GetBoolean
//purpose  : 
//=======================================================================

inline const BinObjMgt_Persistent& BinObjMgt_Persistent::GetBoolean
  (Standard_Boolean& theValue) const
{
//  Standard_Integer anIntVal = (Standard_Integer) theValue;
  Standard_Integer anIntVal;
  GetInteger (anIntVal);
  theValue = (Standard_Boolean) anIntVal;
  return *this;
}