Electroneum
serialization.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2018, The Electroneum Project
2 // Copyrights(c) 2014-2017, The Monero Project
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31 
43 #pragma once
44 #include <vector>
45 #include <list>
46 #include <string>
47 #include <boost/type_traits/is_integral.hpp>
48 #include <boost/type_traits/integral_constant.hpp>
49 
54 template <class T>
55 struct is_blob_type { typedef boost::false_type type; };
56 
61 template <class T>
62 struct has_free_serializer { typedef boost::true_type type; };
63 
68 template <class T>
69 struct is_pair_type { typedef boost::false_type type; };
70 
71 template<typename F, typename S>
72 struct is_pair_type<std::pair<F,S>> { typedef boost::true_type type; };
73 
87 template <class Archive, class T>
88 struct serializer{
89  static bool serialize(Archive &ar, T &v) {
90  return serialize(ar, v, typename boost::is_integral<T>::type(), typename is_blob_type<T>::type(), typename is_pair_type<T>::type());
91  }
92  template<typename A>
93  static bool serialize(Archive &ar, T &v, boost::false_type, boost::true_type, A a) {
94  ar.serialize_blob(&v, sizeof(v));
95  return true;
96  }
97  template<typename A>
98  static bool serialize(Archive &ar, T &v, boost::true_type, boost::false_type, A a) {
99  ar.serialize_int(v);
100  return true;
101  }
102  static bool serialize(Archive &ar, T &v, boost::false_type, boost::false_type, boost::false_type) {
103  //serialize_custom(ar, v, typename has_free_serializer<T>::type());
104  return v.do_serialize(ar);
105  }
106  static bool serialize(Archive &ar, T &v, boost::false_type, boost::false_type, boost::true_type) {
107  //serialize_custom(ar, v, typename has_free_serializer<T>::type());
108  return do_serialize(ar, v);
109  }
110  static void serialize_custom(Archive &ar, T &v, boost::true_type) {
111  }
112 };
113 
118 template <class Archive, class T>
119 inline bool do_serialize(Archive &ar, T &v)
120 {
122 }
123 template <class Archive>
124 inline bool do_serialize(Archive &ar, bool &v)
125 {
126  ar.serialize_blob(&v, sizeof(v));
127  return true;
128 }
129 
130 // Never used in the code base
131 // #ifndef __GNUC__
132 // #ifndef constexpr
133 // #define constexpr
134 // #endif
135 // #endif
136 
137 /* the following add a trait to a set and define the serialization DSL*/
138 
143 #define BLOB_SERIALIZER(T) \
144  template<> \
145  struct is_blob_type<T> { \
146  typedef boost::true_type type; \
147  }
148 
153 #define FREE_SERIALIZER(T) \
154  template<> \
155  struct has_free_serializer<T> { \
156  typedef boost::true_type type; \
157  }
158 
163 #define VARIANT_TAG(Archive, Type, Tag) \
164  template <bool W> \
165  struct variant_serialization_traits<Archive<W>, Type> { \
166  static inline typename Archive<W>::variant_tag_type get_tag() { \
167  return Tag; \
168  } \
169  }
170 
177 #define BEGIN_SERIALIZE() \
178  template <bool W, template <bool> class Archive> \
179  bool do_serialize(Archive<W> &ar) {
180 
186 #define BEGIN_SERIALIZE_OBJECT() \
187  template <bool W, template <bool> class Archive> \
188  bool do_serialize(Archive<W> &ar) { \
189  ar.begin_object(); \
190  bool r = do_serialize_object(ar); \
191  ar.end_object(); \
192  return r; \
193  } \
194  template <bool W, template <bool> class Archive> \
195  bool do_serialize_object(Archive<W> &ar){
196 
199 #define PREPARE_CUSTOM_VECTOR_SERIALIZATION(size, vec) \
200  ::serialization::detail::prepare_custom_vector_serialization(size, vec, typename Archive<W>::is_saving())
201 
205 #define END_SERIALIZE() \
206  return true; \
207  }
208 
212 #define VALUE(f) \
213  do { \
214  ar.tag(#f); \
215  bool r = ::do_serialize(ar, f); \
216  if (!r || !ar.stream().good()) return false; \
217  } while(0);
218 
223 #define FIELD_N(t, f) \
224  do { \
225  ar.tag(t); \
226  bool r = ::do_serialize(ar, f); \
227  if (!r || !ar.stream().good()) return false; \
228  } while(0);
229 
234 #define FIELD(f) \
235  do { \
236  ar.tag(#f); \
237  bool r = ::do_serialize(ar, f); \
238  if (!r || !ar.stream().good()) return false; \
239  } while(0);
240 
245 #define FIELDS(f) \
246  do { \
247  bool r = ::do_serialize(ar, f); \
248  if (!r || !ar.stream().good()) return false; \
249  } while(0);
250 
254 #define VARINT_FIELD(f) \
255  do { \
256  ar.tag(#f); \
257  ar.serialize_varint(f); \
258  if (!ar.stream().good()) return false; \
259  } while(0);
260 
265 #define VARINT_FIELD_N(t, f) \
266  do { \
267  ar.tag(t); \
268  ar.serialize_varint(f); \
269  if (!ar.stream().good()) return false; \
270  } while(0);
271 
272 
273 namespace serialization {
279  namespace detail
280  {
285  template <typename T>
286  void prepare_custom_vector_serialization(size_t size, std::vector<T>& vec, const boost::mpl::bool_<true>& /*is_saving*/)
287  {
288  }
289 
290  template <typename T>
291  void prepare_custom_vector_serialization(size_t size, std::vector<T>& vec, const boost::mpl::bool_<false>& /*is_saving*/)
292  {
293  vec.resize(size);
294  }
295 
300  template<class Stream>
301  bool do_check_stream_state(Stream& s, boost::mpl::bool_<true>)
302  {
303  return s.good();
304  }
311  template<class Stream>
312  bool do_check_stream_state(Stream& s, boost::mpl::bool_<false>)
313  {
314  bool result = false;
315  if (s.good())
316  {
317  std::ios_base::iostate state = s.rdstate();
318  result = EOF == s.peek();
319  s.clear(state);
320  }
321  return result;
322  }
323  }
324 
329  template<class Archive>
330  bool check_stream_state(Archive& ar)
331  {
332  return detail::do_check_stream_state(ar.stream(), typename Archive::is_saving());
333  }
334 
339  template <class Archive, class T>
340  inline bool serialize(Archive &ar, T &v)
341  {
342  bool r = do_serialize(ar, v);
343  return r && check_stream_state(ar);
344  }
345 }
346 
347 #include "string.h"
348 #include "vector.h"
349 #include "list.h"
350 #include "pair.h"
Definition: binary_utils.h:37
bool check_stream_state(Archive &ar)
Definition: serialization.h:330
const uint32_t T[512]
Definition: groestl_tables.h:34
static void serialize_custom(Archive &ar, T &v, boost::true_type)
Definition: serialization.h:110
static bool serialize(Archive &ar, T &v, boost::false_type, boost::false_type, boost::false_type)
Definition: serialization.h:102
static bool serialize(Archive &ar, T &v)
Definition: serialization.h:89
Definition: block_queue.cpp:41
void prepare_custom_vector_serialization(size_t size, std::vector< T > &vec, const boost::mpl::bool_< true > &)
Definition: serialization.h:286
a descriptor for dispatching serialize
Definition: serialization.h:55
bool serialize(Archive &ar, T &v)
Definition: serialization.h:340
boost::false_type type
Definition: serialization.h:69
declaration and default definition for the functions used the API
a descriptor for dispatching serialize
Definition: serialization.h:62
boost::true_type type
Definition: serialization.h:72
boost::true_type type
Definition: serialization.h:62
boost::false_type type
Definition: serialization.h:55
... wouldn't a class be better?
Definition: serialization.h:88
static bool serialize(Archive &ar, T &v, boost::true_type, boost::false_type, A a)
Definition: serialization.h:98
Definition: blake256.h:37
static bool serialize(Archive &ar, T &v, boost::false_type, boost::true_type, A a)
Definition: serialization.h:93
string a
Definition: MakeCryptoOps.py:15
a descriptor for dispatching serialize
Definition: serialization.h:69
bool do_serialize(Archive &ar, T &v)
just calls the serialize function defined for ar and v...
Definition: serialization.h:119
bool do_check_stream_state(Stream &s, boost::mpl::bool_< true >)
Definition: serialization.h:301
#define s(x, c)
Definition: aesb.c:46
static bool serialize(Archive &ar, T &v, boost::false_type, boost::false_type, boost::true_type)
Definition: serialization.h:106