Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/opencascade/BinObjMgt_Persistent.lxx is written in an unsupported language. File is not indexed.

0001 // Created on: 2002-10-30
0002 // Created by: Michael SAZONOV
0003 // Copyright (c) 2002-2014 OPEN CASCADE SAS
0004 //
0005 // This file is part of Open CASCADE Technology software library.
0006 //
0007 // This library is free software; you can redistribute it and/or modify it under
0008 // the terms of the GNU Lesser General Public License version 2.1 as published
0009 // by the Free Software Foundation, with special exception defined in the file
0010 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0011 // distribution for complete text of the license and disclaimer of any warranty.
0012 //
0013 // Alternatively, this file may be used under the terms of Open CASCADE
0014 // commercial license or contractual agreement.
0015 
0016 #define BP_HEADSIZE ((int)(3 * sizeof(int)))
0017 #define BP_PIECESIZE 102400
0018 
0019 //=================================================================================================
0020 
0021 inline void BinObjMgt_Persistent::SetId(const int theId)
0022 {
0023   ((int*)myData(1))[1] = theId;
0024 }
0025 
0026 //=======================================================================
0027 // function : SetTypeId
0028 // purpose  : Sets the Id of the type of the object
0029 //=======================================================================
0030 
0031 inline void BinObjMgt_Persistent::SetTypeId(const int theTypeId)
0032 {
0033   ((int*)myData(1))[0] = theTypeId;
0034   myStreamStart.Nullify();
0035 }
0036 
0037 //=================================================================================================
0038 
0039 inline int BinObjMgt_Persistent::Id() const
0040 {
0041   return ((int*)myData(1))[1];
0042 }
0043 
0044 //=======================================================================
0045 // function : TypeId
0046 // purpose  : Returns the Id of the type of the object
0047 //=======================================================================
0048 
0049 inline int BinObjMgt_Persistent::TypeId() const
0050 {
0051   return ((int*)myData(1))[0];
0052 }
0053 
0054 //=================================================================================================
0055 
0056 inline int BinObjMgt_Persistent::Length() const
0057 {
0058   return mySize - BP_HEADSIZE;
0059 }
0060 
0061 //=================================================================================================
0062 
0063 inline Standard_OStream& operator<<(Standard_OStream& theOS, BinObjMgt_Persistent& theObj)
0064 {
0065   return theObj.Write(theOS);
0066 }
0067 
0068 //=================================================================================================
0069 
0070 inline Standard_IStream& operator>>(Standard_IStream& theIS, BinObjMgt_Persistent& theObj)
0071 {
0072   return theObj.Read(theIS);
0073 }
0074 
0075 //=======================================================================
0076 // function : Position
0077 // purpose  : Tells the current position for get/put
0078 //=======================================================================
0079 
0080 inline int BinObjMgt_Persistent::Position() const
0081 {
0082   return (myIndex - 1) * BP_PIECESIZE + myOffset;
0083 }
0084 
0085 //=======================================================================
0086 // function : SetPosition
0087 // purpose  : Sets the current position for get/put.
0088 //           Resets an error state depending on the validity of thePos.
0089 //           Returns the new state (value of IsOK())
0090 //=======================================================================
0091 
0092 inline bool BinObjMgt_Persistent::SetPosition(const int thePos) const
0093 {
0094   ((BinObjMgt_Persistent*)this)->myIndex   = thePos / BP_PIECESIZE + 1;
0095   ((BinObjMgt_Persistent*)this)->myOffset  = thePos % BP_PIECESIZE;
0096   ((BinObjMgt_Persistent*)this)->myIsError = thePos > mySize || thePos < BP_HEADSIZE;
0097   return !myIsError;
0098 }
0099 
0100 //=======================================================================
0101 // function : Truncate
0102 // purpose  : Truncates the buffer by current position,
0103 //           i.e. updates mySize
0104 //=======================================================================
0105 
0106 inline void BinObjMgt_Persistent::Truncate()
0107 {
0108   mySize = Position();
0109 }
0110 
0111 //=======================================================================
0112 // function : IsError
0113 // purpose  : Indicates an error after Get methods or SetPosition
0114 //=======================================================================
0115 
0116 inline bool BinObjMgt_Persistent::IsError() const
0117 {
0118   return myIsError;
0119 }
0120 
0121 //=======================================================================
0122 // function : IsOK
0123 // purpose  : Indicates a good state after Get methods or SetPosition
0124 //=======================================================================
0125 
0126 inline bool BinObjMgt_Persistent::IsOK() const
0127 {
0128   return !myIsError;
0129 }
0130 
0131 //=======================================================================
0132 // function : alignOffset
0133 // purpose  : Aligns myOffset to the given size;
0134 //           enters the next piece if the end of the current one is reached;
0135 //           toClear==true means to fill unused space by 0
0136 //=======================================================================
0137 
0138 inline void BinObjMgt_Persistent::alignOffset(const int theSize, const bool toClear) const
0139 {
0140   unsigned alignMask = theSize - 1;
0141   int      anOffset  = (myOffset + alignMask) & ~alignMask;
0142 
0143   if (anOffset > myOffset)
0144   {
0145     if (toClear && anOffset <= BP_PIECESIZE)
0146       memset(((char*)myData(myIndex)) + myOffset, 0, anOffset - myOffset);
0147     ((BinObjMgt_Persistent*)this)->myOffset = anOffset;
0148   }
0149 
0150   // ensure there is a room for at least one item in the current piece
0151   if (myOffset >= BP_PIECESIZE)
0152   {
0153     ((BinObjMgt_Persistent*)this)->myIndex++;
0154     ((BinObjMgt_Persistent*)this)->myOffset = 0;
0155   }
0156 }
0157 
0158 //=======================================================================
0159 // function : prepareForPut
0160 // purpose  : Prepares the room for theSize bytes;
0161 //           returns the number of pieces except for the current one
0162 //           are to be occupied
0163 //=======================================================================
0164 
0165 inline int BinObjMgt_Persistent::prepareForPut(const int theSize)
0166 {
0167   int nbPieces = (myOffset + theSize - 1) / BP_PIECESIZE;
0168   int nbToAdd  = myIndex + nbPieces - myData.Length();
0169   if (nbToAdd > 0)
0170     // create needed pieces
0171     incrementData(nbToAdd);
0172   int aNewPosition = Position() + theSize;
0173   if (aNewPosition > mySize)
0174     mySize = aNewPosition;
0175   return nbPieces;
0176 }
0177 
0178 //=======================================================================
0179 // function : noMoreData
0180 // purpose  : Checks if there is no more data of the given size starting
0181 //           from the current position in myData
0182 //=======================================================================
0183 
0184 inline bool BinObjMgt_Persistent::noMoreData(const int theSize) const
0185 {
0186   ((BinObjMgt_Persistent*)this)->myIsError = Position() + theSize > mySize;
0187   return myIsError;
0188 }
0189 
0190 //=================================================================================================
0191 
0192 inline BinObjMgt_Persistent& BinObjMgt_Persistent::PutBoolean(const bool theValue)
0193 {
0194   return PutInteger((int)theValue);
0195 }
0196 
0197 //=================================================================================================
0198 
0199 inline const BinObjMgt_Persistent& BinObjMgt_Persistent::GetBoolean(bool& theValue) const
0200 {
0201   //  int anIntVal = (int) theValue;
0202   int anIntVal;
0203   GetInteger(anIntVal);
0204   theValue = anIntVal != 0;
0205   return *this;
0206 }