Monero
Loading...
Searching...
No Matches
keyvalue_serialization.h
Go to the documentation of this file.
1// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are met:
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above copyright
9// notice, this list of conditions and the following disclaimer in the
10// documentation and/or other materials provided with the distribution.
11// * Neither the name of the Andrey N. Sabelnikov nor the
12// names of its contributors may be used to endorse or promote products
13// derived from this software without specific prior written permission.
14//
15// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY
19// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25//
26
27#pragma once
28
29#include <type_traits>
30#include <boost/utility/value_init.hpp>
31#include <boost/foreach.hpp>
32#include "misc_log_ex.h"
34
35#undef MONERO_DEFAULT_LOG_CATEGORY
36#define MONERO_DEFAULT_LOG_CATEGORY "serialization"
37
38namespace epee
39{
40 /************************************************************************/
41 /* Serialize map declarations */
42 /************************************************************************/
43#define BEGIN_KV_SERIALIZE_MAP() \
44public: \
45 template<class t_storage> \
46 bool store( t_storage& st, typename t_storage::hsection hparent_section = nullptr) const\
47 {\
48 using type = typename std::remove_const<typename std::remove_reference<decltype(*this)>::type>::type; \
49 auto &self = const_cast<type&>(*this); \
50 return self.template serialize_map<true>(st, hparent_section); \
51 }\
52 template<class t_storage> \
53 bool _load( t_storage& stg, typename t_storage::hsection hparent_section = nullptr)\
54 {\
55 return serialize_map<false>(stg, hparent_section);\
56 }\
57 template<class t_storage> \
58 bool load( t_storage& stg, typename t_storage::hsection hparent_section = nullptr)\
59 {\
60 try{\
61 return serialize_map<false>(stg, hparent_section);\
62 }\
63 catch(const std::exception& err) \
64 { \
65 (void)(err); \
66 LOG_ERROR("Exception on unserializing: " << err.what());\
67 return false; \
68 }\
69 }\
70 /*template<typename T> T& this_type_resolver() { return *this; }*/ \
71 /*using this_type = std::result_of<decltype(this_type_resolver)>::type;*/ \
72 template<bool is_store, class t_storage> \
73 bool serialize_map(t_storage& stg, typename t_storage::hsection hparent_section) \
74 { \
75 decltype(*this) &this_ref = *this; \
76 (void) this_ref; // Suppress unused var warnings. Sometimes this var is used, sometimes not.
77
78#define KV_SERIALIZE_N(varialble, val_name) \
79 epee::serialization::selector<is_store>::serialize(this_ref.varialble, stg, hparent_section, val_name);
80
81#define KV_SERIALIZE_PARENT(type) \
82 do { \
83 if (!((type*)this)->serialize_map<is_store, t_storage>(stg, hparent_section)) \
84 return false; \
85 } while(0);
86
87 template<typename T> inline void serialize_default(const T &t, T v) { }
88 template<typename T> inline void serialize_default(T &t, T v) { t = v; }
89
90#define KV_SERIALIZE_OPT_N(variable, val_name, default_value) \
91 do { \
92 if (is_store && this_ref.variable == default_value) \
93 break; \
94 if (!epee::serialization::selector<is_store>::serialize(this_ref.variable, stg, hparent_section, val_name)) \
95 epee::serialize_default(this_ref.variable, default_value); \
96 } while (0);
97
98#define KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, val_name) \
99 epee::serialization::selector<is_store>::serialize_t_val_as_blob(this_ref.varialble, stg, hparent_section, val_name);
100
101#define KV_SERIALIZE_VAL_POD_AS_BLOB_N(variable, val_name) \
102 static_assert(std::is_trivially_copyable<decltype(this_ref.variable)>(), "t_type must be a trivially copyable type."); \
103 static_assert(std::is_standard_layout<decltype(this_ref.variable)>(), "t_type must be a standard layout type."); \
104 KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(variable, val_name)
105
106#define KV_SERIALIZE_VAL_POD_AS_BLOB_OPT_N(variable, val_name, default_value) \
107 do { \
108 static_assert(std::is_trivially_copyable<decltype(this_ref.variable)>(), "t_type must be a trivially copyable type."); \
109 static_assert(std::is_standard_layout<decltype(this_ref.variable)>(), "t_type must be a standard layout type."); \
110 bool ret = KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(variable, val_name) \
111 if (!ret) \
112 epee::serialize_default(this_ref.variable, default_value); \
113 } while(0);
114
115#define KV_SERIALIZE_CONTAINER_POD_AS_BLOB_N(varialble, val_name) \
116 epee::serialization::selector<is_store>::serialize_stl_container_pod_val_as_blob(this_ref.varialble, stg, hparent_section, val_name);
117
118#define END_KV_SERIALIZE_MAP() return true;}
119
120#define KV_SERIALIZE(varialble) KV_SERIALIZE_N(varialble, #varialble)
121#define KV_SERIALIZE_VAL_POD_AS_BLOB(varialble) KV_SERIALIZE_VAL_POD_AS_BLOB_N(varialble, #varialble)
122#define KV_SERIALIZE_VAL_POD_AS_BLOB_OPT(varialble, def) KV_SERIALIZE_VAL_POD_AS_BLOB_OPT_N(varialble, #varialble, def)
123#define KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(varialble) KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, #varialble) //skip is_trivially_copyable and is_standard_layout compile time check
124#define KV_SERIALIZE_CONTAINER_POD_AS_BLOB(varialble) KV_SERIALIZE_CONTAINER_POD_AS_BLOB_N(varialble, #varialble)
125#define KV_SERIALIZE_OPT(variable,default_value) KV_SERIALIZE_OPT_N(variable, #variable, default_value)
126
127}
128
129
130
131
TODO: (mj-xmr) This will be reduced in an another PR.
Definition byte_slice.h:40
void serialize_default(const T &t, T v)
Definition keyvalue_serialization.h:87
#define T(x)