Electroneum
serialization.h
Go to the documentation of this file.
1 // Copyrights(c) 2017-2019, 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 <set>
47 #include <unordered_set>
48 #include <string>
49 #include <boost/type_traits/is_integral.hpp>
50 #include <boost/type_traits/integral_constant.hpp>
51 
56 template <class T>
57 struct is_blob_type { typedef boost::false_type type; };
58 
63 template <class T>
64 struct has_free_serializer { typedef boost::true_type type; };
65 
70 template <class T>
71 struct is_pair_type { typedef boost::false_type type; };
72 
73 template<typename F, typename S>
74 struct is_pair_type<std::pair<F,S>> { typedef boost::true_type type; };
75 
89 template <class Archive, class T>
90 struct serializer{
91  static bool serialize(Archive &ar, T &v) {
92  return serialize(ar, v, typename boost::is_integral<T>::type(), typename is_blob_type<T>::type(), typename is_pair_type<T>::type());
93  }
94  template<typename A>
95  static bool serialize(Archive &ar, T &v, boost::false_type, boost::true_type, A a) {
96  ar.serialize_blob(&v, sizeof(v));
97  return true;
98  }
99  template<typename A>
100  static bool serialize(Archive &ar, T &v, boost::true_type, boost::false_type, A a) {
101  ar.serialize_int(v);
102  return true;
103  }
104  static bool serialize(Archive &ar, T &v, boost::false_type, boost::false_type, boost::false_type) {
105  //serialize_custom(ar, v, typename has_free_serializer<T>::type());
106  return v.do_serialize(ar);
107  }
108  static bool serialize(Archive &ar, T &v, boost::false_type, boost::false_type, boost::true_type) {
109  //serialize_custom(ar, v, typename has_free_serializer<T>::type());
110  return do_serialize(ar, v);
111  }
112  static void serialize_custom(Archive &ar, T &v, boost::true_type) {
113  }
114 };
115 
120 template <class Archive, class T>
121 inline bool do_serialize(Archive &ar, T &v)
122 {
124 }
125 template <class Archive>
126 inline bool do_serialize(Archive &ar, bool &v)
127 {
128  ar.serialize_blob(&v, sizeof(v));
129  return true;
130 }
131 
132 // Never used in the code base
133 // #ifndef __GNUC__
134 // #ifndef constexpr
135 // #define constexpr
136 // #endif
137 // #endif
138 
139 /* the following add a trait to a set and define the serialization DSL*/
140 
145 #define BLOB_SERIALIZER(T) \
146  template<> \
147  struct is_blob_type<T> { \
148  typedef boost::true_type type; \
149  }
150 
155 #define FREE_SERIALIZER(T) \
156  template<> \
157  struct has_free_serializer<T> { \
158  typedef boost::true_type type; \
159  }
160 
165 #define VARIANT_TAG(Archive, Type, Tag) \
166  template <bool W> \
167  struct variant_serialization_traits<Archive<W>, Type> { \
168  static inline typename Archive<W>::variant_tag_type get_tag() { \
169  return Tag; \
170  } \
171  }
172 
179 #define BEGIN_SERIALIZE() \
180  template <bool W, template <bool> class Archive> \
181  bool do_serialize(Archive<W> &ar) {
182 
188 #define BEGIN_SERIALIZE_OBJECT() \
189  template <bool W, template <bool> class Archive> \
190  bool do_serialize(Archive<W> &ar) { \
191  ar.begin_object(); \
192  bool r = do_serialize_object(ar); \
193  ar.end_object(); \
194  return r; \
195  } \
196  template <bool W, template <bool> class Archive> \
197  bool do_serialize_object(Archive<W> &ar){
198 
201 #define PREPARE_CUSTOM_VECTOR_SERIALIZATION(size, vec) \
202  ::serialization::detail::prepare_custom_vector_serialization(size, vec, typename Archive<W>::is_saving())
203 
207 #define END_SERIALIZE() \
208  return ar.stream().good(); \
209  }
210 
214 #define VALUE(f) \
215  do { \
216  ar.tag(#f); \
217  bool r = ::do_serialize(ar, f); \
218  if (!r || !ar.stream().good()) return false; \
219  } while(0);
220 
225 #define FIELD_N(t, f) \
226  do { \
227  ar.tag(t); \
228  bool r = ::do_serialize(ar, f); \
229  if (!r || !ar.stream().good()) return false; \
230  } while(0);
231 
236 #define FIELD(f) \
237  do { \
238  ar.tag(#f); \
239  bool r = ::do_serialize(ar, f); \
240  if (!r || !ar.stream().good()) return false; \
241  } while(0);
242 
247 #define FIELDS(f) \
248  do { \
249  bool r = ::do_serialize(ar, f); \
250  if (!r || !ar.stream().good()) return false; \
251  } while(0);
252 
256 #define VARINT_FIELD(f) \
257  do { \
258  ar.tag(#f); \
259  ar.serialize_varint(f); \
260  if (!ar.stream().good()) return false; \
261  } while(0);
262 
267 #define VARINT_FIELD_N(t, f) \
268  do { \
269  ar.tag(t); \
270  ar.serialize_varint(f); \
271  if (!ar.stream().good()) return false; \
272  } while(0);
273 
274 
275 namespace serialization {
281  namespace detail
282  {
287  template <typename T>
288  void prepare_custom_vector_serialization(size_t size, std::vector<T>& vec, const boost::mpl::bool_<true>& /*is_saving*/)
289  {
290  }
291 
292  template <typename T>
293  void prepare_custom_vector_serialization(size_t size, std::vector<T>& vec, const boost::mpl::bool_<false>& /*is_saving*/)
294  {
295  vec.resize(size);
296  }
297 
302  template<class Stream>
303  bool do_check_stream_state(Stream& s, boost::mpl::bool_<true>)
304  {
305  return s.good();
306  }
313  template<class Stream>
314  bool do_check_stream_state(Stream& s, boost::mpl::bool_<false>)
315  {
316  bool result = false;
317  if (s.good())
318  {
319  std::ios_base::iostate state = s.rdstate();
320  result = EOF == s.peek();
321  s.clear(state);
322  }
323  return result;
324  }
325  }
326 
331  template<class Archive>
332  bool check_stream_state(Archive& ar)
333  {
334  return detail::do_check_stream_state(ar.stream(), typename Archive::is_saving());
335  }
336 
341  template <class Archive, class T>
342  inline bool serialize(Archive &ar, T &v)
343  {
344  bool r = do_serialize(ar, v);
345  return r && check_stream_state(ar);
346  }
347 }
348 
349 #include "string.h"
350 #include "vector.h"
351 #include "list.h"
352 #include "pair.h"
353 #include "set.h"
Definition: binary_utils.h:37
bool check_stream_state(Archive &ar)
Definition: serialization.h:332
const uint32_t T[512]
Definition: groestl_tables.h:34
static void serialize_custom(Archive &ar, T &v, boost::true_type)
Definition: serialization.h:112
static bool serialize(Archive &ar, T &v, boost::false_type, boost::false_type, boost::false_type)
Definition: serialization.h:104
static bool serialize(Archive &ar, T &v)
Definition: serialization.h:91
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:288
a descriptor for dispatching serialize
Definition: serialization.h:57
bool serialize(Archive &ar, T &v)
Definition: serialization.h:342
boost::false_type type
Definition: serialization.h:71
declaration and default definition for the functions used the API
a descriptor for dispatching serialize
Definition: serialization.h:64
boost::true_type type
Definition: serialization.h:74
type
Definition: json.h:74
boost::true_type type
Definition: serialization.h:64
boost::false_type type
Definition: serialization.h:57
... wouldn&#39;t a class be better?
Definition: serialization.h:90
static bool serialize(Archive &ar, T &v, boost::true_type, boost::false_type, A a)
Definition: serialization.h:100
Definition: blake256.h:37
static bool serialize(Archive &ar, T &v, boost::false_type, boost::true_type, A a)
Definition: serialization.h:95
string a
Definition: MakeCryptoOps.py:15
a descriptor for dispatching serialize
Definition: serialization.h:71
bool do_serialize(Archive &ar, T &v)
just calls the serialize function defined for ar and v...
Definition: serialization.h:121
bool do_check_stream_state(Stream &s, boost::mpl::bool_< true >)
Definition: serialization.h:303
#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:108