Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-18 08:30:30

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 class StdObjMgt_SharedObject
0021 {
0022 public:
0023   template <class Transient>
0024   class AbstractPersistentBase : public Standard_Transient
0025   {
0026   public:
0027     virtual Handle(Transient) Import() const = 0;
0028   };
0029 
0030   template <class TransientT, class Base = StdObjMgt_Persistent>
0031   class SharedBase : public Base
0032   {
0033   public:
0034     //! Changes transient object
0035     inline void Transient(const Handle(TransientT)& theTransient) { myTransient = theTransient; }
0036 
0037     //! Import transient object from the persistent data.
0038     inline const Handle(TransientT)& Import() { return myTransient; }
0039 
0040   protected:
0041     Handle(TransientT) myTransient;
0042   };
0043 
0044   template <class Base, class Transient, class Persistent = AbstractPersistentBase<Transient>>
0045   class DelayedBase : public Base
0046   {
0047   public:
0048     typedef Transient  TransientBase;
0049     typedef Persistent PersistentBase;
0050 
0051     //! Import transient object from the persistent data.
0052     virtual Handle(Transient) Import() { return myTransient; }
0053 
0054   public:
0055     Handle(Transient) myTransient;
0056   };
0057 
0058   template <class Base, class PersistentData, class Transient = typename Base::TransientBase>
0059   class IgnoreData : public Base
0060   {
0061   public:
0062     //! Read persistent data from a file.
0063     virtual void Read(StdObjMgt_ReadData& theReadData) { PersistentData().Read(theReadData); }
0064 
0065     //! Write persistent data to a file.
0066     virtual void Write(StdObjMgt_WriteData& theWriteData) const
0067     {
0068       PersistentData().Write(theWriteData);
0069     }
0070 
0071     //! Gets persistent child objects
0072     virtual void PChildren(StdObjMgt_Persistent::SequenceOfPersistent& theChildren) const
0073     {
0074       PersistentData().PChildren(theChildren);
0075     }
0076 
0077     //! Returns persistent type name
0078     virtual Standard_CString PName() const { return PersistentData().PName(); }
0079 
0080     //! Import transient object from the persistent data.
0081     virtual Handle(Transient) Import() { return NULL; }
0082   };
0083 
0084 private:
0085   template <class Base>
0086   class delayedSubBase : public Base
0087   {
0088   public:
0089     //! Import transient object from the persistent data.
0090     virtual Handle(typename Base::TransientBase) Import()
0091     {
0092       if (Base::myTransient.IsNull() && !myPersistent.IsNull())
0093       {
0094         Base::myTransient = myPersistent->Import();
0095         myPersistent.Nullify();
0096       }
0097 
0098       return Base::myTransient;
0099     }
0100 
0101   public:
0102     Handle(typename Base::PersistentBase) myPersistent;
0103   };
0104 
0105 public:
0106   template <class Base, class Persistent = typename Base::PersistentBase>
0107   class Delayed : public delayedSubBase<Base>
0108   {
0109   private:
0110     template <class T1, class T2>
0111     struct DownCast
0112     {
0113       static Handle(T1) make(const Handle(T2)& theT2) { return Handle(T1)::DownCast(theT2); }
0114     };
0115 
0116     template <class T>
0117     struct DownCast<T, T>
0118     {
0119       static Handle(T) make(const Handle(T)& theT) { return theT; }
0120     };
0121 
0122   public:
0123     //! Read persistent data from a file.
0124     virtual void Read(StdObjMgt_ReadData& theReadData)
0125     {
0126       Handle(Persistent) aPersistent = new Persistent;
0127       aPersistent->Read(theReadData);
0128       this->myPersistent = aPersistent;
0129     }
0130 
0131     //! Write persistent data to a file.
0132     virtual void Write(StdObjMgt_WriteData& theWriteData) const
0133     {
0134       Handle(Persistent) aPersistent =
0135         DownCast<Persistent, typename Base::PersistentBase>::make(this->myPersistent);
0136       Standard_NoSuchObject_Raise_if(
0137         aPersistent.IsNull(),
0138         "StdObjMgt_SharedObject::Delayed::Write - persistent object wasn't set for writing!");
0139       aPersistent->Write(theWriteData);
0140     }
0141 
0142     //! Gets persistent child objects
0143     virtual void PChildren(StdObjMgt_Persistent::SequenceOfPersistent& theChildren) const
0144     {
0145       Handle(Persistent) aPersistent =
0146         DownCast<Persistent, typename Base::PersistentBase>::make(this->myPersistent);
0147       Standard_NoSuchObject_Raise_if(
0148         aPersistent.IsNull(),
0149         "StdObjMgt_SharedObject::Delayed::PChildren - persistent object wasn't set for writing!");
0150       aPersistent->PChildren(theChildren);
0151     }
0152 
0153     //! Returns persistent type name
0154     virtual Standard_CString PName() const
0155     {
0156       Handle(Persistent) aPersistent =
0157         DownCast<Persistent, typename Base::PersistentBase>::make(this->myPersistent);
0158       Standard_NoSuchObject_Raise_if(
0159         aPersistent.IsNull(),
0160         "StdObjMgt_SharedObject::Delayed::PName - persistent object wasn't set for writing!");
0161       return aPersistent->PName();
0162     }
0163   };
0164 };
0165 
0166 #endif