Monero
Loading...
Searching...
No Matches
serialization.h
Go to the documentation of this file.
1// Copyright (c) 2014-2022, The Monero Project
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28//
29// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
30
41
42#pragma once
43#include <vector>
44#include <deque>
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#include <boost/mpl/bool.hpp>
52
57template <class T>
58struct is_blob_type { typedef boost::false_type type; };
59
68template <class Archive, class T>
69inline std::enable_if_t<is_blob_type<T>::type::value, bool> do_serialize(Archive &ar, T &v)
70{
71 ar.serialize_blob(&v, sizeof(v));
72 return true;
73}
74template <class Archive, class T>
75inline std::enable_if_t<boost::is_integral<T>::value, bool> do_serialize(Archive &ar, T &v)
76{
77 ar.serialize_int(v);
78 return true;
79}
80template <class Archive, class T>
81inline auto do_serialize(Archive &ar, T &v) -> decltype(v.member_do_serialize(ar), true)
82{
83 return v.member_do_serialize(ar);
84}
85template <class Archive>
86inline bool do_serialize(Archive &ar, bool &v)
87{
88 ar.serialize_blob(&v, sizeof(v));
89 return true;
90}
91
92/* the following add a trait to a set and define the serialization DSL*/
93
98#define BLOB_SERIALIZER(T) \
99 template<> \
100 struct is_blob_type<T> { \
101 typedef boost::true_type type; \
102 }
103
108#define VARIANT_TAG(Archive, Type, Tag) \
109 template <bool W> \
110 struct variant_serialization_traits<Archive<W>, Type> { \
111 static inline typename Archive<W>::variant_tag_type get_tag() { \
112 return Tag; \
113 } \
114 }
115
122#define BEGIN_SERIALIZE() \
123 template <bool W, template <bool> class Archive> \
124 bool member_do_serialize(Archive<W> &ar) {
125
131#define BEGIN_SERIALIZE_OBJECT() \
132 template <bool W, template <bool> class Archive> \
133 bool member_do_serialize(Archive<W> &ar) { \
134 ar.begin_object(); \
135 bool r = do_serialize_object(ar); \
136 ar.end_object(); \
137 return r; \
138 } \
139 template <bool W, template <bool> class Archive> \
140 bool do_serialize_object(Archive<W> &ar){
141
144#define PREPARE_CUSTOM_VECTOR_SERIALIZATION(size, vec) \
145 ::serialization::detail::prepare_custom_vector_serialization(size, vec, typename Archive<W>::is_saving())
146
150#define END_SERIALIZE() \
151 return ar.good(); \
152 }
153
158#define FIELD_N(t, f) \
159 do { \
160 ar.tag(t); \
161 bool r = do_serialize(ar, f); \
162 if (!r || !ar.good()) return false; \
163 } while(0);
164
169#define FIELD(f) \
170 do { \
171 ar.tag(#f); \
172 bool r = do_serialize(ar, f); \
173 if (!r || !ar.good()) return false; \
174 } while(0);
175
180#define FIELDS(f) \
181 do { \
182 bool r = do_serialize(ar, f); \
183 if (!r || !ar.good()) return false; \
184 } while(0);
185
189#define VARINT_FIELD(f) \
190 do { \
191 ar.tag(#f); \
192 ar.serialize_varint(f); \
193 if (!ar.good()) return false; \
194 } while(0);
195
200#define VARINT_FIELD_N(t, f) \
201 do { \
202 ar.tag(t); \
203 ar.serialize_varint(f); \
204 if (!ar.good()) return false; \
205 } while(0);
206
209#define MAGIC_FIELD(m) \
210 std::string magic = m; \
211 do { \
212 ar.tag("magic"); \
213 ar.serialize_blob((void*)magic.data(), magic.size()); \
214 if (!ar.good()) return false; \
215 if (magic != m) return false; \
216 } while(0);
217
220#define VERSION_FIELD(v) \
221 uint32_t version = v; \
222 do { \
223 ar.tag("version"); \
224 ar.serialize_varint(version); \
225 if (!ar.good()) return false; \
226 } while(0);
227
228
229namespace serialization {
235 namespace detail
236 {
241 template <typename T>
242 void prepare_custom_vector_serialization(size_t size, std::vector<T>& vec, const boost::mpl::bool_<true>& /*is_saving*/)
243 {
244 }
245
246 template <typename T>
247 void prepare_custom_vector_serialization(size_t size, std::vector<T>& vec, const boost::mpl::bool_<false>& /*is_saving*/)
248 {
249 vec.resize(size);
250 }
251
256 template<class Archive>
257 bool do_check_stream_state(Archive& ar, boost::mpl::bool_<true>, bool noeof)
258 {
259 return ar.good();
260 }
261
267 template<class Archive>
268 bool do_check_stream_state(Archive& ar, boost::mpl::bool_<false>, bool noeof)
269 {
270 bool result = false;
271 if (ar.good())
272 {
273 result = noeof || ar.eof();
274 }
275 return result;
276 }
277 }
278
283 template<class Archive>
284 bool check_stream_state(Archive& ar, bool noeof = false)
285 {
286 return detail::do_check_stream_state(ar, typename Archive::is_saving(), noeof);
287 }
288
293 template <class Archive, class T>
294 inline bool serialize(Archive &ar, T &v)
295 {
296 bool r = do_serialize(ar, v);
297 return r && check_stream_state(ar, false);
298 }
299
304 template <class Archive, class T>
305 inline bool serialize_noeof(Archive &ar, T &v)
306 {
307 bool r = do_serialize(ar, v);
308 return r && check_stream_state(ar, true);
309 }
310}
binary_archive< false > ar
Definition cold-outputs.cpp:54
bool do_serialize(Archive< false > &ar, std::vector< T > &v)
Definition containers.h:109
Definition container.h:34
bool do_check_stream_state(Archive &ar, boost::mpl::bool_< true >, bool noeof)
Definition serialization.h:257
void prepare_custom_vector_serialization(size_t size, std::vector< T > &vec, const boost::mpl::bool_< true > &)
Definition serialization.h:242
Definition binary_utils.h:36
bool serialize_noeof(Archive &ar, T &v)
Definition serialization.h:305
bool check_stream_state(Archive &ar, bool noeof=false)
Definition serialization.h:284
bool serialize(Archive &ar, T &v)
Definition serialization.h:294
std::enable_if_t< is_blob_type< T >::type::value, bool > do_serialize(Archive &ar, T &v)
main function for dispatching serialization for a given pair of archive and value types
Definition serialization.h:69
tools::wallet2::message_signature_result_t result
Definition signature.cpp:62
a descriptor for dispatching serialize
Definition serialization.h:58
boost::false_type type
Definition serialization.h:58
#define T(x)