NeoPZ
TPZStream.h
Go to the documentation of this file.
1 
8 #ifndef TPZSTREAM_H
9 #define TPZSTREAM_H
10 
11 #include "pzreal.h" // for REAL, TPZFlopCounter, is_arithmetic_pz
12 #include <stddef.h> // for NULL
13 #include <complex> // for complex
14 #include <fstream> // for string
15 #include <map> // for map
16 #include <set> // for set
17 #include <type_traits> // for enable_if, is_same
18 #include <vector> // for vector
19 #include "pzmanvector.h" // for TPZManVector
20 #include "pzvec.h" // for TPZVec
21 #include "tpzautopointer.h" // for TPZAutoPointer
22 #include <inttypes.h>
23 
24 #include "TPZPersistenceManager.h"
25 
26 #ifdef _AUTODIFF
27 template <class T> class Fad;
28 template <int Num, class T> class TFad;
29 #endif
30 
31 static uint64_t fCurrentVersion = 1; //TODO:AQUIFRANTake this away
32 
33 #define TPZostream std::ostream
34 
35 template<class T, int EXP>
37 
38 template <class T, int EXP>
40 
50 class TPZStream {
51 public:
52 
54  }
55 
56  virtual ~TPZStream() {
57  }
58 
59  virtual void Write(const bool val);
60 
61  virtual void Write(const int *p, int howMany = 1) = 0;
62 
63  virtual void Write(const unsigned int *p, int howMany = 1) = 0;
64 
65  virtual void Write(const int64_t *p, int howMany = 1) = 0;
66 
67  virtual void Write(const uint64_t *p, int howMany = 1) = 0;
68 
69 #if defined WIN32 || defined __APPLE__
70  /* On Windows7 64bits, long types have a 32 bits representation. We need to
71  adjust this, calling the functions with 64 bits. */
72  virtual void Write(const long *p, int howMany = 1);
73 
74  virtual void Write(const long unsigned int *p, int howMany = 1);
75 #endif
76 
77  virtual void Write(const float *p, int howMany = 1) = 0;
78 
79  virtual void Write(const double *p, int howMany = 1) = 0;
80 
81  virtual void Write(const long double *p, int howMany = 1);
82 
83  virtual void Write(const unsigned char *p, int howMany = 1) = 0;
84 
85  virtual void Write(const char *p, int howMany = 1) = 0;
86 
87  virtual void Write(const std::string *p, int howMany = 1);
88 
89  virtual void Write(const std::complex< float > *p, int howMany = 1) = 0;
90 
91  virtual void Write(const std::complex< double > *p, int howMany = 1) = 0;
92 
93  virtual void Write(const std::complex< long double > *p, int howMany = 1);
94 
95 #ifdef _AUTODIFF
96 
97  virtual void Write(const TFad< 1, REAL > *p, int howMany = 1) = 0;
98 
99  virtual void Write(const TFad< 6, REAL > *p, int howMany = 1) = 0;
100 
101  virtual void Write(const TFad< 8, REAL > *p, int howMany = 1) = 0;
102 
103  virtual void Write(const TFad< 9, REAL > *p, int howMany = 1) = 0;
104 
105  virtual void Write(const TFad< 10, REAL > *p, int howMany = 1) = 0;
106 
107  virtual void Write(const TFad< 14, REAL > *p, int howMany = 1) = 0;
108 
109  virtual void Write(const Fad< float > *p, int howMany = 1) = 0;
110 
111  virtual void Write(const Fad< double > *p, int howMany = 1) = 0;
112 
113  virtual void Write(const Fad< long double > *p, int howMany = 1);
114 
115  virtual void Write(const Fad<std::complex< float > > *p, int howMany = 1);
116 
117  virtual void Write(const Fad<std::complex< double > >*p, int howMany = 1);
118 
119  virtual void Write(const Fad<std::complex< long double >> *p, int howMany = 1);
120 
121 #endif
122 
123  void Write(const TPZFlopCounter *p, int howMany = 1);
124 
125  virtual void Read(bool &val);
126 
127  virtual void Read(int *p, int howMany = 1) = 0;
128 
129  virtual void Read(unsigned int *p, int howMany = 1) = 0;
130 
131  virtual void Read(int64_t *p, int howMany = 1) = 0;
132 
133  virtual void Read(uint64_t *p, int howMany = 1) = 0;
134 
135 #if defined WIN32 || defined __APPLE__
136  /* On Windows7 64bits, long types have a 32 bits representation. We need to
137  adjust this, calling the functions with 64 bits. */
138 
139  virtual void Read(long *p, int howMany = 1);
140 
141  virtual void Read(long unsigned int *p, int howMany = 1);
142 #endif
143 
144  virtual void Read(float *p, int howMany = 1) = 0;
145 
146  virtual void Read(double *p, int howMany = 1) = 0;
147 
148  virtual void Read(long double *p, int howMany = 1);
149 
150  virtual void Read(unsigned char *p, int howMany = 1) = 0;
151 
152  virtual void Read(char *p, int howMany = 1) = 0;
153 
154  virtual void Read(std::string *p, int howMany = 1);
155 
156  virtual void Read(std::complex< float > *p, int howMany = 1) = 0;
157 
158  virtual void Read(std::complex< double > *p, int howMany = 1) = 0;
159 
160  virtual void Read(std::complex< long double > *p, int howMany = 1);
161 
162 #ifdef _AUTODIFF
163 
164  virtual void Read(TFad< 1, REAL > *p, int howMany = 1) = 0;
165 
166  virtual void Read(TFad< 6, REAL > *p, int howMany = 1) = 0;
167 
168  virtual void Read(TFad< 8, REAL > *p, int howMany = 1) = 0;
169 
170  virtual void Read(TFad< 9, REAL > *p, int howMany = 1) = 0;
171 
172  virtual void Read(TFad< 10, REAL > *p, int howMany = 1) = 0;
173 
174  virtual void Read(TFad< 14, REAL > *p, int howMany = 1) = 0;
175 
176  virtual void Read(Fad< float > *p, int howMany = 1) = 0;
177 
178  virtual void Read(Fad< double > *p, int howMany = 1) = 0;
179 
180  virtual void Read(Fad< long double > *p, int howMany = 1);
181 
182  virtual void Read(Fad<std::complex< float >> *p, int howMany = 1);
183 
184  virtual void Read(Fad<std::complex< double >> *p, int howMany = 1);
185 
186  virtual void Read(Fad<std::complex< long double >> *p, int howMany = 1);
187 
188 
189 #endif
190 
191  void Read(TPZFlopCounter *p, int howMany = 1);
192 
193  //VECTORS AND ARRAYS
194 
195  template <class T,
196  typename std::enable_if<(std::is_same<std::string, T>::value || is_arithmetic_pz<T>::value), int>::type* = nullptr>
197  void Write(const TPZVec<T> &vec) {
198  int64_t nel = vec.NElements();
199  this->Write(&nel);
200  if (nel) this->Write(&vec[0], vec.NElements());
201  }
202 
203  template <class T,
204  typename std::enable_if<!(std::is_same<std::string, T>::value || is_arithmetic_pz<T>::value), int>::type* = nullptr>
205  void Write(const TPZVec<T> &vec) {
206  int64_t c, nc = vec.NElements();
207  this->Write(&nc);
208  for (c = 0; c < nc; c++) {
209  vec[c].Write(*this, 0);
210  }
211  }
212 
213  template <class T>
215  uint64_t size = vec.NElements();
216  this->Write(&size);
217  for (uint64_t i = 0; i < size; ++i) {
218  TPZPersistenceManager::WritePointer(vec[i].operator ->(), this);
219  }
220  }
221 
222  template <class T,
223  typename std::enable_if<(std::is_same<std::string, T>::value || is_arithmetic_pz<T>::value), int>::type* = nullptr>
224  void Write(const std::vector<T> &vec) {
225  int nel = vec.size();
226  this->Write(&nel);
227  for (int c = 0; c < nel; c++){
228  this->Write(&vec[c]);
229  }
230  }
231 
232  template <class T,
233  typename std::enable_if<!(std::is_same<std::string, T>::value || is_arithmetic_pz<T>::value), int>::type* = nullptr>
234  void Write(const std::vector<T> &vec) {
235  int c, nc = vec.size();
236  this->Write(&nc);
237  for (c = 0; c < nc; c++)
238  vec[c].Write(*this, 0);
239  }
240 
241  void Write(const TPZVec<TPZFlopCounter> &vec) {
242  int64_t nel = vec.NElements();
243  this->Write(&nel);
244  TPZVec<REAL> temp(nel);
245  for (int iel = 0; iel < nel; iel++) {
246  temp[iel] = vec[iel];
247  }
248  if (nel) this->Write(&temp[0], vec.NElements());
249  }
250 
251  template <class T, class U> void Write(const std::map<T, U> &vec) {
252  int64_t sz = vec.size();
253  TPZManVector<T> keyVec(sz);
254  TPZManVector<U> valVec(sz);
255  int64_t count = 0;
256  typename std::map<T, U>::const_iterator it;
257  for (it = vec.begin(); it != vec.end(); it++) {
258  keyVec[count] = it->first;
259  valVec[count++] = it->second;
260  }
261  Write(keyVec);
262  Write(valVec);
263  }
264 
265  template <class T,
266  typename std::enable_if<(std::is_same<std::string, T>::value || is_arithmetic_pz<T>::value), int>::type* = nullptr>
267  void Write(const std::set<T> &vec) {
268  int nel = vec.size();
269  this->Write(&nel);
270  typename std::set<T>::iterator it = vec.begin();
271  while (it != vec.end()) {
272  int val = *it;
273  this->Write(&val);
274  it++;
275  }
276  }
277 
278  template <class T>
279  void Read(TPZVec<T> &vec) {
280  Read(vec, NULL);
281  }
282 
283  template <class T,
284  typename std::enable_if<!(std::is_same<std::string, T>::value || is_arithmetic_pz<T>::value), int>::type* = nullptr>
285  void Read(TPZVec<T> &vec, void *context) {
286  int64_t c, nc;
287  this->Read(&nc, 1);
288  vec.Resize(nc);
289  for (c = 0; c < nc; c++) {
290  vec[c].Read(*this, context);
291  }
292  }
293 
294  template <class T,
295  typename std::enable_if<(std::is_same<std::string, T>::value || is_arithmetic_pz<T>::value), int>::type* = nullptr>
296  void Read(TPZVec<T> &vec, void *context) {
297  int64_t nc;
298  this->Read(&nc, 1);
299  vec.Resize(nc);
300  if (nc) this->Read(&vec[0], nc);
301  }
302 
303 
304  template <class T>
306  uint64_t size;
307  this->Read(&size,1);
308  vec.resize(size);
309  for (uint64_t i = 0; i < size; ++i) {
310  vec[i] = TPZAutoPointerDynamicCast<T>(TPZPersistenceManager::GetAutoPointer(this));
311  }
312  }
313 
315  int64_t nc;
316  this->Read(&nc, 1);
317  vec.Resize(nc);
318  TPZVec<REAL> temp(nc);
319  if (nc) this->Read(&temp[0], nc);
320  for (int64_t ic = 0; ic < nc; ic++) {
321  vec[ic] = temp[ic];
322  }
323  }
324 
325  template <class T,
326  typename std::enable_if<!(std::is_same<std::string, T>::value || is_arithmetic_pz<T>::value), int>::type* = nullptr>
327  void Read(std::vector<T> &vec, void *context) {
328  int c, nc;
329  this->Read(&nc, 1);
330  vec.resize(nc);
331  for (c = 0; c < nc; c++) {
332  vec[c].Read(*this, context);
333  }
334  }
335 
336  template <class T,
337  typename std::enable_if<(std::is_same<std::string, T>::value || is_arithmetic_pz<T>::value), int>::type* = nullptr>
338  void Read(std::vector<T> &vec, void *context) {
339  int nel;
340  this->Read(&nel, 1);
341  vec.resize(nel);
342  for (int i = 0; i < nel; i++) {
343  this->Read(&vec[i]);
344  }
345  }
346 
347  template <int N>
349  int64_t nc;
350  this->Read(&nc, 1);
351  vec.Resize(nc);
352  if (nc) this->Read(&vec[0], nc);
353  }
354 
355  template <int N>
357  int64_t nc;
358  this->Read(&nc, 1);
359  vec.Resize(nc);
360  if (nc) this->Read(&vec[0], nc);
361  }
362 
363  template <class T, class U> void Read(std::map<T, U> &vec) {
364  TPZManVector<T> keyVec;
365  this->Read(keyVec);
366  TPZManVector<U> valVec;
367  this->Read(valVec);
368  int sz = keyVec.NElements();
369 #ifdef PZDEBUG
370  if (sz != valVec.NElements()) {
371  DebugStop();
372  }
373 #endif
374  int i;
375  for (i = 0; i < sz; ++i) {
376  vec[keyVec[i]] = valVec[i];
377  }
378  }
379 
380  template <class T,
381  typename std::enable_if<(std::is_same<std::string, T>::value || is_arithmetic_pz<T>::value), int>::type* = nullptr>
382  void Read(std::set<T> &vec) {
383  int nel;
384  this->Read(&nel, 1);
385  for (int i = 0; i < nel; i++) {
386  T val;
387  this->Read(&val);
388  vec.insert(val);
389  }
390  }
392 
393  template <class T>
394  void WritePointers(const TPZVec<T *> &vec);
395 
396  template <class T>
397  void ReadPointers(TPZVec<T *> &vec);
398 
399  template <class T>
400  void WritePointers(const std::map<int, TPZAutoPointer<T>> &vec);
401 
402  template <class T>
403  void ReadPointers(std::map<int, TPZAutoPointer<T> > &vec);
404 
405  template <class T>
406  void WritePointers(const std::map<int, T *> &vec);
407 
408  template <class T>
409  void ReadPointers(std::map<int, T *> &vec);
410 
411  template <class T>
412  void WritePointers(const std::set<T *> &vec);
413 
414  template <class T>
415  void ReadPointers(std::set<T *> &vec);
416 
417  template <class T, int EXP>
418  void WritePointers(const TPZChunkVector<T *, EXP> &vec);
419 
420  template <class T, int EXP>
422 
423  template <class T, int EXP>
425 
426  template <class T, int EXP>
428 
429 protected:
430 
431 };
432 
433 #include "TPZPersistenceManager.h"
434 template <class T> void TPZStream::WritePointers(const TPZVec<T *> &vec) {
435  uint64_t nObjects = vec.NElements();
436  this->Write(&nObjects);
437  for (int64_t i = 0; i < nObjects; i++) {
439  }
440 }
441 
442 template <class T> void TPZStream::ReadPointers(TPZVec<T *> &vec) {
443  uint64_t nObjects;
444  this->Read(&nObjects);
445  vec.Resize(nObjects);
446  for (int64_t i = 0; i < nObjects; ++i) {
447  vec[(const int64_t)(i)] = dynamic_cast<T *>(TPZPersistenceManager::GetInstance(this));
448  }
449 }
450 
451 template <class T>
452 void TPZStream::WritePointers(const std::map<int, TPZAutoPointer<T>> &map) {
453  uint64_t nObjects = map.size();
454  this->Write(&nObjects);
455  typedef typename std::map<int, TPZAutoPointer<T>>::const_iterator map_it;
456  map_it it;
457  for (it = map.begin(); it != map.end(); it++) {
458  this->Write(&(it->first));
459  TPZPersistenceManager::WritePointer(it->second, this);
460  }
461 }
462 
463 template <class T>
464 void TPZStream::ReadPointers(std::map<int, TPZAutoPointer<T>> &map) {
465  uint64_t nObjects;
466  this->Read(&nObjects);
467  int key;
468  for (uint64_t i = 0; i < nObjects; ++i) {
469  Read(&key);
470  map[key] = TPZAutoPointerDynamicCast<T>(TPZPersistenceManager::GetAutoPointer(this));
471  }
472 }
473 
474 template <class T>
475 void TPZStream::WritePointers(const std::map<int, T *> &vec) {
476  uint64_t nObjects = vec.size();
477  this->Write(&nObjects);
478  typedef typename std::map<int, T *>::const_iterator vec_it;
479  vec_it it;
480  for (it = vec.begin(); it != vec.end(); it++) {
481  this->Write(&it->first);
482  TPZPersistenceManager::WritePointer(it->second, this);
483  }
484 }
485 
486 template <class T> void TPZStream::ReadPointers(std::map<int, T *> &map) {
487  uint64_t nObjects;
488  this->Read(&nObjects);
489  int key;
490  for (uint64_t i = 0; i < nObjects; ++i) {
491  Read(&key);
492  map[key] = dynamic_cast<T *>(TPZPersistenceManager::GetInstance(this));
493  }
494 }
495 
496 template <class T> void TPZStream::WritePointers(const std::set<T *> &_set) {
497  uint64_t nObjects = _set.size();
498  this->Write(&nObjects);
499  typedef typename std::set<T *>::const_iterator _set_it;
500  _set_it it;
501  while (it != _set.end()) {
503  it++;
504  }
505 }
506 
507 template <class T> void TPZStream::ReadPointers(std::set<T *> &_set) {
508  uint64_t nObjects;
509  this->Read(&nObjects);
510 
511  for(uint64_t i = 0; i < nObjects; ++i) {
512  _set.insert(dynamic_cast<T *>(TPZPersistenceManager::GetInstance(this)));
513  }
514 }
515 
516 template <class T, int EXP>
518  uint64_t nObjects = vec.NElements();
519  this->Write(&nObjects);
520  for (uint64_t i = 0; i < nObjects; i++) {
522  }
523 }
524 
525 template <class T, int EXP>
527  uint64_t nObjects;
528  this->Read(&nObjects);
529  vec.Resize(nObjects);
530  for (uint64_t i = 0; i < nObjects; ++i) {
531  vec[i] = dynamic_cast<T *>(TPZPersistenceManager::GetInstance(this));
532  }
533 }
534 
535 template <class T, int EXP>
537  uint64_t nObjects = vec.NElements();
538  this->Write(&nObjects);
539  for (uint64_t i = 0; i < nObjects; i++) {
541  }
542  int compactScheme = as_integer(vec.fCompactScheme);
543  this->Write(&compactScheme);
544  Write(vec.fFree);
545  Write(vec.fNFree);
546 }
547 
548 template <class T, int EXP>
550  uint64_t nObjects;
551  this->Read(&nObjects);
552  vec.Resize(nObjects);
553  for (uint64_t i = 0; i < nObjects; ++i) {
554  vec[i] = dynamic_cast<T *>(TPZPersistenceManager::GetInstance(this));
555  }
556  int compactScheme;
557  Read(&compactScheme);
558  vec.fCompactScheme = (typename TPZAdmChunkVector<T *, EXP>::CompactScheme) compactScheme;
559  Read(vec.fFree);
560  Read(vec.fNFree);
561 }
562 
563 #endif//TPZSTREAM_H
Implements a vector class which allows to use external storage provided by the user. Utility.
Definition: pzquad.h:16
virtual ~TPZStream()
Definition: TPZStream.h:56
virtual void resize(const int64_t newsize)
Definition: pzvec.h:213
Templated vector implementation.
void WritePointers(const TPZVec< TPZAutoPointer< T >> &vec)
Definition: TPZStream.h:214
TPZStream()
Definition: TPZStream.h:53
std::underlying_type< Enumeration >::type as_integer(const Enumeration value)
Definition: pzreal.h:37
void Write(const std::vector< T > &vec)
Definition: TPZStream.h:224
void Read(TPZManVector< REAL, N > &vec)
Definition: TPZStream.h:348
void Read(TPZVec< T > &vec, void *context)
Definition: TPZStream.h:285
void Read(TPZManVector< int64_t, N > &vec)
Definition: TPZStream.h:356
Definition: fad.h:54
This class implements a simple vector storage scheme for a templated class T. Utility.
Definition: pzgeopoint.h:19
int64_t NElements() const
Access method to query the number of elements of the vector.
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
Implements a chunk vector with free store administration. Utility.
Definition: TPZStream.h:39
static TPZSavable * GetInstance(const int64_t &objId)
void Resize(const int64_t newsize)
Increase the size of the chunk vector.
void ReadPointers(TPZVec< TPZAutoPointer< T >> &vec)
Definition: TPZStream.h:305
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object.
Definition: pzmanvector.h:426
void Read(TPZVec< T > &vec)
Definition: TPZStream.h:279
static uint64_t fCurrentVersion
Definition: TPZStream.h:31
static TPZAutoPointer< TPZSavable > GetAutoPointer(const int64_t &objId)
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object reallocating the necessary storage, copying the existing objects to the new...
Definition: pzvec.h:373
void Read(std::set< T > &vec)
Definition: TPZStream.h:382
CompactScheme fCompactScheme
Internal variable indicating the type of compacting scheme.
Definition: pzadmchunk.h:153
void Resize(const int newsize)
Increase the size of the chunk vector.
Definition: pzadmchunk.h:280
virtual void Write(const bool val)
Definition: TPZStream.cpp:8
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
void Write(const std::map< T, U > &vec)
Definition: TPZStream.h:251
Definition: tfad.h:64
Free store vector implementation.
Contains declaration of the TPZAutoPointer class which has Increment and Decrement actions are mutexe...
void Read(std::map< T, U > &vec)
Definition: TPZStream.h:363
TPZManVector< int > fNFree
Number of free elements within each chunk.
Definition: pzadmchunk.h:156
TPZStack< int > fFree
List of indexes of freed elements.
Definition: pzadmchunk.h:159
This class implements floating point number associated with a counter of the operations performed wit...
Definition: pzreal.h:261
Defines the interface for saving and reading data. Persistency.
Definition: TPZStream.h:50
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
Contains the declaration of TPZFlopCounter class and TPZCounter struct.
void Read(TPZVec< TPZFlopCounter > &vec)
Definition: TPZStream.h:314
void Write(const TPZVec< T > &vec)
Definition: TPZStream.h:197
void Write(const TPZVec< TPZFlopCounter > &vec)
Definition: TPZStream.h:241
static void WritePointer(const TPZSavable *obj, TPZStream *stream)
void Read(std::vector< T > &vec, void *context)
Definition: TPZStream.h:327
virtual void Read(bool &val)
Definition: TPZStream.cpp:91
This class implements a reference counter mechanism to administer a dynamically allocated object...
void Write(const std::set< T > &vec)
Definition: TPZStream.h:267