49 switch (cStreamType) {
64 mpStream->OpenWrite(fileName);
66 std::string versionString(
"FileVersion");
67 mpStream->Write(versionString.c_str(), 11);
70 if (!mpStream->AmIOpenForWrite()) {
73 if (mPointersToSave.size() > 0) {
77 mFileVersionInfo.clear();
78 mObjectsStream.clear();
80 mPointersToSave.clear();
82 mNextPointerToSave = 0;
86 auto objId = ScheduleToWrite(obj);
87 unsigned int nMainObjIds = mMainObjIds.size();
88 mMainObjIds.resize(nMainObjIds + 1);
89 mMainObjIds[nMainObjIds] = objId;
92 uint64_t written = 0x46;
94 for (; mNextPointerToSave < mPointersToSave.size(); ++mNextPointerToSave) {
96 mObjectsStream.Write(&mNextPointerToSave, 1);
97 auto pointer = mPointersToSave[mNextPointerToSave];
99 auto classId = pointer->ClassId();
100 if (classId == -1 || classId == 666) {
103 mObjectsStream.Write(&classId, 1);
105 mCurrentObjectStream.clear();
106 pointer->Write(mCurrentObjectStream, 0);
107 unsigned int size = mCurrentObjectStream.Size();
108 mObjectsStream.Write(&size, 1);
109 mObjectsStream << mCurrentObjectStream;
112 std::cout <<
" ObjId: " << mNextPointerToSave
113 <<
" Class name: " <<
typeid(*pointer).name()
114 <<
" ClassId: " << classId
115 <<
" range: " << written + 1 <<
" (0x" << std::hex << written + 1 << std::dec <<
") - " 116 << written +
sizeof (int64_t) + 2 *
sizeof (
int) +size
117 <<
"(0x" << std::hex << written +
sizeof (int64_t) + 2 *
sizeof (
int) +size <<
")" << std::dec
118 <<
" size: " << size << std::endl;
119 written +=
sizeof (int64_t) + 2 *
sizeof (
int) +size;
127 bool check_Q =
false;
128 for (
auto item : mFileVersionInfo) {
129 if(item.first.compare(
"NeoPZ") == 0){
137 mpStream->Write(mFileVersionInfo);
139 const uint64_t nObjects = mPointersToSave.size();
140 mpStream->Write(&nObjects);
142 size_t nObjectBytes = mObjectsStream.Size();
143 char *temp =
new char[nObjectBytes];
144 mObjectsStream.GetDataFromBuffer(temp);
145 mpStream->Write(temp, nObjectBytes);
148 unsigned int nMainObjects = mMainObjIds.size();
149 mpStream->Write(&nMainObjects);
150 for (
auto mainObjId : mMainObjIds) {
151 mpStream->Write(&mainObjId);
154 mpStream->CloseWrite();
155 mFileVersionInfo.clear();
156 mObjectsStream.clear();
158 mPointersToSave.clear();
160 mNextPointerToSave = 0;
165 auto iMap = mObjMap.find(obj);
167 if (iMap == mObjMap.end()) {
168 objId = mObjMap.size();
169 if (mPointersToSave.size() != objId) {
172 mObjMap.insert(std::make_pair(obj, objId));
173 mFileVersionInfo.insert(obj->
Version());
174 mPointersToSave.resize(objId + 1);
175 mPointersToSave[objId] =
obj;
177 objId = iMap->second;
183 const int64_t objId = ScheduleToWrite(obj);
184 stream->
Write(&objId);
203 for (uint64_t i = 0; i < size; ++i) {
204 TranslateNextPointer(chunk, toVersion);
215 std::cout <<
"Class : " << restoreClass->Restore()->ClassId() <<
" -> " <<
typeid(*restoreClass->Restore()).name() << std::endl;
216 if (restoreClass->GetTranslator()){
217 std::cout <<
"Translator : " <<
typeid(*restoreClass->GetTranslator()).name() << std::endl;
219 std::cout << std::endl;
221 TPZSavable *savable = restoreClass->Restore();
223 int classid = savable->
ClassId();
225 std::cout <<
" Class being registered with classid = " << classid << std::endl;
226 std::cout << std::endl;
230 for (
auto versionMap : objHistory) {
231 if (std::find(mVersionHistory.begin(), mVersionHistory.end(), versionMap) == mVersionHistory.end()) {
232 mVersionHistory.push_back(versionMap);
237 std::list<std::map < std::string, uint64_t>> mapsToRemove;
238 for (
auto versionMap : mVersionHistory) {
239 for (
auto otherMap : mVersionHistory) {
240 if (otherMap == versionMap) {
243 if (std::search(otherMap.begin(), otherMap.end(), versionMap.begin(), versionMap.end()) != otherMap.end()) {
244 mapsToRemove.push_back(versionMap);
249 for (
auto mapToRemove : mapsToRemove) {
250 mVersionHistory.remove(mapToRemove);
254 switch (cStreamType) {
268 mpStream->OpenRead(fileName);
269 std::string versionString(
"FileVersion");
270 char versionRead[12];
271 mpStream->Read(versionRead, 11);
272 versionRead[11] =
'\0';
274 if (versionString.compare(versionRead) != 0) {
275 PZError <<
"Error reading file - unversioned file" << std::endl;
279 mFileVersionInfo.clear();
280 mpStream->Read(mFileVersionInfo);
283 mpStream->Read(&nObjects);
284 mChunksVec.Resize(nObjects);
288 unsigned int objSize;
289 for (int64_t i = 0; i < nObjects; i++) {
290 mpStream->Read(&objId);
291 mpStream->Read(&classId);
292 mpStream->Read(&objSize);
296 std::map<std::string, uint64_t> currentVersionInfo = mFileVersionInfo;
297 auto versionIt = std::find(mVersionHistory.begin(), mVersionHistory.end(), currentVersionInfo);
298 if (versionIt == mVersionHistory.end()) {
299 std::cerr <<
"This file cannot be read by this version of the program." << std::endl;
304 while (versionIt != mVersionHistory.end()) {
305 std::map<std::string, uint64_t> nextVersion = *versionIt;
307 for (int64_t i = 0; i < nObjects; i++) {
308 auto chunk = mChunksVec[i];
309 chunk->mOldVersion = chunk->mNewVersion;
310 chunk->mOldStream = chunk->mNewStream;
311 chunk->mNewStream.clear();
314 for (int64_t i = 0; i < nObjects; i++) {
327 currentVersionInfo = nextVersion;
328 nObjects = mChunksVec.size();
332 mObjVec.Resize(mChunksVec.size());
334 for (
unsigned int i = 0; i < mObjVec.size(); ++i) {
335 int classId = mChunksVec[i]->GetClassId();
337 mObjVec[i].ResetReadStatus();
343 for (
unsigned int i = 0; i < mObjVec.size(); ++i) {
344 int classId = mChunksVec[i]->GetClassId();
345 if (classId != -1 && !mObjVec[i].IsAlreadyRead()) {
346 mObjVec[i].SetRead();
347 mObjVec[i].GetPointerToMyObj()->
Read(mChunksVec[i]->mNewStream, NULL);
349 if (mChunksVec[i]->mNewStream.Size() != 0) {
356 unsigned int nMainObjects;
357 mpStream->Read(&nMainObjects);
358 mMainObjIds.resize(nMainObjects);
359 for (
unsigned int i = 0; i < nMainObjects; ++i) {
360 mpStream->Read(&mMainObjIds[i]);
362 mpStream->CloseRead();
364 mNextMainObjIndex = 0;
369 auto nChunks = mChunksVec.size();
370 mChunksVec.resize(nChunks + 1);
375 mChunksVec[objId] = chunk;
380 auto nObjects = mObjVec.size();
381 mObjVec.Resize(nObjects + 1);
382 return &mObjVec[nObjects];
386 TPZSavable *
obj = GetInstance(mMainObjIds[mNextMainObjIndex++]);
387 if (mNextMainObjIndex == mMainObjIds.size()) {
394 mObjVec[cId].SetInstance(obj);
399 if (!mObjVec[objId].IsAlreadyRead()) {
400 mObjVec[objId].SetRead();
401 mObjVec[objId].GetPointerToMyObj()->
Read(mChunksVec[objId]->mNewStream, NULL);
402 if (mChunksVec[objId]->mNewStream.Size() != 0) {
406 return mObjVec[objId].GetPointerToMyObj();
413 stream->
Read(&objId);
414 return GetInstance(objId);
420 if (!mObjVec[objId].IsAlreadyRead()) {
421 mObjVec[objId].SetRead();
422 mObjVec[objId].GetPointerToMyObj()->
Read(mChunksVec[objId]->mNewStream, NULL);
423 if (mChunksVec[objId]->mNewStream.Size() != 0) {
427 return mObjVec[objId].GetAutoPointerToMyObj();
434 if (!mObjVec[objId].IsAlreadyRead()) {
435 mObjVec[objId].SetRead();
436 mObjVec[objId].GetPointerToMyObj()->Read(mChunksVec[objId]->mNewStream, NULL);
437 if (mChunksVec[objId]->mNewStream.Size() != 0) {
441 return mObjVec[objId].GetSharedPtrToMyObj();
443 std::shared_ptr<TPZSavable> sharedPtr;
449 stream->
Read(&objId);
450 return GetAutoPointer(objId);
455 stream->
Read(&objId);
456 return GetSharedPointer(objId);
463 mNextMainObjIndex = 0;
static std::list< std::map< std::string, uint64_t > > mVersionHistory
Class for creating a bidirectional circular buffer.
static std::map< const TPZSavable *, int64_t > mObjMap
virtual void Read(int *p, int howMany=1)
static TPZVec< const TPZSavable * > mPointersToSave
static void OpenWrite(const std::string &fileName, streamType=binary)
Contains declaration of the TPZSavable class which defines the interface to save and restore objects ...
static void TranslateNextPointer(TPZChunkInTranslation &chunk, const std::map< std::string, uint64_t > &toVersion)
static TPZSavable * ReadFromFile()
virtual void Write(const int *p, int howMany=1)
static int64_t NewChunkInTranslation()
static unsigned int OpenRead(const std::string &fileName, streamType=binary)
Templated vector implementation.
static TPZRestoredInstance * NewRestoredInstance()
virtual void Read(TPZStream &buf, void *context)
read objects from the stream
static int64_t mNextPointerToSave
static TPZSavable * GetInstance(const int64_t &objId)
static void SetChunk(const int64_t &objId, TPZAutoPointer< TPZChunkInTranslation > chunk)
static void AddInstanceToVec(TPZSavable *, const int &id)
static TPZVec< TPZAutoPointer< TPZChunkInTranslation > > mChunksVec
virtual void UpdateStream(TPZChunkInTranslation &chunk, const std::map< std::string, uint64_t > &toVersion)
static TPZAutoPointer< TPZSavable > GetAutoPointer(const int64_t &objId)
TPZContBufferedStream mOldStream
virtual int ClassId() const =0
Define the class id associated with the class.
static TPZGeneralFStream * mpStream
static unsigned int mNextMainObjIndex
static TPZContBufferedStream mCurrentObjectStream
Implements reading from and writing to an ascii file. Persistency.
virtual void Write(const bool val)
#define DebugStop()
Returns a message to user put a breakpoint in.
virtual std::pair< std::string, uint64_t > Version() const
static void RegisterClassId(int classid, TPZRestore_t fun)
static TPZSavable * CreateInstance(const int &classId)
static std::shared_ptr< TPZSavable > GetSharedPointer(const int64_t &objId)
static std::map< int, TPZRestore_t > & ClassIdMap()
This static function guarantees that the gMap object is available when needed.
static std::map< std::string, uint64_t > mFileVersionInfo
static TPZVec< int64_t > mMainObjIds
static void WriteToFile(const TPZSavable *)
static std::pair< std::string, uint64_t > NeoPZVersion()
static int64_t ScheduleToWrite(const TPZSavable *obj)
std::map< std::string, uint64_t > mNewVersion
static TPZContBufferedStream mObjectsStream
static TPZVec< TPZRestoredInstance > mObjVec
Defines the interface for saving and reading data. Persistency.
virtual std::list< std::map< std::string, uint64_t > > VersionHistory() const
This class defines the interface to save and restore objects from TPZStream objects. Persistency.
static std::set< TPZRestoreClassBase * > & RestoreClassSet()
This static function guarantees that the gMap object is available when needed.
static void WritePointer(const TPZSavable *obj, TPZStream *stream)
#define PZError
Defines the output device to error messages and the DebugStop() function.
static void TranslatePointers(TPZChunkInTranslation &chunk, const std::map< std::string, uint64_t > &toVersion)
TPZContBufferedStream mNewStream
virtual void Read(bool &val)