35#include <boost/mpl/vector.hpp>
36#include <boost/mpl/contains_fwd.hpp>
38#undef MONERO_DEFAULT_LOG_CATEGORY
39#define MONERO_DEFAULT_LOG_CATEGORY "serialization"
45 template<
class C>
void hint_resize(C &container,
size_t size) {}
46 template<
class C>
void hint_resize(std::vector<C> &container,
size_t size) { container.reserve(size); }
52 template<
class t_type,
class t_storage>
53 static bool serialize_t_val_as_blob(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
55 std::string blob((
const char *)&
d,
sizeof(
d));
56 return stg.set_value(pname, std::move(blob), hparent_section);
59 template<
class t_type,
class t_storage>
63 if(!stg.get_value(pname, blob, hparent_section))
65 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);
66 d = *(
const t_type*)blob.data();
70 template<
class serializible_type,
class t_storage>
71 static bool serialize_t_obj(
const serializible_type& obj, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
73 typename t_storage::hsection hchild_section = stg.open_section(pname, hparent_section,
true);
74 CHECK_AND_ASSERT_MES(hchild_section,
false,
"serialize_t_obj: failed to open/create section " << pname);
75 return obj.store(stg, hchild_section);
78 template<
class serializible_type,
class t_storage>
79 static bool unserialize_t_obj(serializible_type& obj, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
81 typename t_storage::hsection hchild_section = stg.open_section(pname, hparent_section,
false);
82 if(!hchild_section)
return false;
83 return obj._load(stg, hchild_section);
86 template<
class stl_container,
class t_storage>
87 static bool serialize_stl_container_t_val (
const stl_container& container, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
89 using value_type =
typename stl_container::value_type;
91 if(!container.size())
return true;
92 typename stl_container::const_iterator it = container.begin();
93 typename t_storage::harray hval_array = stg.insert_first_value(pname, value_type(*it), hparent_section);
94 CHECK_AND_ASSERT_MES(hval_array,
false,
"failed to insert first value to storage");
96 for(;it!= container.end();it++)
97 stg.insert_next_value(hval_array, value_type(*it));
102 template<
class stl_container,
class t_storage>
106 typename stl_container::value_type exchange_val;
107 typename t_storage::harray hval_array = stg.get_first_value(pname, exchange_val, hparent_section);
108 if(!hval_array)
return false;
109 container.insert(container.end(), std::move(exchange_val));
110 while(stg.get_next_value(hval_array, exchange_val))
111 container.insert(container.end(), std::move(exchange_val));
114 template<
class stl_container,
class t_storage>
117 if(!container.size())
return true;
119 mb.resize(
sizeof(
typename stl_container::value_type)*container.size());
120 typename stl_container::value_type* p_elem = (
typename stl_container::value_type*)mb.data();
121 BOOST_FOREACH(
const typename stl_container::value_type& v, container)
126 return stg.set_value(pname, std::move(mb), hparent_section);
129 template<
class stl_container,
class t_storage>
134 bool res = stg.get_value(pname, buff, hparent_section);
137 size_t loaded_size = buff.size();
138 typename stl_container::value_type* pelem = (
typename stl_container::value_type*)buff.data();
139 CHECK_AND_ASSERT_MES(!(loaded_size%
sizeof(
typename stl_container::value_type)),
141 "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());
142 size_t count = (loaded_size/
sizeof(
typename stl_container::value_type));
143 hint_resize(container, count);
144 for(
size_t i = 0; i < count; i++)
145 container.insert(container.end(), *(pelem++));
150 template<
class stl_container,
class t_storage>
154 if(!container.size())
return true;
155 typename stl_container::const_iterator it = container.begin();
156 typename t_storage::hsection hchild_section =
nullptr;
157 typename t_storage::harray hsec_array = stg.insert_first_section(pname, hchild_section, hparent_section);
158 CHECK_AND_ASSERT_MES(hsec_array && hchild_section,
false,
"failed to insert first section with section name " << pname);
159 res = it->store(stg, hchild_section);
161 for(;it!= container.end();it++)
163 stg.insert_next_section(hsec_array, hchild_section);
164 res |= it->store(stg, hchild_section);
169 template<
class stl_container,
class t_storage>
174 typename stl_container::value_type val =
typename stl_container::value_type();
175 typename t_storage::hsection hchild_section =
nullptr;
176 typename t_storage::harray hsec_array = stg.get_first_section(pname, hchild_section, hparent_section);
177 if(!hsec_array || !hchild_section)
return false;
178 res = val._load(stg, hchild_section);
179 container.insert(container.end(), val);
180 while(stg.get_next_section(hsec_array, hchild_section))
182 typename stl_container::value_type val_l =
typename stl_container::value_type();
183 res |= val_l._load(stg, hchild_section);
184 container.insert(container.end(), std::move(val_l));
195 template<
class t_type,
class t_storage>
196 static bool kv_serialize(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
198 return stg.set_value(pname, t_type(
d), hparent_section);
201 template<
class t_type,
class t_storage>
202 static bool kv_unserialize(t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
204 return stg.get_value(pname,
d, hparent_section);
207 template<
class t_type,
class t_storage>
208 static bool kv_serialize(
const std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
213 template<
class t_type,
class t_storage>
214 static bool kv_unserialize(std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
219 template<
class t_type,
class t_storage>
220 static bool kv_serialize(
const std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
225 template<
class t_type,
class t_storage>
226 static bool kv_unserialize(std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
231 template<
class t_type,
class t_storage>
232 static bool kv_serialize(
const std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
237 template<
class t_type,
class t_storage>
238 static bool kv_unserialize(std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
243 template<
class t_type,
class t_storage>
244 static bool kv_serialize(
const std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
249 template<
class t_type,
class t_storage>
250 static bool kv_unserialize(std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
259 template<
class t_type,
class t_storage>
260 static bool kv_serialize(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
265 template<
class t_type,
class t_storage>
266 static bool kv_unserialize(t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
272 template<
class t_type,
class t_storage>
273 static bool kv_serialize(
const std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
278 template<
class t_type,
class t_storage>
279 static bool kv_unserialize(std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
284 template<
class t_type,
class t_storage>
285 static bool kv_serialize(
const std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
290 template<
class t_type,
class t_storage>
291 static bool kv_unserialize(std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
296 template<
class t_type,
class t_storage>
297 static bool kv_serialize(
const std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
302 template<
class t_type,
class t_storage>
303 static bool kv_unserialize(std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
308 template<
class t_type,
class t_storage>
309 static bool kv_serialize(
const std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
314 template<
class t_type,
class t_storage>
315 static bool kv_unserialize(std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
320 template<
class t_storage>
321 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
328 template<
class t_type,
class t_storage>
329 static bool serialize(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
334 template<
class t_type,
class t_storage>
340 template<
class t_type,
class t_storage>
341 static bool serialize_t_val_as_blob(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
351 template<
class t_type,
class t_storage>
352 static bool serialize(t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
356 template<
class t_type,
class t_storage>
362 template<
class t_type,
class t_storage>
369 template<
class t_type,
class t_storage>
370 bool kv_serialize(
const t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
375 template<
class t_type,
class t_storage>
376 bool kv_unserialize(t_type&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
381 template<
class t_type,
class t_storage>
382 bool kv_serialize(
const std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
387 template<
class t_type,
class t_storage>
388 bool kv_unserialize(std::vector<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
393 template<
class t_type,
class t_storage>
394 bool kv_serialize(
const std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
399 template<
class t_type,
class t_storage>
400 bool kv_unserialize(std::deque<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
405 template<
class t_type,
class t_storage>
406 bool kv_serialize(
const std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
411 template<
class t_type,
class t_storage>
412 bool kv_unserialize(std::list<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
417 template<
class t_type,
class t_storage>
418 bool kv_serialize(
const std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
423 template<
class t_type,
class t_storage>
424 bool kv_unserialize(std::set<t_type>&
d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
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:115
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:79
bool kv_unserialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition keyvalue_serialization_overloads.h:376
bool kv_serialize(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition keyvalue_serialization_overloads.h:370
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:103
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:130
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:60
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:53
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:71
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:170
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:87
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:151
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
Definition keyvalue_serialization_overloads.h:322
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:273
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:315
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:297
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:285
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:279
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:291
static bool kv_unserialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition keyvalue_serialization_overloads.h:266
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:303
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:260
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:309
static bool kv_unserialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition keyvalue_serialization_overloads.h:202
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:226
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:244
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:220
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:238
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:208
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:250
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:232
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:196
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:214
Definition keyvalue_serialization_overloads.h:190
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:363
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:357
static bool serialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition keyvalue_serialization_overloads.h:352
static bool serialize(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
Definition keyvalue_serialization_overloads.h:329
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:341
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:335
Definition keyvalue_serialization_overloads.h:324