Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-14 10:36:35

0001 // Copyright (c) 2015 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _StdObjMgt_SharedObject_HeaderFile
0015 #define _StdObjMgt_SharedObject_HeaderFile
0016 
0017 #include <Standard_NoSuchObject.hxx>
0018 #include <StdObjMgt_Persistent.hxx>
0019 
0020 // NOLINTBEGIN(modernize-use-override)
0021 class StdObjMgt_SharedObject
0022 {
0023 public:
0024   template <class Transient>
0025   class AbstractPersistentBase : public Standard_Transient
0026   {
0027   public:
0028     virtual occ::handle<Transient> Import() const = 0;
0029   };
0030 
0031   template <class TransientT, class Base = StdObjMgt_Persistent>
0032   class SharedBase : public Base
0033   {
0034   public:
0035     //! Changes transient object
0036     inline void Transient(const occ::handle<TransientT>& theTransient)
0037     {
0038       myTransient = theTransient;
0039     }
0040 
0041     //! Import transient object from the persistent data.
0042     inline const occ::handle<TransientT>& Import() { return myTransient; }
0043 
0044   protected:
0045     occ::handle<TransientT> myTransient;
0046   };
0047 
0048   template <class Base, class Transient, class Persistent = AbstractPersistentBase<Transient>>
0049   class DelayedBase : public Base
0050   {
0051   public:
0052     typedef Transient  TransientBase;
0053     typedef Persistent PersistentBase;
0054 
0055     //! Import transient object from the persistent data.
0056     virtual occ::handle<Transient> Import() { return myTransient; }
0057 
0058   public:
0059     occ::handle<Transient> myTransient;
0060   };
0061 
0062   template <class Base, class PersistentData, class Transient = typename Base::TransientBase>
0063   class IgnoreData : public Base
0064   {
0065   public:
0066     //! Read persistent data from a file.
0067     virtual void Read(StdObjMgt_ReadData& theReadData) { PersistentData().Read(theReadData); }
0068 
0069     //! Write persistent data to a file.
0070     virtual void Write(StdObjMgt_WriteData& theWriteData) const
0071     {
0072       PersistentData().Write(theWriteData);
0073     }
0074 
0075     //! Gets persistent child objects
0076     virtual void PChildren(StdObjMgt_Persistent::SequenceOfPersistent& theChildren) const
0077     {
0078       PersistentData().PChildren(theChildren);
0079     }
0080 
0081     //! Returns persistent type name
0082     virtual const char* PName() const { return PersistentData().PName(); }
0083 
0084     //! Import transient object from the persistent data.
0085     virtual occ::handle<Transient> Import() { return nullptr; }
0086   };
0087 
0088 private:
0089   template <class Base>
0090   class delayedSubBase : public Base
0091   {
0092   public:
0093     //! Import transient object from the persistent data.
0094     virtual Handle(typename Base::TransientBase) Import()
0095     {
0096       if (Base::myTransient.IsNull() && !myPersistent.IsNull())
0097       {
0098         Base::myTransient = myPersistent->Import();
0099         myPersistent.Nullify();
0100       }
0101 
0102       return Base::myTransient;
0103     }
0104 
0105   public:
0106     Handle(typename Base::PersistentBase) myPersistent;
0107   };
0108 
0109 public:
0110   template <class Base, class Persistent = typename Base::PersistentBase>
0111   class Delayed : public delayedSubBase<Base>
0112   {
0113   private:
0114     template <class T1, class T2>
0115     struct DownCast
0116     {
0117       static occ::handle<T1> make(const occ::handle<T2>& theT2)
0118       {
0119         return occ::down_cast<T1>(theT2);
0120       }
0121     };
0122 
0123     template <class T>
0124     struct DownCast<T, T>
0125     {
0126       static occ::handle<T> make(const occ::handle<T>& theT) { return theT; }
0127     };
0128 
0129   public:
0130     //! Read persistent data from a file.
0131     virtual void Read(StdObjMgt_ReadData& theReadData)
0132     {
0133       occ::handle<Persistent> aPersistent = new Persistent;
0134       aPersistent->Read(theReadData);
0135       this->myPersistent = aPersistent;
0136     }
0137 
0138     //! Write persistent data to a file.
0139     virtual void Write(StdObjMgt_WriteData& theWriteData) const
0140     {
0141       occ::handle<Persistent> aPersistent =
0142         DownCast<Persistent, typename Base::PersistentBase>::make(this->myPersistent);
0143       Standard_NoSuchObject_Raise_if(
0144         aPersistent.IsNull(),
0145         "StdObjMgt_SharedObject::Delayed::Write - persistent object wasn't set for writing!");
0146       aPersistent->Write(theWriteData);
0147     }
0148 
0149     //! Gets persistent child objects
0150     virtual void PChildren(StdObjMgt_Persistent::SequenceOfPersistent& theChildren) const
0151     {
0152       occ::handle<Persistent> aPersistent =
0153         DownCast<Persistent, typename Base::PersistentBase>::make(this->myPersistent);
0154       Standard_NoSuchObject_Raise_if(
0155         aPersistent.IsNull(),
0156         "StdObjMgt_SharedObject::Delayed::PChildren - persistent object wasn't set for writing!");
0157       aPersistent->PChildren(theChildren);
0158     }
0159 
0160     //! Returns persistent type name
0161     virtual const char* PName() const
0162     {
0163       occ::handle<Persistent> aPersistent =
0164         DownCast<Persistent, typename Base::PersistentBase>::make(this->myPersistent);
0165       Standard_NoSuchObject_Raise_if(
0166         aPersistent.IsNull(),
0167         "StdObjMgt_SharedObject::Delayed::PName - persistent object wasn't set for writing!");
0168       return aPersistent->PName();
0169     }
0170   };
0171 };
0172 
0173 // NOLINTEND(modernize-use-override)
0174 #endif