Electroneum
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 <boost/utility/value_init.hpp>
30#include <boost/foreach.hpp>
31#include "misc_log_ex.h"
32#include "enableable.h"
34
35#undef ELECTRONEUM_DEFAULT_LOG_CATEGORY
36#define ELECTRONEUM_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 return serialize_map<true>(*this, st, hparent_section);\
49 }\
50 template<class t_storage> \
51 bool _load( t_storage& stg, typename t_storage::hsection hparent_section = nullptr)\
52 {\
53 return serialize_map<false>(*this, stg, hparent_section);\
54 }\
55 template<class t_storage> \
56 bool load( t_storage& stg, typename t_storage::hsection hparent_section = nullptr)\
57 {\
58 try{\
59 return serialize_map<false>(*this, stg, hparent_section);\
60 }\
61 catch(const std::exception& err) \
62 { \
63 (void)(err); \
64 LOG_ERROR("Exception on unserializing: " << err.what());\
65 return false; \
66 }\
67 }\
68 template<bool is_store, class this_type, class t_storage> \
69 static bool serialize_map(this_type& this_ref, t_storage& stg, typename t_storage::hsection hparent_section) \
70 {
71
72#define KV_SERIALIZE_N(varialble, val_name) \
73 epee::serialization::selector<is_store>::serialize(this_ref.varialble, stg, hparent_section, val_name);
74
75 template<typename T> inline void serialize_default(const T &t, T v) { }
76 template<typename T> inline void serialize_default(T &t, T v) { t = v; }
77
78#define KV_SERIALIZE_OPT_N(variable, val_name, default_value) \
79 do { \
80 if (!epee::serialization::selector<is_store>::serialize(this_ref.variable, stg, hparent_section, val_name)) \
81 epee::serialize_default(this_ref.variable, default_value); \
82 } while (0);
83
84#define KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, val_name) \
85 epee::serialization::selector<is_store>::serialize_t_val_as_blob(this_ref.varialble, stg, hparent_section, val_name);
86
87#define KV_SERIALIZE_VAL_POD_AS_BLOB_N(varialble, val_name) \
88 static_assert(std::is_pod<decltype(this_ref.varialble)>::value, "t_type must be a POD type."); \
89 KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, val_name)
90
91#define KV_SERIALIZE_VAL_POD_AS_BLOB_OPT_N(varialble, val_name, default_value) \
92 do { \
93 static_assert(std::is_pod<decltype(this_ref.varialble)>::value, "t_type must be a POD type."); \
94 bool ret = KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, val_name); \
95 if (!ret) \
96 epee::serialize_default(this_ref.varialble, default_value); \
97 } while(0);
98
99#define KV_SERIALIZE_CONTAINER_POD_AS_BLOB_N(varialble, val_name) \
100 epee::serialization::selector<is_store>::serialize_stl_container_pod_val_as_blob(this_ref.varialble, stg, hparent_section, val_name);
101
102#define END_KV_SERIALIZE_MAP() return true;}
103
104#define KV_SERIALIZE(varialble) KV_SERIALIZE_N(varialble, #varialble)
105#define KV_SERIALIZE_VAL_POD_AS_BLOB(varialble) KV_SERIALIZE_VAL_POD_AS_BLOB_N(varialble, #varialble)
106#define KV_SERIALIZE_VAL_POD_AS_BLOB_OPT(varialble, def) KV_SERIALIZE_VAL_POD_AS_BLOB_OPT_N(varialble, #varialble, def)
107#define KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE(varialble) KV_SERIALIZE_VAL_POD_AS_BLOB_FORCE_N(varialble, #varialble) //skip is_pod compile time check
108#define KV_SERIALIZE_CONTAINER_POD_AS_BLOB(varialble) KV_SERIALIZE_CONTAINER_POD_AS_BLOB_N(varialble, #varialble)
109#define KV_SERIALIZE_OPT(variable,default_value) KV_SERIALIZE_OPT_N(variable, #variable, default_value)
110
111}
112
113
114
115
void serialize_default(const T &t, T v)
#define T(x)