33#include <boost/mpl/vector.hpp>
34#include <boost/mpl/contains_fwd.hpp>
36#undef MONERO_DEFAULT_LOG_CATEGORY
37#define MONERO_DEFAULT_LOG_CATEGORY "serialization"
43 template<
class C>
void hint_resize(C &container,
size_t size) {}
44 template<
class C>
void hint_resize(std::vector<C> &container,
size_t size) { container.reserve(size); }
50 template<
class t_type,
class t_storage>
51 static bool serialize_t_val_as_blob(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
53 std::string blob((
const char *)&
d,
sizeof(
d));
54 return stg.set_value(pname, std::move(blob), hparent_section);
57 template<
class t_type,
class t_storage>
61 if(!stg.get_value(pname, blob, hparent_section))
63 CHECK_AND_ASSERT_MES(blob.size() ==
sizeof(
d),
false,
"unserialize_t_val_as_blob: size of " <<
typeid(t_type).name() <<
" = " <<
sizeof(t_type) <<
", but stored blod size = " << blob.size() <<
", value name = " << pname);
64 d = *(
const t_type*)blob.data();
68 template<
class serializible_type,
class t_storage>
69 static bool serialize_t_obj(
const serializible_type& obj, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
71 typename t_storage::hsection hchild_section = stg.open_section(pname, hparent_section,
true);
72 CHECK_AND_ASSERT_MES(hchild_section,
false,
"serialize_t_obj: failed to open/create section " << pname);
73 return obj.store(stg, hchild_section);
76 template<
class serializible_type,
class t_storage>
77 static bool unserialize_t_obj(serializible_type& obj, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
79 typename t_storage::hsection hchild_section = stg.open_section(pname, hparent_section,
false);
80 if(!hchild_section)
return false;
81 return obj._load(stg, hchild_section);
84 template<
class stl_container,
class t_storage>
85 static bool serialize_stl_container_t_val (
const stl_container& container, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
87 using value_type =
typename stl_container::value_type;
89 if(!container.size())
return true;
90 typename stl_container::const_iterator it = container.begin();
91 typename t_storage::harray hval_array = stg.insert_first_value(pname, value_type(*it), hparent_section);
92 CHECK_AND_ASSERT_MES(hval_array,
false,
"failed to insert first value to storage");
94 for(;it!= container.end();it++)
95 stg.insert_next_value(hval_array, value_type(*it));
100 template<
class stl_container,
class t_storage>
104 typename stl_container::value_type exchange_val;
105 typename t_storage::harray hval_array = stg.get_first_value(pname, exchange_val, hparent_section);
106 if(!hval_array)
return false;
107 container.insert(container.end(), std::move(exchange_val));
108 while(stg.get_next_value(hval_array, exchange_val))
109 container.insert(container.end(), std::move(exchange_val));
112 template<
class stl_container,
class t_storage>
115 if(!container.size())
return true;
117 mb.resize(
sizeof(
typename stl_container::value_type)*container.size());
118 typename stl_container::value_type* p_elem = (
typename stl_container::value_type*)mb.data();
119 BOOST_FOREACH(
const typename stl_container::value_type& v, container)
124 return stg.set_value(pname, std::move(mb), hparent_section);
127 template<
class stl_container,
class t_storage>
132 bool res = stg.get_value(pname, buff, hparent_section);
135 size_t loaded_size = buff.size();
136 typename stl_container::value_type* pelem = (
typename stl_container::value_type*)buff.data();
137 CHECK_AND_ASSERT_MES(!(loaded_size%
sizeof(
typename stl_container::value_type)),
139 "size in blob " << loaded_size <<
" not have not zero modulo for sizeof(value_type) = " <<
sizeof(
typename stl_container::value_type) <<
", type " <<
typeid(
typename stl_container::value_type).
name());
140 size_t count = (loaded_size/
sizeof(
typename stl_container::value_type));
141 hint_resize(container,
count);
142 for(
size_t i = 0; i <
count; i++)
143 container.insert(container.end(), *(pelem++));
148 template<
class stl_container,
class t_storage>
152 if(!container.size())
return true;
153 typename stl_container::const_iterator it = container.begin();
154 typename t_storage::hsection hchild_section =
nullptr;
155 typename t_storage::harray hsec_array = stg.insert_first_section(pname, hchild_section, hparent_section);
156 CHECK_AND_ASSERT_MES(hsec_array && hchild_section,
false,
"failed to insert first section with section name " << pname);
157 res = it->store(stg, hchild_section);
159 for(;it!= container.end();it++)
161 stg.insert_next_section(hsec_array, hchild_section);
162 res |= it->store(stg, hchild_section);
167 template<
class stl_container,
class t_storage>
172 typename stl_container::value_type val =
typename stl_container::value_type();
173 typename t_storage::hsection hchild_section =
nullptr;
174 typename t_storage::harray hsec_array = stg.get_first_section(pname, hchild_section, hparent_section);
175 if(!hsec_array || !hchild_section)
return false;
176 res = val._load(stg, hchild_section);
177 container.insert(container.end(), val);
178 while(stg.get_next_section(hsec_array, hchild_section))
180 typename stl_container::value_type val_l =
typename stl_container::value_type();
181 res |= val_l._load(stg, hchild_section);
182 container.insert(container.end(), std::move(val_l));
193 template<
class t_type,
class t_storage>
194 static bool kv_serialize(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
196 return stg.set_value(pname, t_type(
d), hparent_section);
199 template<
class t_type,
class t_storage>
200 static bool kv_unserialize(t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
202 return stg.get_value(pname,
d, hparent_section);
205 template<
class t_type,
class t_storage>
206 static bool kv_serialize(
const std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
211 template<
class t_type,
class t_storage>
212 static bool kv_unserialize(std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
217 template<
class t_type,
class t_storage>
218 static bool kv_serialize(
const std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
223 template<
class t_type,
class t_storage>
224 static bool kv_unserialize(std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
229 template<
class t_type,
class t_storage>
230 static bool kv_serialize(
const std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
235 template<
class t_type,
class t_storage>
236 static bool kv_unserialize(std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
241 template<
class t_type,
class t_storage>
242 static bool kv_serialize(
const std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
247 template<
class t_type,
class t_storage>
248 static bool kv_unserialize(std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
257 template<
class t_type,
class t_storage>
258 static bool kv_serialize(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
263 template<
class t_type,
class t_storage>
264 static bool kv_unserialize(t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
270 template<
class t_type,
class t_storage>
271 static bool kv_serialize(
const std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
276 template<
class t_type,
class t_storage>
277 static bool kv_unserialize(std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
282 template<
class t_type,
class t_storage>
283 static bool kv_serialize(
const std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
288 template<
class t_type,
class t_storage>
289 static bool kv_unserialize(std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
294 template<
class t_type,
class t_storage>
295 static bool kv_serialize(
const std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
300 template<
class t_type,
class t_storage>
301 static bool kv_unserialize(std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
306 template<
class t_type,
class t_storage>
307 static bool kv_serialize(
const std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
312 template<
class t_type,
class t_storage>
313 static bool kv_unserialize(std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
318 template<
class t_storage>
319 struct base_serializable_types:
public boost::mpl::vector<uint64_t, uint32_t, uint16_t, uint8_t, int64_t, int32_t, int16_t, int8_t, double, bool, std::string, typename t_storage::meta_entry>::type
326 template<
class t_type,
class t_storage>
327 static bool serialize(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
332 template<
class t_type,
class t_storage>
338 template<
class t_type,
class t_storage>
339 static bool serialize_t_val_as_blob(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
349 template<
class t_type,
class t_storage>
350 static bool serialize(t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
354 template<
class t_type,
class t_storage>
360 template<
class t_type,
class t_storage>
367 template<
class t_type,
class t_storage>
368 bool kv_serialize(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
373 template<
class t_type,
class t_storage>
374 bool kv_unserialize(t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
379 template<
class t_type,
class t_storage>
380 bool kv_serialize(
const std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
385 template<
class t_type,
class t_storage>
386 bool kv_unserialize(std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
391 template<
class t_type,
class t_storage>
392 bool kv_serialize(
const std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
397 template<
class t_type,
class t_storage>
398 bool kv_unserialize(std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
403 template<
class t_type,
class t_storage>
404 bool kv_serialize(
const std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
409 template<
class t_type,
class t_storage>
410 bool kv_unserialize(std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
415 template<
class t_type,
class t_storage>
416 bool kv_serialize(
const std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
421 template<
class t_type,
class t_storage>
422 bool kv_unserialize(std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
int * count
Definition: gmock_stress_test.cc:176
const char * res
Definition: hmac_keccak.cpp:42
static bool serialize_stl_container_pod_val_as_blob(const stl_container &container, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:113
static bool unserialize_t_obj(serializible_type &obj, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:77
bool kv_unserialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:374
bool kv_serialize(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:368
static bool unserialize_stl_container_t_val(stl_container &container, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:101
static bool unserialize_stl_container_pod_val_as_blob(stl_container &container, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:128
static bool unserialize_t_val_as_blob(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:58
static bool serialize_t_val_as_blob(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:51
static bool serialize_t_obj(const serializible_type &obj, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:69
static bool unserialize_stl_container_t_obj(stl_container &container, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:168
static bool serialize_stl_container_t_val(const stl_container &container, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:85
static bool serialize_stl_container_t_obj(const stl_container &container, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:149
TODO: (mj-xmr) This will be reduced in an another PR.
Definition: byte_slice.h:40
Definition: binary_utils.h:36
const char * name
Definition: options.c:30
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1225
#define true
Definition: stdbool.h:36
#define false
Definition: stdbool.h:37
Definition: keyvalue_serialization_overloads.h:320
static bool kv_serialize(const std::vector< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:271
static bool kv_unserialize(std::set< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:313
static bool kv_serialize(const std::list< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:295
static bool kv_serialize(const std::deque< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:283
static bool kv_unserialize(std::vector< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:277
static bool kv_unserialize(std::deque< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:289
static bool kv_unserialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:264
static bool kv_unserialize(std::list< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:301
static bool kv_serialize(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:258
static bool kv_serialize(const std::set< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:307
static bool kv_unserialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:200
static bool kv_unserialize(std::deque< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:224
static bool kv_serialize(const std::set< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:242
static bool kv_serialize(const std::deque< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:218
static bool kv_unserialize(std::list< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:236
static bool kv_serialize(const std::vector< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:206
static bool kv_unserialize(std::set< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:248
static bool kv_serialize(const std::list< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:230
static bool kv_serialize(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:194
static bool kv_unserialize(std::vector< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:212
Definition: keyvalue_serialization_overloads.h:188
static bool serialize_t_val_as_blob(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:361
static bool serialize_stl_container_pod_val_as_blob(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:355
static bool serialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:350
static bool serialize(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:327
static bool serialize_t_val_as_blob(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:339
static bool serialize_stl_container_pod_val_as_blob(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition: keyvalue_serialization_overloads.h:333
Definition: keyvalue_serialization_overloads.h:322