| 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 "portable_storage_base.h" |
| 30 |
#include "portable_storage_val_converters.h" |
| 31 |
#include "misc_log_ex.h" |
| 32 |
#include "span.h" |
| 33 |
|
| 34 |
#include <boost/mpl/contains.hpp> |
| 35 |
|
| 36 |
namespace epee |
| 37 |
{ |
| 38 |
class byte_slice; |
| 39 |
class byte_stream; |
| 40 |
namespace serialization |
| 41 |
{ |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class portable_storage |
| 46 |
{ |
| 47 |
public: |
| 48 |
typedef epee::serialization::hsection hsection; |
| 49 |
typedef epee::serialization::harray harray; |
| 50 |
typedef storage_entry meta_entry; |
| 51 |
|
| 52 |
struct limits_t |
| 53 |
{ |
| 54 |
size_t n_objects; |
| 55 |
size_t n_fields; |
| 56 |
size_t n_strings; |
| 57 |
}; |
| 58 |
|
| 59 |
portable_storage(){} |
| 60 |
virtual ~portable_storage(){} |
| 61 |
hsection open_section(const std::string& section_name, hsection hparent_section, bool create_if_notexist = false); |
| 62 |
template<class t_value> |
| 63 |
bool get_value(const std::string& value_name, t_value& val, hsection hparent_section); |
| 64 |
bool get_value(const std::string& value_name, storage_entry& val, hsection hparent_section); |
| 65 |
template<class t_value> |
| 66 |
bool set_value(const std::string& value_name, t_value&& target, hsection hparent_section); |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
template<class t_value> |
| 71 |
harray get_first_value(const std::string& value_name, t_value& target, hsection hparent_section); |
| 72 |
template<class t_value> |
| 73 |
bool get_next_value(harray hval_array, t_value& target); |
| 74 |
template<class t_value> |
| 75 |
harray insert_first_value(const std::string& value_name, t_value&& target, hsection hparent_section); |
| 76 |
template<class t_value> |
| 77 |
bool insert_next_value(harray hval_array, t_value&& target); |
| 78 |
|
| 79 |
harray get_first_section(const std::string& pSectionName, hsection& h_child_section, hsection hparent_section); |
| 80 |
bool get_next_section(harray hSecArray, hsection& h_child_section); |
| 81 |
harray insert_first_section(const std::string& pSectionName, hsection& hinserted_childsection, hsection hparent_section); |
| 82 |
bool insert_next_section(harray hSecArray, hsection& hinserted_childsection); |
| 83 |
|
| 84 |
|
| 85 |
bool delete_entry(const std::string& pentry_name, hsection hparent_section = nullptr); |
| 86 |
|
| 87 |
|
| 88 |
bool store_to_binary(byte_slice& target, std::size_t initial_buffer_size = 8192); |
| 89 |
bool store_to_binary(byte_stream& ss); |
| 90 |
bool load_from_binary(const epee::span<const uint8_t> target, const limits_t *limits = nullptr); |
| 91 |
bool load_from_binary(const std::string& target, const limits_t *limits = nullptr) |
| 92 |
{ |
| 93 |
return load_from_binary(epee::strspan<uint8_t>(target), limits); |
| 94 |
} |
| 95 |
|
| 96 |
template<class trace_policy> |
| 97 |
bool dump_as_xml(std::string& targetObj, const std::string& root_name = ""); |
| 98 |
bool dump_as_json(std::string& targetObj, size_t indent = 0, bool insert_newlines = true); |
| 99 |
bool load_from_json(const std::string& source); |
| 100 |
|
| 101 |
private: |
| 102 |
section m_root; |
| 103 |
hsection get_root_section() {return &m_root;} |
| 104 |
storage_entry* find_storage_entry(const std::string& pentry_name, hsection psection); |
| 105 |
template<class entry_type> |
| 106 |
storage_entry* insert_new_entry_get_storage_entry(const std::string& pentry_name, hsection psection, entry_type&& entry); |
| 107 |
|
| 108 |
hsection insert_new_section(const std::string& pentry_name, hsection psection); |
| 109 |
|
| 110 |
#pragma pack(push) |
| 111 |
#pragma pack(1) |
| 112 |
struct storage_block_header |
| 113 |
{ |
| 114 |
uint32_t m_signature_a; |
| 115 |
uint32_t m_signature_b; |
| 116 |
uint8_t m_ver; |
| 117 |
}; |
| 118 |
#pragma pack(pop) |
| 119 |
}; |
| 120 |
|
| 121 |
template<class trace_policy> |
| 122 |
bool portable_storage::dump_as_xml(std::string& targetObj, const std::string& root_name) |
| 123 |
{ |
| 124 |
return false; |
| 125 |
} |
| 126 |
|
| 127 |
template<class to_type> |
| 128 |
struct get_value_visitor: boost::static_visitor<void> |
| 129 |
{ |
| 130 |
to_type& m_target; |
| 131 |
get_value_visitor(to_type& target):m_target(target){} |
| 132 |
template<class from_type> |
| 133 |
void operator()(const from_type& v){convert_t(v, m_target);} |
| 134 |
}; |
| 135 |
|
| 136 |
template<class t_value> |
| 137 |
bool portable_storage::get_value(const std::string& value_name, t_value& val, hsection hparent_section) |
| 138 |
{ |
| 139 |
BOOST_MPL_ASSERT(( boost::mpl::contains<storage_entry::types, t_value> )); |
| 140 |
|
| 141 |
if(!hparent_section) hparent_section = &m_root; |
| 142 |
storage_entry* pentry = find_storage_entry(value_name, hparent_section); |
| 143 |
if(!pentry) |
| 144 |
return false; |
| 145 |
|
| 146 |
get_value_visitor<t_value> gvv(val); |
| 147 |
boost::apply_visitor(gvv, *pentry); |
| 148 |
return true; |
| 149 |
|
| 150 |
} |
| 151 |
|
| 152 |
template<class t_value> |
| 153 |
bool portable_storage::set_value(const std::string& value_name, t_value&& v, hsection hparent_section) |
| 154 |
{ |
| 155 |
using t_real_value = typename std::decay<t_value>::type; |
| 156 |
BOOST_MPL_ASSERT(( boost::mpl::contains<boost::mpl::push_front<storage_entry::types, storage_entry>::type, t_real_value> )); |
| 157 |
TRY_ENTRY(); |
| 158 |
if(!hparent_section) |
| 159 |
hparent_section = &m_root; |
| 160 |
storage_entry* pentry = find_storage_entry(value_name, hparent_section); |
| 161 |
if(!pentry) |
| 162 |
{ |
| 163 |
pentry = insert_new_entry_get_storage_entry(value_name, hparent_section, std::forward<t_value>(v)); |
| 164 |
if(!pentry) |
| 165 |
return false; |
| 166 |
return true; |
| 167 |
} |
| 168 |
*pentry = std::forward<t_value>(v); |
| 169 |
return true; |
| 170 |
CATCH_ENTRY("portable_storage::template<>set_value", false); |
| 171 |
} |
| 172 |
|
| 173 |
template<class entry_type> |
| 174 |
storage_entry* portable_storage::insert_new_entry_get_storage_entry(const std::string& pentry_name, hsection psection, entry_type&& entry) |
| 175 |
{ |
| 176 |
static_assert(std::is_rvalue_reference<entry_type&&>(), "unexpected copy of value"); |
| 177 |
TRY_ENTRY(); |
| 178 |
CHECK_AND_ASSERT(psection, nullptr); |
| 179 |
CHECK_AND_ASSERT(!pentry_name.empty(), nullptr); |
| 180 |
auto ins_res = psection->m_entries.emplace(pentry_name, std::forward<entry_type>(entry)); |
| 181 |
return &ins_res.first->second; |
| 182 |
CATCH_ENTRY("portable_storage::insert_new_entry_get_storage_entry", nullptr); |
| 183 |
} |
| 184 |
|
| 185 |
template<class to_type> |
| 186 |
struct get_first_value_visitor: boost::static_visitor<bool> |
| 187 |
{ |
| 188 |
to_type& m_target; |
| 189 |
get_first_value_visitor(to_type& target):m_target(target){} |
| 190 |
template<class from_type> |
| 191 |
bool operator()(const array_entry_t<from_type>& a) |
| 192 |
{ |
| 193 |
const from_type* pv = a.get_first_val(); |
| 194 |
if(!pv) |
| 195 |
return false; |
| 196 |
convert_t(*pv, m_target); |
| 197 |
return true; |
| 198 |
} |
| 199 |
}; |
| 200 |
|
| 201 |
template<class t_value> |
| 202 |
harray portable_storage::get_first_value(const std::string& value_name, t_value& target, hsection hparent_section) |
| 203 |
{ |
| 204 |
BOOST_MPL_ASSERT(( boost::mpl::contains<storage_entry::types, t_value> )); |
| 205 |
|
| 206 |
if(!hparent_section) hparent_section = &m_root; |
| 207 |
storage_entry* pentry = find_storage_entry(value_name, hparent_section); |
| 208 |
if(!pentry) |
| 209 |
return nullptr; |
| 210 |
if(pentry->type() != typeid(array_entry)) |
| 211 |
return nullptr; |
| 212 |
array_entry& ar_entry = boost::get<array_entry>(*pentry); |
| 213 |
|
| 214 |
get_first_value_visitor<t_value> gfv(target); |
| 215 |
if(!boost::apply_visitor(gfv, ar_entry)) |
| 216 |
return nullptr; |
| 217 |
return &ar_entry; |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
template<class to_type> |
| 222 |
struct get_next_value_visitor: boost::static_visitor<bool> |
| 223 |
{ |
| 224 |
to_type& m_target; |
| 225 |
get_next_value_visitor(to_type& target):m_target(target){} |
| 226 |
template<class from_type> |
| 227 |
bool operator()(const array_entry_t<from_type>& a) |
| 228 |
{ |
| 229 |
|
| 230 |
const from_type* pv = a.get_next_val(); |
| 231 |
if(!pv) |
| 232 |
return false; |
| 233 |
convert_t(*pv, m_target); |
| 234 |
return true; |
| 235 |
} |
| 236 |
}; |
| 237 |
|
| 238 |
template<class t_value> |
| 239 |
bool portable_storage::get_next_value(harray hval_array, t_value& target) |
| 240 |
{ |
| 241 |
BOOST_MPL_ASSERT(( boost::mpl::contains<storage_entry::types, t_value> )); |
| 242 |
|
| 243 |
CHECK_AND_ASSERT(hval_array, false); |
| 244 |
array_entry& ar_entry = *hval_array; |
| 245 |
get_next_value_visitor<t_value> gnv(target); |
| 246 |
if(!boost::apply_visitor(gnv, ar_entry)) |
| 247 |
return false; |
| 248 |
return true; |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
template<class t_value> |
| 253 |
harray portable_storage::insert_first_value(const std::string& value_name, t_value&& target, hsection hparent_section) |
| 254 |
{ |
| 255 |
using t_real_value = typename std::decay<t_value>::type; |
| 256 |
static_assert(std::is_rvalue_reference<t_value&&>(), "unexpected copy of value"); |
| 257 |
TRY_ENTRY(); |
| 258 |
if(!hparent_section) hparent_section = &m_root; |
| 259 |
storage_entry* pentry = find_storage_entry(value_name, hparent_section); |
| 260 |
if(!pentry) |
| 261 |
{ |
| 262 |
pentry = insert_new_entry_get_storage_entry(value_name, hparent_section, array_entry(array_entry_t<t_real_value>())); |
| 263 |
if(!pentry) |
| 264 |
return nullptr; |
| 265 |
} |
| 266 |
if(pentry->type() != typeid(array_entry)) |
| 267 |
*pentry = storage_entry(array_entry(array_entry_t<t_real_value>())); |
| 268 |
|
| 269 |
array_entry& arr = boost::get<array_entry>(*pentry); |
| 270 |
if(arr.type() != typeid(array_entry_t<t_real_value>)) |
| 271 |
arr = array_entry(array_entry_t<t_real_value>()); |
| 272 |
|
| 273 |
array_entry_t<t_real_value>& arr_typed = boost::get<array_entry_t<t_real_value> >(arr); |
| 274 |
arr_typed.insert_first_val(std::forward<t_value>(target)); |
| 275 |
return &arr; |
| 276 |
CATCH_ENTRY("portable_storage::insert_first_value", nullptr); |
| 277 |
} |
| 278 |
|
| 279 |
template<class t_value> |
| 280 |
bool portable_storage::insert_next_value(harray hval_array, t_value&& target) |
| 281 |
{ |
| 282 |
using t_real_value = typename std::decay<t_value>::type; |
| 283 |
static_assert(std::is_rvalue_reference<t_value&&>(), "unexpected copy of value"); |
| 284 |
TRY_ENTRY(); |
| 285 |
CHECK_AND_ASSERT(hval_array, false); |
| 286 |
|
| 287 |
CHECK_AND_ASSERT_MES(hval_array->type() == typeid(array_entry_t<t_real_value>), |
| 288 |
false, "unexpected type in insert_next_value: " << typeid(array_entry_t<t_real_value>).name()); |
| 289 |
|
| 290 |
array_entry_t<t_real_value>& arr_typed = boost::get<array_entry_t<t_real_value> >(*hval_array); |
| 291 |
arr_typed.insert_next_value(std::forward<t_value>(target)); |
| 292 |
return true; |
| 293 |
CATCH_ENTRY("portable_storage::insert_next_value", false); |
| 294 |
} |
| 295 |
} |
| 296 |
} |