| 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 <cstddef> |
| 30 |
#include <cstdint> |
| 31 |
#include <set> |
| 32 |
#include <list> |
| 33 |
#include <vector> |
| 34 |
#include <deque> |
| 35 |
#include <boost/mpl/vector.hpp> |
| 36 |
#include <boost/mpl/contains_fwd.hpp> |
| 37 |
|
| 38 |
#undef MONERO_DEFAULT_LOG_CATEGORY |
| 39 |
#define MONERO_DEFAULT_LOG_CATEGORY "serialization" |
| 40 |
|
| 41 |
namespace epee |
| 42 |
{ |
| 43 |
namespace |
| 44 |
{ |
| 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); } |
| 47 |
} |
| 48 |
namespace serialization |
| 49 |
{ |
| 50 |
|
| 51 |
|
| 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) |
| 54 |
{ |
| 55 |
std::string blob((const char *)&d, sizeof(d)); |
| 56 |
return stg.set_value(pname, std::move(blob), hparent_section); |
| 57 |
} |
| 58 |
|
| 59 |
template<class t_type, class t_storage> |
| 60 |
static bool unserialize_t_val_as_blob(t_type& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) |
| 61 |
{ |
| 62 |
std::string blob; |
| 63 |
if(!stg.get_value(pname, blob, hparent_section)) |
| 64 |
return false; |
| 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(); |
| 67 |
return true; |
| 68 |
} |
| 69 |
|
| 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) |
| 72 |
{ |
| 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); |
| 76 |
} |
| 77 |
|
| 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) |
| 80 |
{ |
| 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); |
| 84 |
} |
| 85 |
|
| 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) |
| 88 |
{ |
| 89 |
using value_type = typename stl_container::value_type; |
| 90 |
|
| 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"); |
| 95 |
it++; |
| 96 |
for(;it!= container.end();it++) |
| 97 |
stg.insert_next_value(hval_array, value_type(*it)); |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
template<class stl_container, class t_storage> |
| 103 |
static bool unserialize_stl_container_t_val(stl_container& container, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) |
| 104 |
{ |
| 105 |
container.clear(); |
| 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)); |
| 112 |
return true; |
| 113 |
} |
| 114 |
template<class stl_container, class t_storage> |
| 115 |
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) |
| 116 |
{ |
| 117 |
if(!container.size()) return true; |
| 118 |
std::string mb; |
| 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) |
| 122 |
{ |
| 123 |
*p_elem = v; |
| 124 |
p_elem++; |
| 125 |
} |
| 126 |
return stg.set_value(pname, std::move(mb), hparent_section); |
| 127 |
} |
| 128 |
|
| 129 |
template<class stl_container, class t_storage> |
| 130 |
static bool unserialize_stl_container_pod_val_as_blob(stl_container& container, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) |
| 131 |
{ |
| 132 |
container.clear(); |
| 133 |
std::string buff; |
| 134 |
bool res = stg.get_value(pname, buff, hparent_section); |
| 135 |
if(res) |
| 136 |
{ |
| 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)), |
| 140 |
false, |
| 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++)); |
| 146 |
} |
| 147 |
return res; |
| 148 |
} |
| 149 |
|
| 150 |
template<class stl_container, class t_storage> |
| 151 |
static bool serialize_stl_container_t_obj (const stl_container& container, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) |
| 152 |
{ |
| 153 |
bool res = false; |
| 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); |
| 160 |
it++; |
| 161 |
for(;it!= container.end();it++) |
| 162 |
{ |
| 163 |
stg.insert_next_section(hsec_array, hchild_section); |
| 164 |
res |= it->store(stg, hchild_section); |
| 165 |
} |
| 166 |
return res; |
| 167 |
} |
| 168 |
|
| 169 |
template<class stl_container, class t_storage> |
| 170 |
static bool unserialize_stl_container_t_obj(stl_container& container, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) |
| 171 |
{ |
| 172 |
bool res = false; |
| 173 |
container.clear(); |
| 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)) |
| 181 |
{ |
| 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)); |
| 185 |
} |
| 186 |
return res; |
| 187 |
} |
| 188 |
|
| 189 |
template<bool> |
| 190 |
struct kv_serialization_overloads_impl_is_base_serializable_types; |
| 191 |
|
| 192 |
template<> |
| 193 |
struct kv_serialization_overloads_impl_is_base_serializable_types<true> |
| 194 |
{ |
| 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) |
| 197 |
{ |
| 198 |
return stg.set_value(pname, t_type(d), hparent_section); |
| 199 |
} |
| 200 |
|
| 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) |
| 203 |
{ |
| 204 |
return stg.get_value(pname, d, hparent_section); |
| 205 |
} |
| 206 |
|
| 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) |
| 209 |
{ |
| 210 |
return serialize_stl_container_t_val(d, stg, hparent_section, pname); |
| 211 |
} |
| 212 |
|
| 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) |
| 215 |
{ |
| 216 |
return unserialize_stl_container_t_val(d, stg, hparent_section, pname); |
| 217 |
} |
| 218 |
|
| 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) |
| 221 |
{ |
| 222 |
return serialize_stl_container_t_val(d, stg, hparent_section, pname); |
| 223 |
} |
| 224 |
|
| 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) |
| 227 |
{ |
| 228 |
return unserialize_stl_container_t_val(d, stg, hparent_section, pname); |
| 229 |
} |
| 230 |
|
| 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) |
| 233 |
{ |
| 234 |
return serialize_stl_container_t_val(d, stg, hparent_section, pname); |
| 235 |
} |
| 236 |
|
| 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) |
| 239 |
{ |
| 240 |
return unserialize_stl_container_t_val(d, stg, hparent_section, pname); |
| 241 |
} |
| 242 |
|
| 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) |
| 245 |
{ |
| 246 |
return serialize_stl_container_t_val(d, stg, hparent_section, pname); |
| 247 |
} |
| 248 |
|
| 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) |
| 251 |
{ |
| 252 |
return unserialize_stl_container_t_val(d, stg, hparent_section, pname); |
| 253 |
} |
| 254 |
|
| 255 |
}; |
| 256 |
template<> |
| 257 |
struct kv_serialization_overloads_impl_is_base_serializable_types<false> |
| 258 |
{ |
| 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) |
| 261 |
{ |
| 262 |
return serialize_t_obj(d, stg, hparent_section, pname); |
| 263 |
} |
| 264 |
|
| 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) |
| 267 |
{ |
| 268 |
return unserialize_t_obj(d, stg, hparent_section, pname); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 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) |
| 274 |
{ |
| 275 |
return serialize_stl_container_t_obj(d, stg, hparent_section, pname); |
| 276 |
} |
| 277 |
|
| 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) |
| 280 |
{ |
| 281 |
return unserialize_stl_container_t_obj(d, stg, hparent_section, pname); |
| 282 |
} |
| 283 |
|
| 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) |
| 286 |
{ |
| 287 |
return serialize_stl_container_t_obj(d, stg, hparent_section, pname); |
| 288 |
} |
| 289 |
|
| 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) |
| 292 |
{ |
| 293 |
return unserialize_stl_container_t_obj(d, stg, hparent_section, pname); |
| 294 |
} |
| 295 |
|
| 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) |
| 298 |
{ |
| 299 |
return serialize_stl_container_t_obj(d, stg, hparent_section, pname); |
| 300 |
} |
| 301 |
|
| 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) |
| 304 |
{ |
| 305 |
return unserialize_stl_container_t_obj(d, stg, hparent_section, pname); |
| 306 |
} |
| 307 |
|
| 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) |
| 310 |
{ |
| 311 |
return serialize_stl_container_t_obj(d, stg, hparent_section, pname); |
| 312 |
} |
| 313 |
|
| 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) |
| 316 |
{ |
| 317 |
return unserialize_stl_container_t_obj(d, stg, hparent_section, pname); |
| 318 |
} |
| 319 |
}; |
| 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 |
| 322 |
{}; |
| 323 |
|
| 324 |
template<bool> struct selector; |
| 325 |
template<> |
| 326 |
struct selector<true> |
| 327 |
{ |
| 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) |
| 330 |
{ |
| 331 |
return kv_serialize(d, stg, hparent_section, pname); |
| 332 |
} |
| 333 |
|
| 334 |
template<class t_type, class t_storage> |
| 335 |
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) |
| 336 |
{ |
| 337 |
return epee::serialization::serialize_stl_container_pod_val_as_blob(d, stg, hparent_section, pname); |
| 338 |
} |
| 339 |
|
| 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) |
| 342 |
{ |
| 343 |
return epee::serialization::serialize_t_val_as_blob(d, stg, hparent_section, pname); |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
}; |
| 348 |
template<> |
| 349 |
struct selector<false> |
| 350 |
{ |
| 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) |
| 353 |
{ |
| 354 |
return kv_unserialize(d, stg, hparent_section, pname); |
| 355 |
} |
| 356 |
template<class t_type, class t_storage> |
| 357 |
static bool serialize_stl_container_pod_val_as_blob(t_type& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) |
| 358 |
{ |
| 359 |
return epee::serialization::unserialize_stl_container_pod_val_as_blob(d, stg, hparent_section, pname); |
| 360 |
} |
| 361 |
|
| 362 |
template<class t_type, class t_storage> |
| 363 |
static bool serialize_t_val_as_blob(t_type& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname) |
| 364 |
{ |
| 365 |
return epee::serialization::unserialize_t_val_as_blob(d, stg, hparent_section, pname); |
| 366 |
} |
| 367 |
}; |
| 368 |
|
| 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) |
| 371 |
{ |
| 372 |
return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_serialize(d, stg, hparent_section, pname); |
| 373 |
} |
| 374 |
|
| 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) |
| 377 |
{ |
| 378 |
return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_unserialize(d, stg, hparent_section, pname); |
| 379 |
} |
| 380 |
|
| 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) |
| 383 |
{ |
| 384 |
return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_serialize(d, stg, hparent_section, pname); |
| 385 |
} |
| 386 |
|
| 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) |
| 389 |
{ |
| 390 |
return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_unserialize(d, stg, hparent_section, pname); |
| 391 |
} |
| 392 |
|
| 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) |
| 395 |
{ |
| 396 |
return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_serialize(d, stg, hparent_section, pname); |
| 397 |
} |
| 398 |
|
| 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) |
| 401 |
{ |
| 402 |
return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_unserialize(d, stg, hparent_section, pname); |
| 403 |
} |
| 404 |
|
| 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) |
| 407 |
{ |
| 408 |
return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_serialize(d, stg, hparent_section, pname); |
| 409 |
} |
| 410 |
|
| 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) |
| 413 |
{ |
| 414 |
return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_unserialize(d, stg, hparent_section, pname); |
| 415 |
} |
| 416 |
|
| 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) |
| 419 |
{ |
| 420 |
return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_serialize(d, stg, hparent_section, pname); |
| 421 |
} |
| 422 |
|
| 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) |
| 425 |
{ |
| 426 |
return kv_serialization_overloads_impl_is_base_serializable_types<boost::mpl::contains<base_serializable_types<t_storage>, typename std::remove_const<t_type>::type>::value>::kv_unserialize(d, stg, hparent_section, pname); |
| 427 |
} |
| 428 |
} |
| 429 |
} |