897606d3437933108cea95fbc1cd51f09f27ea57
[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
20
21 //! Root class for a temporary persistent object corresponding to an attribute.
22 template <class Transient>
23 class StdObjMgt_Attribute : public    Standard_Transient,
24                             protected StdObjMgt_ContentTypes
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
52   protected:
53     DataType myData;
54   };
55
56   struct SingleInt : Simple <Value<Standard_Integer> > {};
57   struct SingleRef : Simple <Reference<> > {};
58
59 private:
60   template <class Persistent>
61   class container : public base
62   {
63   public:
64     //! Read persistent data from a file.
65     Standard_EXPORT virtual void Read (StdObjMgt_ReadData& theReadData)
66     {
67       myPersistent = new Persistent;
68       myPersistent->Read (theReadData);
69     }
70
71     //! Import transient attribuite from the persistent data
72     Standard_EXPORT virtual void ImportAttribute()
73     {
74       if (myPersistent && this->myTransient)
75       {
76         myPersistent->Import (this->myTransient);
77         myPersistent.Nullify();
78       }
79     }
80
81   private:
82     Handle(Persistent) myPersistent;
83   };
84
85 public:
86   template <class Persistent>
87   static Handle(StdObjMgt_Persistent) Instantiate()
88     { return new container<Persistent>; }
89 };
90
91 #endif // _StdObjMgt_Attribute_HeaderFile