NeoPZ
TPZSavable.h
Go to the documentation of this file.
1 
6 #ifndef TPZSAVABLE_H
7 #define TPZSAVABLE_H
8 
9 #include <string> // for string
10 #include <set> // for std::set
11 #include <map> // for std::map
12 #include <utility> // for pair
13 #include "pzerror.h" // for DebugStop
14 #include "pzvec.h" // for TPZVec
15 #include <list> // for std::list
16 class TPZSavable; // lines 20-20
17 class TPZStream; // lines 21-21
19 class TPZChunkTranslator;
25 const int TPZSAVEABLEID = -1;
26 
29 
30 
36 //#define DEBUG_SAVEABLE
37 
38 #ifdef PZDEBUG_SAVEABLE
39 #define SAVEABLE_STR_NOTE(buf,str) { std::string msg(str); buf.Write(&msg,1); }
40 #define SAVEABLE_SKIP_NOTE(buf) { std::string str; buf.Read(&str,1); }
41 #else
42 #define SAVEABLE_STR_NOTE(buf,str)
43 #define SAVEABLE_SKIP_NOTE(buf)
44 #endif
45 
46 
47 //Change the default constructor of TPZRegisterClassId to
48 // TPZRegisterClassId() = delete;
49 // in order to check all TPZSavable children classes for ClassId()
51 public:
52  // this matches the signature of 'ClassId() const'
53  template <typename T>
54  TPZRegisterClassId(int (T::*)() const) {
55  }
56  TPZRegisterClassId() = default;
57 };
58 
67 class TPZSavable : public virtual TPZRegisterClassId {
68 
69 public :
71  static std::set<TPZRestoreClassBase*> &RestoreClassSet() {
72  static std::set<TPZRestoreClassBase*> gRestoreClassSet;
73  return gRestoreClassSet;
74  }
75 
77  static std::map<int,TPZRestore_t> &ClassIdMap() {
78  static std::map<int,TPZRestore_t> gClassIdMap;
79  return gClassIdMap;
80  }
81 
82 
83 public:
84 
86  virtual ~TPZSavable()
87  {
88  }
89 
95  public:
96 virtual int ClassId() const = 0;
97 
98 
99  virtual std::list<std::map<std::string, uint64_t>> VersionHistory() const;
100  virtual std::pair<std::string, uint64_t> Version() const;
101 
102  static std::pair<std::string, uint64_t> NeoPZVersion();
103 
105  //virtual void Write(TPZStream &buf, int withclassid) const;
106 
108  virtual void Write(TPZStream &buf, int withclassid) const;
109 
111  virtual void Read(TPZStream &buf, void *context);
112 
118  virtual bool Compare(TPZSavable *copy, bool override = false);
119 
125  virtual bool Compare(TPZSavable *copy, bool override = false) const;
126 
127  static void Register(TPZRestoreClassBase *restore);
128 
129  static void RegisterClassId(int classid, TPZRestore_t fun);
130 
131  static TPZSavable *CreateInstance(const int &classId);
132 };
133 
134 
136 public :
137  virtual TPZSavable *Restore()=0;
138  virtual TPZChunkTranslator *GetTranslator()=0;
139 };
140 
149 template<class T>
151 public:
152 
155  TPZSavable::Register(this);
156  }
157 
159  virtual TPZSavable *Restore() {
160  T *ptr = new T;
161  return ptr;
162  }
163 
165  return nullptr;
166  }
167 
168 private:
170 };
171 
172 template<class T>
174 
175 
176 template<class T, class TranslatorType>
178 public:
179 
182  TPZSavable::Register(this);
183  }
184 
186 
188  virtual TPZSavable *Restore() {
189  T *ptr = new T;
190  return ptr;
191  }
192 
194  if (!gTranslator){
195  gTranslator = new TranslatorType();
196  }
197  return gTranslator;
198  }
199 
200 private:
203 };
204 
205 #include "TPZChunkTranslator.h"
206 template<class T, class TranslatorType>
208  if (gTranslator) {
209  delete gTranslator;
210  }
211 }
212 
213 template<class T, class TranslatorType>
215 
216 template<class T, class TranslatorType>
218 
219 #endif //TPZSAVABLE_H
static TPZChunkTranslator * gTranslator
Definition: TPZSavable.h:202
TPZRestoreClass()
Constructor.
Definition: TPZSavable.h:154
Templated vector implementation.
Defines PZError.
TPZRestoreClassBase * TPZRestore_t
Typedef of TPZRestore_t.
Definition: TPZSavable.h:28
static TPZRestoreClass< T > gRestoreObject
Definition: TPZSavable.h:169
TPZRegisterClassId(int(T::*)() const)
Definition: TPZSavable.h:54
const int TPZSAVEABLEID
Identifier as saveable object.
Definition: TPZSavable.h:25
static TPZRestoreClassWithTranslator< T, TranslatorType > gRestoreObject
Definition: TPZSavable.h:201
TPZRestoreClassWithTranslator()
Constructor.
Definition: TPZSavable.h:181
virtual TPZChunkTranslator * GetTranslator()
Definition: TPZSavable.h:193
virtual ~TPZSavable()
Definition: TPZSavable.h:86
virtual TPZSavable * Restore()
Restores object from Map based in classid into the buf.
Definition: TPZSavable.h:159
static std::map< int, TPZRestore_t > & ClassIdMap()
This static function guarantees that the gMap object is available when needed.
Definition: TPZSavable.h:77
virtual TPZChunkTranslator * GetTranslator()
Definition: TPZSavable.h:164
static void Register(TPZRestoreClassBase *restore)
Definition: TPZSavable.cpp:64
TPZRegisterClassId()=default
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
This class defines the interface to save and restore objects from TPZStream objects. Persistency.
Definition: TPZSavable.h:67
static std::set< TPZRestoreClassBase * > & RestoreClassSet()
This static function guarantees that the gMap object is available when needed.
Definition: TPZSavable.h:71
virtual TPZSavable * Restore()
Restores object from Map based in classid into the buf.
Definition: TPZSavable.h:188
Implements an interface to register a class id and a restore function. Persistence.
Definition: TPZSavable.h:150