0a203cc51853c28ab0c2fbaf995be45b80ead038
[occt.git] / src / StdObjMgt / StdObjMgt_Attribute.hxx
1 // Copyright (c) 2015 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifndef _StdObjMgt_Attribute_HeaderFile
15 #define _StdObjMgt_Attribute_HeaderFile
16
17 #include <StdObjMgt_Persistent.hxx>
18 #include <StdObjMgt_ReadData.hxx>
19 #include <StdObjMgt_WriteData.hxx>
20
21
22 //! Root class for a temporary persistent object corresponding to an attribute.
23 template <class Transient>
24 class StdObjMgt_Attribute : public Standard_Transient
25 {
26   class base : public StdObjMgt_Persistent
27   {
28   public:
29     //! Create an empty transient attribuite
30     Standard_EXPORT virtual Handle(TDF_Attribute) CreateAttribute()
31       { return myTransient = new Transient; }
32
33     //! Get transient attribuite for the persistent data
34     Standard_EXPORT virtual Handle(TDF_Attribute) GetAttribute() const
35       { return Handle(TDF_Attribute)(myTransient); }
36
37   protected:
38     Handle(Transient) myTransient;
39   };
40
41 public:
42   class Static : public base {};
43
44   template <class DataType>
45   class Simple : public Static
46   {
47   public:
48     //! Read persistent data from a file.
49     Standard_EXPORT virtual void Read (StdObjMgt_ReadData& theReadData)
50       { theReadData >> myData; }
51     //! Write persistent data to a file.
52     Standard_EXPORT virtual void Write (StdObjMgt_WriteData& theWriteData) const
53       { theWriteData << myData; }
54     Standard_EXPORT virtual void PChildren(StdObjMgt_Persistent::SequenceOfPersistent&) const { }
55     Standard_EXPORT virtual Standard_CString PName() const { return "StdObjMgt_Attribute::undefined"; }
56
57   protected:
58     DataType myData;
59   };
60
61   struct SingleInt : Simple<Standard_Integer> {};
62   struct SingleRef : Simple<Handle(StdObjMgt_Persistent)> {};
63
64 private:
65   template <class Persistent>
66   class container : public base
67   {
68   public:
69     //! Read persistent data from a file.
70     Standard_EXPORT virtual void Read (StdObjMgt_ReadData& theReadData)
71     {
72       myPersistent = new Persistent;
73       myPersistent->Read (theReadData);
74     }
75     //! Write persistent data to a file.
76     Standard_EXPORT virtual void Write(StdObjMgt_WriteData& theWriteData) const
77       { myPersistent->Write(theWriteData); }
78     Standard_EXPORT virtual void PChildren(StdObjMgt_Persistent::SequenceOfPersistent&) const { }
79     Standard_EXPORT virtual Standard_CString PName() const 
80       { return myPersistent->PName(); }
81
82     //! Import transient attribuite from the persistent data
83     Standard_EXPORT virtual void ImportAttribute()
84     {
85       if (myPersistent && this->myTransient)
86       {
87         myPersistent->Import (this->myTransient);
88         myPersistent.Nullify();
89       }
90     }
91
92   private:
93     Handle(Persistent) myPersistent;
94   };
95
96 public:
97   template <class Persistent>
98   static Handle(StdObjMgt_Persistent) Instantiate()
99     { return new container<Persistent>; }
100 };
101
102 #endif // _StdObjMgt_Attribute_HeaderFile