SUNphi  1.0
Binarize.hpp
Go to the documentation of this file.
1 #ifndef _BINARIZE_HPP
2 #define _BINARIZE_HPP
3 
4 /// \file Binarize.hpp
5 ///
6 /// \brief Converts to and from binary
7 
8 #include <cstring>
9 #include <string>
10 #include <vector>
11 
12 #include <metaprogramming/CRTP.hpp>
13 #include <metaprogramming/SFINAE.hpp>
14 
15 #include <ios/MinimalLogger.hpp>
16 
17 namespace SUNphi
18 {
21 
22  /// Determine whether a type is serializable
23  template <typename T>
24  [[ maybe_unused ]]
25  constexpr bool isBinarizable=
26  hasMember_binarize<T> and hasMember_deBinarize<T>;
27 
28  /// Class to convert objects to and from binary
29  class Binarizer
30  {
31  /// Buffer
32  std::vector<char> buf;
33 
34  /// Reading position
36 
37  /// Push data on the back
38  Binarizer& pushBack(const void* data, ///< Data to be pushed
39  const size_t& size) ///< Data size
40  {
41  buf.insert(buf.end(),(char*)data,(char*)data+size);
42 
43  return
44  *this;
45  }
46 
47  /// Push data on the back
48  Binarizer& readAdvancing(void* out, ///< Data to be filled
49  const size_t& size) ///< Data size
50  {
51  memcpy(out,&buf[readPos],size);
52 
53  readPos+=
54  size;
55 
56  return
57  *this;
58  }
59 
60  public:
61 
62  /// Used size
64  const
65  {
66  return
67  buf.size();
68  }
69 
70  /// Add both begin and end method, with and not const
71 #define PROVIDE_BEGIN_END(CV)
72  /*! Pointer to the beginning of the buffer */
73  CV auto begin()
74  CV
75  {
76  return
77  buf.begin();
78  }
79 
80  /*! Pointer to the end of the buffer */
81  CV auto end()
82  CV
83  {
84  return
85  buf.end();
86  }
87 
90 
91 #undef PROVIDE_BEGIN_END
92 
93  /// Write on the binarizer, if the type has a member binarize
94  template <typename T,
97  {
98  return
99  rhs.binarize(*this);
100  }
101 
102  /// Read from the binarizer, if the type has a member deBinarize
103  template <typename T,
106  {
107  return
108  rhs.deBinarize(*this);
109  }
110 
111  /// Write on the binarizer
112  template <typename T,
115  {
116  return
117  pushBack(&rhs,sizeof(T));
118  }
119 
120  /// Read from the binarizer
121  template <typename T,
124  {
125  return
126  readAdvancing(&rhs,sizeof(T));
127  }
128 
129  /// Binarize a tuple-like
130  template <typename T,
132  Binarizer& binarize(T&& rhs) ///< Input
133  {
134  forEach(rhs,
135  [this](auto& s)
136  {
137  this->binarize(s);
138  });
139 
140  return
141  *this;
142  }
143 
144  /// DeBinarize a tuple-like
145  template <typename T,
147  Binarizer& deBinarize(T&& rhs) ///< Output
148  {
149  forEach(rhs,
150  [this](auto& s)
151  {
152  this->deBinarize(s);
153  });
154 
155  return
156  *this;
157  }
158 
159  /// Binarize a vector-like
160  template <typename T,
162  Binarizer& binarize(T&& rhs) ///< Input
163  {
164  /// Number of elements
165  const size_t nel=
166  rhs.size();
167 
168  this->binarize(nel);
169 
170  for(size_t iel=0;iel<nel;iel++)
171  this->binarize(rhs[iel]);
172 
173  return
174  *this;
175  }
176 
177  /// DeBinarize a vector-like
178  template <typename T,
180  Binarizer& deBinarize(T&& rhs) ///< Output
181  {
182  /// Number of elements
183  size_t nel;
184 
185  this->deBinarize(nel);
186 
187  rhs.resize(nel);
188 
189  for(size_t iel=0;iel<nel;iel++)
190  this->deBinarize(rhs[iel]);
191 
192  return
193  *this;
194  }
195 
196  /// Restart from head
198  {
199  readPos=
200  0;
201  }
202 
203  /// Restart to write
204  void clear()
205  {
206  buf.clear();
207  restartReading();
208  }
209  };
210 
211  /// Add binarizable functionality via CRTP
212  template <typename T>
214  {
215  public:
216 
218 
219  /// Binarize a Serializable
220  template <typename B=Binarizer>
221  Binarizer& binarize(B&& out={}) ///< Output
222  const
223  {
224  return
225  out.binarize(CRTP_THIS());
226  }
227 
228  /// DeBinarize a Serializable
229  template <typename B=Binarizer>
230  Binarizer& deBinarize(B&& rhs) ///< Input
231  {
232  return
233  rhs.deBinarize(CRTP_THIS());
234  }
235  };
236 }
237 
238 #endif
#define CRTP_THIS
Access to the inheriting class.
Definition: CRTP.hpp:32
#define DEFINE_HAS_MEMBER(TAG)
Definition: TypeTraits.hpp:581
constexpr bool isBinarizable
Determine whether a type is serializable.
Definition: Binarize.hpp:25
Add binarizable functionality via CRTP.
Definition: Binarize.hpp:213
Binarizer & binarize(B &&out={}) const
Binarize a Serializable.
Definition: Binarize.hpp:221
#define PROVIDE_CRTP_CAST_OPERATOR(CLASS)
Definition: CRTP.hpp:16
Binarizer & deBinarize(B &&rhs)
DeBinarize a Serializable.
Definition: Binarize.hpp:230
void clear()
Restart to write.
Definition: Binarize.hpp:204
Binarizer & pushBack(const void *data, const size_t &size)
Push data on the back.
Definition: Binarize.hpp:38
std::vector< char > buf
Buffer.
Definition: Binarize.hpp:32
#define SFINAE_ON_TEMPLATE_ARG(...)
Definition: SFINAE.hpp:24
Binarizer & readAdvancing(void *out, const size_t &size)
Push data on the back.
Definition: Binarize.hpp:48
size_t size() const
Used size.
Definition: Binarize.hpp:63
#define PROVIDE_BEGIN_END(CV)
Add both begin and end method, with and not const.
Definition: Binarize.hpp:71