33 #include <boost/mpl/vector.hpp> 34 #include <boost/mpl/contains_fwd.hpp> 36 #undef ELECTRONEUM_DEFAULT_LOG_CATEGORY 37 #define ELECTRONEUM_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(
const t_type& d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
53 return stg.set_value(pname, d, hparent_section);
56 template<
class t_type,
class t_storage>
57 static bool unserialize_t_val(t_type& d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
59 return stg.get_value(pname, d, hparent_section);
62 template<
class t_type,
class t_storage>
63 static bool serialize_t_val_as_blob(
const t_type& d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
66 return stg.set_value(pname, blob, hparent_section);
69 template<
class t_type,
class t_storage>
70 static bool unserialize_t_val_as_blob(t_type& d, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
73 if(!stg.get_value(pname, blob, hparent_section))
75 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);
76 d = *(
const t_type*)blob.data();
80 template<
class serializible_type,
class t_storage>
81 static bool serialize_t_obj(
const serializible_type& obj, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
83 typename t_storage::hsection hchild_section = stg.open_section(pname, hparent_section,
true);
84 CHECK_AND_ASSERT_MES(hchild_section,
false,
"serialize_t_obj: failed to open/create section " << pname);
85 return obj.store(stg, hchild_section);
88 template<
class serializible_type,
class t_storage>
89 static bool unserialize_t_obj(serializible_type& obj, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
91 typename t_storage::hsection hchild_section = stg.open_section(pname, hparent_section,
false);
92 if(!hchild_section)
return false;
93 return obj._load(stg, hchild_section);
96 template<
class serializible_type,
class t_storage>
101 return serialize_t_obj(obj.
v, stg, hparent_section, pname);
104 template<
class serializible_type,
class t_storage>
108 typename t_storage::hsection hchild_section = stg.open_section(pname, hparent_section,
false);
109 if(!hchild_section)
return false;
111 return obj.
v._load(stg, hchild_section);
114 template<
class stl_container,
class t_storage>
115 static bool serialize_stl_container_t_val (
const stl_container& container, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
117 if(!container.size())
return true;
118 typename stl_container::const_iterator it = container.begin();
119 typename t_storage::harray hval_array = stg.insert_first_value(pname, *it, hparent_section);
122 for(;it!= container.end();it++)
123 stg.insert_next_value(hval_array, *it);
128 template<
class stl_container,
class t_storage>
129 static bool unserialize_stl_container_t_val(stl_container& container, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
132 typename stl_container::value_type exchange_val;
133 typename t_storage::harray hval_array = stg.get_first_value(pname, exchange_val, hparent_section);
134 if(!hval_array)
return false;
135 container.insert(container.end(),
std::move(exchange_val));
136 while(stg.get_next_value(hval_array, exchange_val))
137 container.insert(container.end(),
std::move(exchange_val));
140 template<
class stl_container,
class t_storage>
141 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)
143 if(!container.size())
return true;
145 mb.resize(
sizeof(
typename stl_container::value_type)*container.size());
146 typename stl_container::value_type* p_elem = (
typename stl_container::value_type*)mb.data();
147 BOOST_FOREACH(
const typename stl_container::value_type& v, container)
152 return stg.set_value(pname, mb, hparent_section);
155 template<
class stl_container,
class t_storage>
156 static bool unserialize_stl_container_pod_val_as_blob(stl_container& container, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
160 bool res = stg.get_value(pname, buff, hparent_section);
163 size_t loaded_size = buff.size();
164 typename stl_container::value_type* pelem = (
typename stl_container::value_type*)buff.data();
167 "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());
168 size_t count = (loaded_size/
sizeof(
typename stl_container::value_type));
169 hint_resize(container,
count);
170 for(
size_t i = 0; i <
count; i++)
171 container.insert(container.end(), *(pelem++));
176 template<
class stl_container,
class t_storage>
177 static bool serialize_stl_container_t_obj (
const stl_container& container, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
180 if(!container.size())
return true;
181 typename stl_container::const_iterator it = container.begin();
183 typename t_storage::harray hsec_array = stg.insert_first_section(pname, hchild_section, hparent_section);
184 CHECK_AND_ASSERT_MES(hsec_array && hchild_section,
false,
"failed to insert first section with section name " << pname);
185 res = it->store(stg, hchild_section);
187 for(;it!= container.end();it++)
189 stg.insert_next_section(hsec_array, hchild_section);
190 res |= it->store(stg, hchild_section);
195 template<
class stl_container,
class t_storage>
196 static bool unserialize_stl_container_t_obj(stl_container& container, t_storage& stg,
typename t_storage::hsection hparent_section,
const char* pname)
200 typename stl_container::value_type val =
typename stl_container::value_type();
202 typename t_storage::harray hsec_array = stg.get_first_section(pname, hchild_section, hparent_section);
203 if(!hsec_array || !hchild_section)
return false;
204 res = val._load(stg, hchild_section);
205 container.insert(container.end(), val);
206 while(stg.get_next_section(hsec_array, hchild_section))
208 typename stl_container::value_type val_l =
typename stl_container::value_type();
209 res |= val_l._load(stg, hchild_section);
210 container.insert(container.end(),
std::move(val_l));
221 template<
class t_type,
class t_storage>
224 return stg.set_value(pname, d, hparent_section);
227 template<
class t_type,
class t_storage>
230 return stg.get_value(pname, d, hparent_section);
233 template<
class t_type,
class t_storage>
236 return serialize_stl_container_t_val(d, stg, hparent_section, pname);
239 template<
class t_type,
class t_storage>
242 return unserialize_stl_container_t_val(d, stg, hparent_section, pname);
245 template<
class t_type,
class t_storage>
248 return serialize_stl_container_t_val(d, stg, hparent_section, pname);
251 template<
class t_type,
class t_storage>
254 return unserialize_stl_container_t_val(d, stg, hparent_section, pname);
257 template<
class t_type,
class t_storage>
260 return serialize_stl_container_t_val(d, stg, hparent_section, pname);
263 template<
class t_type,
class t_storage>
266 return unserialize_stl_container_t_val(d, stg, hparent_section, pname);
269 template<
class t_type,
class t_storage>
272 return serialize_stl_container_t_val(d, stg, hparent_section, pname);
275 template<
class t_type,
class t_storage>
278 return unserialize_stl_container_t_val(d, stg, hparent_section, pname);
285 template<
class t_type,
class t_storage>
288 return serialize_t_obj(d, stg, hparent_section, pname);
291 template<
class t_type,
class t_storage>
294 return unserialize_t_obj(d, stg, hparent_section, pname);
298 template<
class t_type,
class t_storage>
301 return serialize_stl_container_t_obj(d, stg, hparent_section, pname);
304 template<
class t_type,
class t_storage>
307 return unserialize_stl_container_t_obj(d, stg, hparent_section, pname);
310 template<
class t_type,
class t_storage>
313 return serialize_stl_container_t_obj(d, stg, hparent_section, pname);
316 template<
class t_type,
class t_storage>
319 return unserialize_stl_container_t_obj(d, stg, hparent_section, pname);
322 template<
class t_type,
class t_storage>
325 return serialize_stl_container_t_obj(d, stg, hparent_section, pname);
328 template<
class t_type,
class t_storage>
331 return unserialize_stl_container_t_obj(d, stg, hparent_section, pname);
334 template<
class t_type,
class t_storage>
337 return serialize_stl_container_t_obj(d, stg, hparent_section, pname);
340 template<
class t_type,
class t_storage>
343 return unserialize_stl_container_t_obj(d, stg, hparent_section, pname);
346 template<
class t_storage>
347 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
354 template<
class t_type,
class t_storage>
360 template<
class t_type,
class t_storage>
363 return epee::serialization::serialize_stl_container_pod_val_as_blob(d, stg, hparent_section, pname);
366 template<
class t_type,
class t_storage>
369 return epee::serialization::serialize_t_val_as_blob(d, stg, hparent_section, pname);
377 template<
class t_type,
class t_storage>
382 template<
class t_type,
class t_storage>
385 return epee::serialization::unserialize_stl_container_pod_val_as_blob(d, stg, hparent_section, pname);
388 template<
class t_type,
class t_storage>
391 return epee::serialization::unserialize_t_val_as_blob(d, stg, hparent_section, pname);
395 template<
class t_type,
class t_storage>
401 template<
class t_type,
class t_storage>
407 template<
class t_type,
class t_storage>
413 template<
class t_type,
class t_storage>
419 template<
class t_type,
class t_storage>
425 template<
class t_type,
class t_storage>
431 template<
class t_type,
class t_storage>
437 template<
class t_type,
class t_storage>
443 template<
class t_type,
class t_storage>
449 template<
class t_type,
class t_storage>
static bool kv_unserialize(std::list< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_serialize(const std::list< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
bool kv_serialize(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_serialize(const std::deque< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool serialize(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
static bool kv_unserialize(std::list< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
bool kv_unserialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool serialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_unserialize(std::deque< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_unserialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_unserialize(std::deque< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
mdb_size_t count(MDB_cursor *cur)
static bool kv_serialize(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_serialize(const std::list< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_unserialize(std::set< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
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)
static bool serialize_stl_container_pod_val_as_blob(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_serialize(const std::set< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
const T & move(const T &t)
static bool kv_unserialize(std::set< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
const GenericPointer< typename T::ValueType > T2 value
static bool kv_unserialize(std::vector< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_serialize(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_serialize(const std::deque< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool serialize_t_val_as_blob(const t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_unserialize(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_serialize(const std::set< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool serialize_t_val_as_blob(t_type &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_serialize(const std::vector< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_serialize(const std::vector< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)
static bool kv_unserialize(std::vector< t_type > &d, t_storage &stg, typename t_storage::hsection hparent_section, const char *pname)