Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-12 09:16:30

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 #ifndef _BinObjMgt_Persistent_HeaderFile
0017 #define _BinObjMgt_Persistent_HeaderFile
0018 
0019 #include <Standard.hxx>
0020 #include <Standard_DefineAlloc.hxx>
0021 #include <Standard_Handle.hxx>
0022 
0023 #include <NCollection_Sequence.hxx>
0024 #include <Standard_Integer.hxx>
0025 #include <Standard_ShortReal.hxx>
0026 #include <BinObjMgt_PChar.hxx>
0027 #include <BinObjMgt_PByte.hxx>
0028 #include <BinObjMgt_PExtChar.hxx>
0029 #include <BinObjMgt_PInteger.hxx>
0030 #include <BinObjMgt_PReal.hxx>
0031 #include <BinObjMgt_PShortReal.hxx>
0032 #include <BinObjMgt_Position.hxx>
0033 #include <Standard_OStream.hxx>
0034 #include <Standard_IStream.hxx>
0035 class TCollection_AsciiString;
0036 class TCollection_ExtendedString;
0037 class TDF_Label;
0038 class Standard_GUID;
0039 class TDF_Data;
0040 
0041 //! Binary persistent representation of an object.
0042 //! Really it is used as a buffer for read/write an object.
0043 //!
0044 //! It takes care of Little/Big endian by inversing bytes
0045 //! in objects of standard types (see FSD_FileHeader.hxx
0046 //! for the default value of DO_INVERSE).
0047 class BinObjMgt_Persistent
0048 {
0049 public:
0050   DEFINE_STANDARD_ALLOC
0051 
0052   //! Empty constructor
0053   Standard_EXPORT BinObjMgt_Persistent();
0054 
0055   Standard_EXPORT BinObjMgt_Persistent& PutCharacter(const char theValue);
0056 
0057   BinObjMgt_Persistent& operator<<(const char theValue) { return PutCharacter(theValue); }
0058 
0059   Standard_EXPORT BinObjMgt_Persistent& PutByte(const uint8_t theValue);
0060 
0061   BinObjMgt_Persistent& operator<<(const uint8_t theValue) { return PutByte(theValue); }
0062 
0063   Standard_EXPORT BinObjMgt_Persistent& PutExtCharacter(const char16_t theValue);
0064 
0065   BinObjMgt_Persistent& operator<<(const char16_t theValue) { return PutExtCharacter(theValue); }
0066 
0067   Standard_EXPORT BinObjMgt_Persistent& PutInteger(const int theValue);
0068 
0069   BinObjMgt_Persistent& operator<<(const int theValue) { return PutInteger(theValue); }
0070 
0071   BinObjMgt_Persistent& PutBoolean(const bool theValue);
0072 
0073   BinObjMgt_Persistent& operator<<(const bool theValue) { return PutBoolean(theValue); }
0074 
0075   Standard_EXPORT BinObjMgt_Persistent& PutReal(const double theValue);
0076 
0077   BinObjMgt_Persistent& operator<<(const double theValue) { return PutReal(theValue); }
0078 
0079   Standard_EXPORT BinObjMgt_Persistent& PutShortReal(const float theValue);
0080 
0081   BinObjMgt_Persistent& operator<<(const float theValue) { return PutShortReal(theValue); }
0082 
0083   //! Offset in output buffer is not aligned
0084   Standard_EXPORT BinObjMgt_Persistent& PutCString(const char* const theValue);
0085 
0086   BinObjMgt_Persistent& operator<<(const char* const theValue) { return PutCString(theValue); }
0087 
0088   //! Offset in output buffer is word-aligned
0089   Standard_EXPORT BinObjMgt_Persistent& PutAsciiString(const TCollection_AsciiString& theValue);
0090 
0091   BinObjMgt_Persistent& operator<<(const TCollection_AsciiString& theValue)
0092   {
0093     return PutAsciiString(theValue);
0094   }
0095 
0096   //! Offset in output buffer is word-aligned
0097   Standard_EXPORT BinObjMgt_Persistent& PutExtendedString(
0098     const TCollection_ExtendedString& theValue);
0099 
0100   BinObjMgt_Persistent& operator<<(const TCollection_ExtendedString& theValue)
0101   {
0102     return PutExtendedString(theValue);
0103   }
0104 
0105   Standard_EXPORT BinObjMgt_Persistent& PutLabel(const TDF_Label& theValue);
0106 
0107   BinObjMgt_Persistent& operator<<(const TDF_Label& theValue) { return PutLabel(theValue); }
0108 
0109   Standard_EXPORT BinObjMgt_Persistent& PutGUID(const Standard_GUID& theValue);
0110 
0111   BinObjMgt_Persistent& operator<<(const Standard_GUID& theValue) { return PutGUID(theValue); }
0112 
0113   //! Put C array of char, theLength is the number of elements
0114   Standard_EXPORT BinObjMgt_Persistent& PutCharArray(const BinObjMgt_PChar theArray,
0115                                                      const int             theLength);
0116 
0117   //! Put C array of unsigned chars, theLength is the number of elements
0118   Standard_EXPORT BinObjMgt_Persistent& PutByteArray(const BinObjMgt_PByte theArray,
0119                                                      const int             theLength);
0120 
0121   //! Put C array of ExtCharacter, theLength is the number of elements
0122   Standard_EXPORT BinObjMgt_Persistent& PutExtCharArray(const BinObjMgt_PExtChar theArray,
0123                                                         const int                theLength);
0124 
0125   //! Put C array of int, theLength is the number of elements
0126   Standard_EXPORT BinObjMgt_Persistent& PutIntArray(const BinObjMgt_PInteger theArray,
0127                                                     const int                theLength);
0128 
0129   //! Put C array of double, theLength is the number of elements
0130   Standard_EXPORT BinObjMgt_Persistent& PutRealArray(const BinObjMgt_PReal theArray,
0131                                                      const int             theLength);
0132 
0133   //! Put C array of float, theLength is the number of elements
0134   Standard_EXPORT BinObjMgt_Persistent& PutShortRealArray(const BinObjMgt_PShortReal theArray,
0135                                                           const int                  theLength);
0136 
0137   Standard_EXPORT const BinObjMgt_Persistent& GetCharacter(char& theValue) const;
0138 
0139   const BinObjMgt_Persistent& operator>>(char& theValue) const { return GetCharacter(theValue); }
0140 
0141   Standard_EXPORT const BinObjMgt_Persistent& GetByte(uint8_t& theValue) const;
0142 
0143   const BinObjMgt_Persistent& operator>>(uint8_t& theValue) const { return GetByte(theValue); }
0144 
0145   Standard_EXPORT const BinObjMgt_Persistent& GetExtCharacter(char16_t& theValue) const;
0146 
0147   const BinObjMgt_Persistent& operator>>(char16_t& theValue) const
0148   {
0149     return GetExtCharacter(theValue);
0150   }
0151 
0152   Standard_EXPORT const BinObjMgt_Persistent& GetInteger(int& theValue) const;
0153 
0154   const BinObjMgt_Persistent& operator>>(int& theValue) const { return GetInteger(theValue); }
0155 
0156   const BinObjMgt_Persistent& GetBoolean(bool& theValue) const;
0157 
0158   const BinObjMgt_Persistent& operator>>(bool& theValue) const { return GetBoolean(theValue); }
0159 
0160   Standard_EXPORT const BinObjMgt_Persistent& GetReal(double& theValue) const;
0161 
0162   const BinObjMgt_Persistent& operator>>(double& theValue) const { return GetReal(theValue); }
0163 
0164   Standard_EXPORT const BinObjMgt_Persistent& GetShortReal(float& theValue) const;
0165 
0166   const BinObjMgt_Persistent& operator>>(float& theValue) const { return GetShortReal(theValue); }
0167 
0168   Standard_EXPORT const BinObjMgt_Persistent& GetAsciiString(
0169     TCollection_AsciiString& theValue) const;
0170 
0171   const BinObjMgt_Persistent& operator>>(TCollection_AsciiString& theValue) const
0172   {
0173     return GetAsciiString(theValue);
0174   }
0175 
0176   Standard_EXPORT const BinObjMgt_Persistent& GetExtendedString(
0177     TCollection_ExtendedString& theValue) const;
0178 
0179   const BinObjMgt_Persistent& operator>>(TCollection_ExtendedString& theValue) const
0180   {
0181     return GetExtendedString(theValue);
0182   }
0183 
0184   Standard_EXPORT const BinObjMgt_Persistent& GetLabel(const occ::handle<TDF_Data>& theDS,
0185                                                        TDF_Label&                   theValue) const;
0186 
0187   Standard_EXPORT const BinObjMgt_Persistent& GetGUID(Standard_GUID& theValue) const;
0188 
0189   const BinObjMgt_Persistent& operator>>(Standard_GUID& theValue) const
0190   {
0191     return GetGUID(theValue);
0192   }
0193 
0194   //! Get C array of char, theLength is the number of elements;
0195   //! theArray must point to a
0196   //! space enough to place theLength elements
0197   Standard_EXPORT const BinObjMgt_Persistent& GetCharArray(const BinObjMgt_PChar theArray,
0198                                                            const int             theLength) const;
0199 
0200   //! Get C array of unsigned chars, theLength is the number of elements;
0201   //! theArray must point to a
0202   //! space enough to place theLength elements
0203   Standard_EXPORT const BinObjMgt_Persistent& GetByteArray(const BinObjMgt_PByte theArray,
0204                                                            const int             theLength) const;
0205 
0206   //! Get C array of ExtCharacter, theLength is the number of elements;
0207   //! theArray must point to a
0208   //! space enough to place theLength elements
0209   Standard_EXPORT const BinObjMgt_Persistent& GetExtCharArray(const BinObjMgt_PExtChar theArray,
0210                                                               const int theLength) const;
0211 
0212   //! Get C array of int, theLength is the number of elements;
0213   //! theArray must point to a
0214   //! space enough to place theLength elements
0215   Standard_EXPORT const BinObjMgt_Persistent& GetIntArray(const BinObjMgt_PInteger theArray,
0216                                                           const int                theLength) const;
0217 
0218   //! Get C array of double, theLength is the number of elements;
0219   //! theArray must point to a
0220   //! space enough to place theLength elements
0221   Standard_EXPORT const BinObjMgt_Persistent& GetRealArray(const BinObjMgt_PReal theArray,
0222                                                            const int             theLength) const;
0223 
0224   //! Get C array of float, theLength is the number of elements;
0225   //! theArray must point to a
0226   //! space enough to place theLength elements
0227   Standard_EXPORT const BinObjMgt_Persistent& GetShortRealArray(const BinObjMgt_PShortReal theArray,
0228                                                                 const int theLength) const;
0229 
0230   //! Tells the current position for get/put
0231   int Position() const;
0232 
0233   //! Sets the current position for get/put.
0234   //! Resets an error state depending on the validity of thePos.
0235   //! Returns the new state (value of IsOK())
0236   bool SetPosition(const int thePos) const;
0237 
0238   //! Truncates the buffer by current position,
0239   //! i.e. updates mySize
0240   void Truncate();
0241 
0242   //! Indicates an error after Get methods or SetPosition
0243   bool IsError() const;
0244 
0245   bool operator!() const { return IsError(); }
0246 
0247   //! Indicates a good state after Get methods or SetPosition
0248   bool IsOK() const;
0249 
0250   operator bool() const { return IsOK(); }
0251 
0252   //! Initializes me to reuse again
0253   Standard_EXPORT void Init();
0254 
0255   //! Sets the Id of the object
0256   void SetId(const int theId);
0257 
0258   //! Sets the Id of the type of the object
0259   void SetTypeId(const int theId);
0260 
0261   //! Returns the Id of the object
0262   int Id() const;
0263 
0264   //! Returns the Id of the type of the object
0265   int TypeId() const;
0266 
0267   //! Returns the length of data
0268   int Length() const;
0269 
0270   //! Stores <me> to the stream.
0271   //! inline Standard_OStream& operator<< (Standard_OStream&,
0272   //! BinObjMgt_Persistent&) is also available.
0273   //! If theDirectStream is true, after this data the direct stream data is stored.
0274   Standard_EXPORT Standard_OStream& Write(Standard_OStream& theOS,
0275                                           const bool        theDirectStream = false);
0276 
0277   //! Retrieves <me> from the stream.
0278   //! inline Standard_IStream& operator>> (Standard_IStream&,
0279   //! BinObjMgt_Persistent&) is also available
0280   Standard_EXPORT Standard_IStream& Read(Standard_IStream& theIS);
0281 
0282   //! Frees the allocated memory;
0283   //! This object can be reused after call to Init
0284   Standard_EXPORT void Destroy();
0285 
0286   ~BinObjMgt_Persistent() { Destroy(); }
0287 
0288   //! Sets the stream for direct writing
0289   Standard_EXPORT void SetOStream(Standard_OStream& theStream) { myOStream = &theStream; }
0290 
0291   //! Sets the stream for direct reading
0292   Standard_EXPORT void SetIStream(Standard_IStream& theStream) { myIStream = &theStream; }
0293 
0294   //! Gets the stream for and enables direct writing
0295   Standard_EXPORT Standard_OStream* GetOStream();
0296   //! Gets the stream for and enables direct reading
0297   Standard_EXPORT Standard_IStream* GetIStream();
0298 
0299   //! Returns true if after this record a direct writing to the stream is performed.
0300   Standard_EXPORT bool IsDirect() { return myDirectWritingIsEnabled; }
0301 
0302   //! Returns the start position of the direct writing in the stream
0303   Standard_EXPORT occ::handle<BinObjMgt_Position> StreamStart() { return myStreamStart; }
0304 
0305 private:
0306   //! Aligns myOffset to the given size;
0307   //! enters the next piece if the end of the current one is reached;
0308   //! toClear==true means to fill unused space by 0
0309   void alignOffset(const int theSize, const bool toClear = false) const;
0310 
0311   //! Prepares the room for theSize bytes;
0312   //! returns the number of pieces except for the current one
0313   //! are to be occupied
0314   int prepareForPut(const int theSize);
0315 
0316   //! Allocates theNbPieces more pieces
0317   Standard_EXPORT void incrementData(const int theNbPieces);
0318 
0319   //! Checks if there is no more data of the given size starting
0320   //! from the current position in myData
0321   bool noMoreData(const int theSize) const;
0322 
0323   //! Puts theLength bytes from theArray
0324   Standard_EXPORT void putArray(void* const theArray, const int theSize);
0325 
0326   //! Gets theLength bytes into theArray
0327   Standard_EXPORT void getArray(void* const theArray, const int theSize) const;
0328 
0329   //! Inverses bytes in the data addressed by the given values
0330   Standard_EXPORT void inverseExtCharData(const int theIndex,
0331                                           const int theOffset,
0332                                           const int theSize);
0333 
0334   //! Inverses bytes in the data addressed by the given values
0335   Standard_EXPORT void inverseIntData(const int theIndex, const int theOffset, const int theSize);
0336 
0337   //! Inverses bytes in the data addressed by the given values
0338   Standard_EXPORT void inverseRealData(const int theIndex, const int theOffset, const int theSize);
0339 
0340   //! Inverses bytes in the data addressed by the given values
0341   Standard_EXPORT void inverseShortRealData(const int theIndex,
0342                                             const int theOffset,
0343                                             const int theSize);
0344 
0345   NCollection_Sequence<void*> myData;
0346   int                         myIndex;
0347   int                         myOffset;
0348   int                         mySize;
0349   bool                        myIsError;
0350   Standard_OStream*           myOStream; ///< stream to write in case direct writing is enabled
0351   Standard_IStream*           myIStream; ///< stream to write in case direct reading is enabled
0352   bool                        myDirectWritingIsEnabled;
0353   // clang-format off
0354   occ::handle<BinObjMgt_Position> myStreamStart; ///< position where the direct writing to the script is started
0355   // clang-format on
0356 };
0357 
0358 #include <BinObjMgt_Persistent.lxx>
0359 
0360 #endif // _BinObjMgt_Persistent_HeaderFile