NuTo
Numerics Tool
SerializeStreamOut.h
Go to the documentation of this file.
1 #pragma once
3 #include <typeinfo>
4 #include <Eigen/Core>
5 
6 namespace Eigen
7 {
8 template <typename T, int TRows, int TCols, int TOptions, int TMaxRows, int TMaxCols>
9 class Matrix;
10 }
11 
12 namespace NuTo
13 {
16 {
17 public:
21  SerializeStreamOut(const std::string& rFile, bool rIsBinary);
22  virtual ~SerializeStreamOut() = default;
23 
24  template <typename T>
25  inline friend SerializeStreamOut& operator<<(SerializeStreamOut& rStream, T& rData)
26  {
27  rStream.Serialize(rData);
28  return rStream;
29  }
30 
31  template <typename T>
32  void Serialize(T& rData)
33  {
34  rData.NuToSerializeSave(*this);
35  }
36  void Serialize(double& rData)
37  {
38  SerializePrimitiveType(rData);
39  }
40  void Serialize(int& rData)
41  {
42  SerializePrimitiveType(rData);
43  }
44  void Serialize(bool& rData)
45  {
46  SerializePrimitiveType(rData);
47  }
48 
49  template <typename T, int TRows, int TCols, int TOptions, int TMaxRows, int TMaxCols>
51  {
52  SaveMatrix(rMatrix);
53  }
54 
55  template <typename T, int TRows, int TCols, int TOptions, int TMaxRows, int TMaxCols>
57  {
58  const auto& rows = rMatrix.rows();
59  const auto& cols = rMatrix.cols();
60  const auto& data = rMatrix.data();
61  if (mIsBinary)
62  {
63  mFileStream.write(reinterpret_cast<const char*>(data), rows * cols * sizeof(T));
64  }
65  else
66  {
67  // one line of debug info:
68  mFileStream << "Matrix ( " << rows << " x " << cols << " ): " << '\n';
69  for (int i = 0; i < rows * cols; ++i)
70  mFileStream << data[i] << '\n';
71  }
72  }
73 
75  void Separator();
76 
77 private:
78  template <typename T>
79  void SerializePrimitiveType(T rData)
80  {
81  if (mIsBinary)
82  {
83  mFileStream.write(reinterpret_cast<const char*>(&rData), sizeof(T));
84  }
85  else
86  {
87  mFileStream << typeid(rData).name() << '\n'; // one line of debug info
88  mFileStream << static_cast<double>(rData) << '\n';
89  }
90  }
91 };
92 } // namespace NuTo
friend SerializeStreamOut & operator<<(SerializeStreamOut &rStream, T &rData)
Definition: SerializeStreamOut.h:25
void Serialize(int &rData)
Definition: SerializeStreamOut.h:40
Serialize output stream.
Definition: SerializeStreamOut.h:15
void SaveMatrix(const Eigen::Matrix< T, TRows, TCols, TOptions, TMaxRows, TMaxCols > &rMatrix)
Definition: SerializeStreamOut.h:56
Definition: SerializeStreamOut.h:6
void Serialize(T &rData)
Definition: SerializeStreamOut.h:32
void Serialize(bool &rData)
Definition: SerializeStreamOut.h:44
Definition: Exception.h:6
Definition: SerializeStreamOut.h:9
void Serialize(double &rData)
Definition: SerializeStreamOut.h:36
Base class for the NuTo SerializeStream Used for the serialization of data values only...
Definition: SerializeStreamBase.h:10
void Serialize(Eigen::Matrix< T, TRows, TCols, TOptions, TMaxRows, TMaxCols > &rMatrix)
Definition: SerializeStreamOut.h:50