Debian Codegraph @

src/serialization/serialization.h

raw symbols SCIP
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
31 /*! \file serialization.h
32 * \brief Simple DSL AAPI based on
33 *
34 * \detailed is_blob_type and has_free_serializer are
35 * both descriptors for dispatching on to the serialize function.
36 *
37 * The API itself defines a domain specific language via dirty macro
38 * hacks. Greenspun's tenth rule is very much in action throughout
39 * this entire code base.
40 */
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
53 /*! \struct is_blob_type
54 *
55 * \brief a descriptor for dispatching serialize
56 */
57 template <class T>
58 struct is_blob_type { typedef boost::false_type type; };
59
60 /*! \fn do_serialize(Archive &ar, T &v)
61 *
62 * \brief main function for dispatching serialization for a given pair of archive and value types
63 *
64 * Types marked true with is_blob_type<T> will be serialized as a blob, integral types will be
65 * serialized as integers, and types who have a `member_do_serialize` method will be serialized
66 * using that method. Booleans are serialized like blobs.
67 */
68 template <class Archive, class T>
69 inline 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 }
74 template <class Archive, class T>
75 inline 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 }
80 template <class Archive, class T>
81 inline auto do_serialize(Archive &ar, T &v) -> decltype(v.member_do_serialize(ar), true)
82 {
83 return v.member_do_serialize(ar);
84 }
85 template <class Archive>
86 inline 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
94 /*! \macro BLOB_SERIALIZER
95 *
96 * \brief makes the type have a blob serializer trait defined
97 */
98 #define BLOB_SERIALIZER(T) \
99 template<> \
100 struct is_blob_type<T> { \
101 typedef boost::true_type type; \
102 }
103
104 /*! \macro VARIANT_TAG
105 *
106 * \brief Adds the tag \tag to the \a Archive of \a Type
107 */
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
116 /*! \macro BEGIN_SERIALIZE
117 *
118 * \brief Begins the environment of the DSL
119 * \detailed for describing how to
120 * serialize an of an archive type
121 */
122 #define BEGIN_SERIALIZE() \
123 template <bool W, template <bool> class Archive> \
124 bool member_do_serialize(Archive<W> &ar) {
125
126 /*! \macro BEGIN_SERIALIZE_OBJECT
127 *
128 * \brief begins the environment of the DSL
129 * \detailed for described the serialization of an object
130 */
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
142 /*! \macro PREPARE_CUSTOM_VECTOR_SERIALIZATION
143 */
144 #define PREPARE_CUSTOM_VECTOR_SERIALIZATION(size, vec) \
145 ::serialization::detail::prepare_custom_vector_serialization(size, vec, typename Archive<W>::is_saving())
146
147 /*! \macro END_SERIALIZE
148 * \brief self-explanatory
149 */
150 #define END_SERIALIZE() \
151 return ar.good(); \
152 }
153
154 /*! \macro FIELD_N(t,f)
155 *
156 * \brief serializes a field \a f tagged \a t
157 */
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
165 /*! \macro FIELD(f)
166 *
167 * \brief tags the field with the variable name and then serializes it
168 */
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
176 /*! \macro FIELDS(f)
177 *
178 * \brief does not add a tag to the serialized value
179 */
180 #define FIELDS(f) \
181 do { \
182 bool r = do_serialize(ar, f); \
183 if (!r || !ar.good()) return false; \
184 } while(0);
185
186 /*! \macro VARINT_FIELD(f)
187 * \brief tags and serializes the varint \a f
188 */
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
196 /*! \macro VARINT_FIELD_N(t, f)
197 *
198 * \brief tags (as \a t) and serializes the varint \a f
199 */
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
207 /*! \macro MAGIC_FIELD(m)
208 */
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
218 /*! \macro VERSION_FIELD(v)
219 */
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
229 namespace serialization {
230 /*! \namespace detail
231 *
232 * \brief declaration and default definition for the functions used the API
233 *
234 */
235 namespace detail
236 {
237 /*! \fn prepare_custom_vector_serialization
238 *
239 * prepares the vector /vec for serialization
240 */
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
252 /*! \fn do_check_stream_state
253 *
254 * \brief self explanatory
255 */
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 /*! \fn do_check_stream_state
262 *
263 * \brief self explanatory
264 *
265 * \detailed Also checks to make sure that the stream is not at EOF
266 */
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
279 /*! \fn check_stream_state
280 *
281 * \brief calls detail::do_check_stream_state for ar
282 */
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
289 /*! \fn serialize
290 *
291 * \brief serializes \a v into \a ar
292 */
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
300 /*! \fn serialize
301 *
302 * \brief serializes \a v into \a ar
303 */
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 }