| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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" |
| 33 |
#include "keyvalue_serialization_overloads.h" |
| 34 |
|
| 35 |
#undef MONERO_DEFAULT_LOG_CATEGORY |
| 36 |
#define MONERO_DEFAULT_LOG_CATEGORY "serialization" |
| 37 |
|
| 38 |
namespace epee |
| 39 |
{ |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
#define BEGIN_KV_SERIALIZE_MAP() \ |
| 44 |
public: \ |
| 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 |
\ |
| 71 |
\ |
| 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; |
| 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 |
|