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{
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
145#define PREPARE_CUSTOM_VECTOR_SERIALIZATION(size, vec, min_wire) \
146 do \
147 { \
148 if (!::serialization::detail::prepare_custom_vector_serialization(size, vec, min_wire, ar)) \
149 return false; \
150 } while (0);
151
155#define END_SERIALIZE() \
156 return ar.good(); \
157 }
158
163#define FIELD_N(t, f) \
164 do { \
165 ar.tag(t); \
166 bool r = do_serialize(ar, f); \
167 if (!r || !ar.good()) return false; \
168 } while(0);
169
174#define FIELD(f) \
175 do { \
176 ar.tag(#f); \
177 bool r = do_serialize(ar, f); \
178 if (!r || !ar.good()) return false; \
179 } while(0);
180
185#define FIELDS(f) \
186 do { \
187 bool r = do_serialize(ar, f); \
188 if (!r || !ar.good()) return false; \
189 } while(0);
190
194#define VARINT_FIELD(f) \
195 do { \
196 ar.tag(#f); \
197 ar.serialize_varint(f); \
198 if (!ar.good()) return false; \
199 } while(0);
200
205#define VARINT_FIELD_N(t, f) \
206 do { \
207 ar.tag(t); \
208 ar.serialize_varint(f); \
209 if (!ar.good()) return false; \
210 } while(0);
211
214#define MAGIC_FIELD(m) \
215 std::string magic = m; \
216 do { \
217 ar.tag("magic"); \
218 ar.serialize_blob((void*)magic.data(), magic.size()); \
219 if (!ar.good()) return false; \
220 if (magic != m) return false; \
221 } while(0);
222
225#define VERSION_FIELD(v) \
226 uint32_t version = v; \
227 do { \
228 ar.tag("version"); \
229 ar.serialize_varint(version); \
230 if (!ar.good()) return false; \
231 } while(0);
232
237#define CONTAINER_FIELD_CAPPED(f, c) \
238 do { \
239 ar.tag(#f); \
240 bool r = do_serialize(ar, f, c); \
241 if (!r || !ar.good()) return false; \
242 } while(0);
243
244namespace serialization {
245
251 namespace detail
252 {
257 template <typename T, template<bool> class A>
258 constexpr bool prepare_custom_vector_serialization(size_t, const std::vector<T>& vec, size_t, const A<true>&) noexcept
259 {
260 return true;
261 }
262
263 template <typename T, template<bool> class A>
264 bool prepare_custom_vector_serialization(const size_t size, std::vector<T>& vec, const size_t min_size, const A<false>& ar)
265 {
266 if (size && ar.remaining_bytes() / size < min_size)
267 return false;
268 vec.resize(size);
269 return true;
270 }
271
276 template<class Archive>
277 bool do_check_stream_state(Archive& ar, boost::mpl::bool_<true>, bool noeof)
278 {
279 return ar.good();
280 }
281
287 template<class Archive>
288 bool do_check_stream_state(Archive& ar, boost::mpl::bool_<false>, bool noeof)
289 {
290 bool result = false;
291 if (ar.good())
292 {
293 result = noeof || ar.eof();
294 }
295 return result;
296 }
297 }
298
303 template<class Archive>
304 bool check_stream_state(Archive& ar, bool noeof = false)
305 {
306 return detail::do_check_stream_state(ar, typename Archive::is_saving(), noeof);
307 }
308
313 template <class Archive, class T>
314 inline bool serialize(Archive &ar, T &v)
315 {
316 bool r = do_serialize(ar, v);
317 return r && check_stream_state(ar, false);
318 }
319
324 template <class Archive, class T>
325 inline bool serialize_noeof(Archive &ar, T &v)
326 {
327 bool r = do_serialize(ar, v);
328 return r && check_stream_state(ar, true);
329 }
330}
binary_archive< false > ar
Definition cold-outputs.cpp:54
bool do_serialize(Archive< false > &ar, std::vector< T > &v, size_t max_cnt=std::numeric_limits< size_t >::max())
Definition containers.h:108
Definition container.h:40
bool do_check_stream_state(Archive &ar, boost::mpl::bool_< true >, bool noeof)
Definition serialization.h:277
constexpr bool prepare_custom_vector_serialization(size_t, const std::vector< T > &vec, size_t, const A< true > &) noexcept
Definition serialization.h:258
Definition binary_utils.h:36
bool serialize_noeof(Archive &ar, T &v)
Definition serialization.h:325
bool check_stream_state(Archive &ar, bool noeof=false)
Definition serialization.h:304
bool serialize(Archive &ar, T &v)
Definition serialization.h:314
Matcher< T > A()
Definition gmock-matchers.h:3732
tools::wallet2::message_signature_result_t result
Definition signature.cpp:62
bool good() const noexcept
Definition binary_archive.h:99
bool eof() const noexcept
If implementing as std::istream, reset stream error state after peek() call.
Definition binary_archive.h:103
void serialize_blob(void *buf, size_t len, const char *delimiter="")
Definition binary_archive.h:130
size_t remaining_bytes() const noexcept
Definition binary_archive.h:169
void serialize_int(T &v)
Definition binary_archive.h:107
a descriptor for dispatching serialize
Definition serialization.h:58
boost::false_type type
Definition serialization.h:58
#define T(x)