NeoPZ
TPZSavable.cpp
Go to the documentation of this file.
1 
6 #include "TPZSavable.h"
7 #include <iostream> // for operator<<, basic_ostream
8 #include <vector> // for allocator
9 #include "TPZStream.h" // for TPZStream
10 #include "pzlog.h" // for glogmutex, LOGPZ_ERROR
11 #include <list>
12 
13 #include "pzlog.h"
14 #include "TPZPersistenceManager.h"
15 #include "TPZChunkTranslator.h"
16 
17 #ifdef LOG4CXX
18 static LoggerPtr logger(Logger::getLogger("pz.saveable"));
19 static LoggerPtr loggerCheck(Logger::getLogger("pz.checkconsistency"));
20 #endif
21 
22 std::list<std::map<std::string, uint64_t>> TPZSavable::VersionHistory() const {
23  std::list<std::map<std::string, uint64_t>> history;
24  std::map<std::string, uint64_t> versionMap;
25  versionMap.insert(std::make_pair("NeoPZ", 1));
26  history.push_back(versionMap);
27  versionMap.clear();
28  versionMap.insert(std::make_pair("NeoPZ", 2));
29  history.push_back(versionMap);
30  versionMap.clear();
31  versionMap.insert(std::make_pair("NeoPZ", 3));
32  history.push_back(versionMap);
33  versionMap.clear();
34  versionMap.insert(std::make_pair("NeoPZ", 4));
35  history.push_back(versionMap);
36  return history;
37 }
38 
39 std::pair<std::string, uint64_t> TPZSavable::Version() const {
40  return TPZSavable::NeoPZVersion();
41 }
42 
43 std::pair<std::string, uint64_t> TPZSavable::NeoPZVersion() {
44  return std::make_pair("NeoPZ", 4);
45 }
46 
47 void TPZSavable::Write(TPZStream &buf, int withclassid) const
48 {
49  DebugStop();
50  if(withclassid) {
51  int var = ClassId();
52  if(var == -1)
53  {
54  std::cout << "TPZSavable::Write const with classid -1 expect trouble\n";
55  }
56  //buf.Write(&var,1);
57  }
58 }
59 
60 
61 void TPZSavable::Read(TPZStream &buf, void *context)
62 {}
63 
65  std::set<TPZRestoreClassBase*>::iterator it;
66  it = RestoreClassSet().find(restore);
67  if(it != RestoreClassSet().end())
68  {
69  std::cout << "TPZSavable::Register duplicate RestoreClass " << std::endl;
70  DebugStop();
71  }
72  RestoreClassSet().insert(restore);
73 }
74 
75 
77 {
78 
79  std::map<int,TPZRestore_t>::iterator it;
80  it = ClassIdMap().find(classid);
81  if(it != ClassIdMap().end())
82  {
83  std::cout << "TPZSavable::Register duplicate classid " << it->second->Restore()->ClassId() << std::endl;
84  DebugStop();
85  }
86  ClassIdMap()[classid] = fun;
87  if (fun->GetTranslator()){
88  fun->GetTranslator()->SetClassId(classid);
89  }
90 
91 }
92 
94 
98 bool TPZSavable::Compare(TPZSavable *copy, bool override)
99 {
100  std::stringstream sout;
101  sout << "Class id " << ClassId() << " Compare needs to be implemented";
102  LOGPZ_ERROR(loggerCheck,sout.str())
103  return false;
104 }
105 
107 
111 bool TPZSavable::Compare(TPZSavable *copy, bool override) const
112 {
113  std::stringstream sout;
114  sout << "Class id " << ClassId() << " Compare needs to be implemented";
115  LOGPZ_ERROR(loggerCheck,sout.str())
116  return false;
117 }
118 
119 
121 
122  std::map<int,TPZRestore_t>::const_iterator it;
123  it = ClassIdMap().find(classId);
124  if(it == ClassIdMap().end()) {
125  std::cout << "TPZSavable trying to restore unknown object with classId " << classId << std::endl;
126 #ifdef LOG4CXX
127  {
128  std::stringstream sout;
129  sout << __PRETTY_FUNCTION__ << " trying to restore unknown object with classId " << classId;
130  LOGPZ_ERROR(logger,sout.str().c_str());
131  }
132 #endif
133  DebugStop();
134  }
135 
136  TPZRestore_t fun= it->second;
137  return fun->Restore();
138 }
Contains declaration of the TPZSavable class which defines the interface to save and restore objects ...
Contains definitions to LOGPZ_DEBUG, LOGPZ_INFO, LOGPZ_WARN, LOGPZ_ERROR and LOGPZ_FATAL, and the implementation of the inline InitializePZLOG(string) function using log4cxx library or not. It must to be called out of "#ifdef LOG4CXX" scope.
virtual void Read(TPZStream &buf, void *context)
read objects from the stream
Definition: TPZSavable.cpp:61
virtual bool Compare(TPZSavable *copy, bool override=false)
Compares the object for identity with the object pointed to, eventually copy the object.
Definition: TPZSavable.cpp:98
virtual int ClassId() const =0
Define the class id associated with the class.
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
virtual std::pair< std::string, uint64_t > Version() const
Definition: TPZSavable.cpp:39
static void RegisterClassId(int classid, TPZRestore_t fun)
Definition: TPZSavable.cpp:76
static TPZSavable * CreateInstance(const int &classId)
Definition: TPZSavable.cpp:120
static std::map< int, TPZRestore_t > & ClassIdMap()
This static function guarantees that the gMap object is available when needed.
Definition: TPZSavable.h:77
virtual void Write(TPZStream &buf, int withclassid) const
Writes this object to the TPZStream buffer. Include the classid if withclassid = true.
Definition: TPZSavable.cpp:47
#define LOGPZ_ERROR(A, B)
Define log for errors (cout)
Definition: pzlog.h:93
virtual TPZSavable * Restore()=0
virtual void SetClassId(int classid)
static std::pair< std::string, uint64_t > NeoPZVersion()
Definition: TPZSavable.cpp:43
static void Register(TPZRestoreClassBase *restore)
Definition: TPZSavable.cpp:64
Contains declaration of the abstract TPZStream class. TPZStream defines the interface for saving and ...
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
virtual std::list< std::map< std::string, uint64_t > > VersionHistory() const
Definition: TPZSavable.cpp:22
virtual TPZChunkTranslator * GetTranslator()=0
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