41 #include <boost/mpl/contains.hpp> 60 template<
class t_value>
63 template<
class t_value>
68 template<
class t_value>
70 template<
class t_value>
72 template<
class t_value>
74 template<
class t_value>
89 template<
class trace_policy>
96 hsection get_root_section() {
return &m_root;}
98 template<
class entry_type>
105 struct storage_block_header
117 std::stringstream ss;
121 CATCH_ENTRY(
"portable_storage::dump_as_json",
false)
128 CATCH_ENTRY(
"portable_storage::load_from_json",
false)
131 template<
class trace_policy>
141 std::stringstream ss;
146 ss.write((
const char*)&sbh,
sizeof(storage_block_header));
150 CATCH_ENTRY(
"portable_storage::store_to_binary",
false)
156 if(
source.size() <
sizeof(storage_block_header))
158 LOG_ERROR(
"portable_storage: wrong binary format, packet size = " <<
source.size() <<
" less than expected sizeof(storage_block_header)=" <<
sizeof(storage_block_header));
161 storage_block_header* pbuff = (storage_block_header*)
source.data();
166 LOG_ERROR(
"portable_storage: wrong binary format - signature mismatch");
171 LOG_ERROR(
"portable_storage: wrong binary format - unknown format ver = " << pbuff->m_ver);
176 buf_reader.
read(m_root);
178 CATCH_ENTRY(
"portable_storage::load_from_binary",
false);
185 hparent_section = hparent_section ? hparent_section:&m_root;
186 storage_entry* pentry = find_storage_entry(section_name, hparent_section);
189 if(!create_if_notexist)
191 return insert_new_section(section_name, hparent_section);
195 if(pentry->type() !=
typeid(
section))
197 if(create_if_notexist)
202 return &boost::get<section>(*pentry);
203 CATCH_ENTRY(
"portable_storage::open_section",
nullptr);
206 template<
class to_type>
211 template<
class from_type>
215 template<
class t_value>
218 BOOST_MPL_ASSERT(( boost::mpl::contains<storage_entry::types, t_value> ));
220 if(!hparent_section) hparent_section = &m_root;
221 storage_entry* pentry = find_storage_entry(value_name, hparent_section);
226 boost::apply_visitor(gvv, *pentry);
235 if(!hparent_section) hparent_section = &m_root;
236 storage_entry* pentry = find_storage_entry(value_name, hparent_section);
245 template<
class t_value>
248 BOOST_MPL_ASSERT(( boost::mpl::contains<boost::mpl::push_front<storage_entry::types, storage_entry>::type, t_value> ));
251 hparent_section = &m_root;
252 storage_entry* pentry = find_storage_entry(value_name, hparent_section);
255 pentry = insert_new_entry_get_storage_entry(value_name, hparent_section, v);
262 CATCH_ENTRY(
"portable_storage::template<>set_value",
false);
270 auto it = psection->
m_entries.find(pentry_name);
275 CATCH_ENTRY(
"portable_storage::find_storage_entry",
nullptr);
278 template<
class entry_type>
283 auto ins_res = psection->m_entries.insert(std::pair<std::string, storage_entry>(pentry_name, entry));
284 return &ins_res.first->second;
285 CATCH_ENTRY(
"portable_storage::insert_new_entry_get_storage_entry",
nullptr);
292 storage_entry* pse = insert_new_entry_get_storage_entry(pentry_name, psection, section());
293 if(!pse)
return nullptr;
294 return &boost::get<section>(*pse);
295 CATCH_ENTRY(
"portable_storage::insert_new_section",
nullptr);
298 template<
class to_type>
303 template<
class from_type>
306 const from_type* pv =
a.get_first_val();
314 template<
class t_value>
317 BOOST_MPL_ASSERT(( boost::mpl::contains<storage_entry::types, t_value> ));
319 if(!hparent_section) hparent_section = &m_root;
320 storage_entry* pentry = find_storage_entry(value_name, hparent_section);
325 array_entry& ar_entry = boost::get<array_entry>(*pentry);
328 if(!boost::apply_visitor(gfv, ar_entry))
334 template<
class to_type>
339 template<
class from_type>
343 const from_type* pv =
a.get_next_val();
352 template<
class t_value>
355 BOOST_MPL_ASSERT(( boost::mpl::contains<storage_entry::types, t_value> ));
360 if(!boost::apply_visitor(gnv, ar_entry))
366 template<
class t_value>
370 if(!hparent_section) hparent_section = &m_root;
371 storage_entry* pentry = find_storage_entry(value_name, hparent_section);
381 array_entry& arr = boost::get<array_entry>(*pentry);
388 CATCH_ENTRY(
"portable_storage::insert_first_value",
nullptr);
391 template<
class t_value>
403 CATCH_ENTRY(
"portable_storage::insert_next_value",
false);
411 if(!hparent_section) hparent_section = &m_root;
412 storage_entry* pentry = find_storage_entry(sec_name, hparent_section);
417 array_entry& ar_entry = boost::get<array_entry>(*pentry);
424 h_child_section = psec;
426 CATCH_ENTRY(
"portable_storage::get_first_section",
nullptr);
441 CATCH_ENTRY(
"portable_storage::get_next_section",
false);
448 if(!hparent_section) hparent_section = &m_root;
449 storage_entry* pentry = find_storage_entry(sec_name, hparent_section);
459 array_entry& ar_entry = boost::get<array_entry>(*pentry);
466 CATCH_ENTRY(
"portable_storage::insert_first_section",
nullptr);
475 false,
"unexpected type(not 'section') in insert_next_section, type: " << hsec_array->type().name());
478 hinserted_childsection = &sec_array.insert_next_value(
section());
480 CATCH_ENTRY(
"portable_storage::insert_next_section",
false);
bool delete_entry(const std::string &pentry_name, hsection hparent_section=nullptr)
harray get_first_value(const std::string &value_name, t_value &target, hsection hparent_section)
const CharType(& source)[N]
hsection open_section(const std::string §ion_name, hsection hparent_section, bool create_if_notexist=false)
bool operator()(const array_entry_t< from_type > &a)
bool get_value(const std::string &value_name, t_value &val, hsection hparent_section)
#define CHECK_AND_ASSERT_MES(expr, fail_ret_val, message)
#define CATCH_ENTRY(location, return_val)
t_entry_type & insert_first_val(const t_entry_type &v)
bool dump_as_xml(std::string &targetObj, const std::string &root_name="")
#define PORTABLE_STORAGE_SIGNATUREA
epee::serialization::hsection hsection
bool get_next_value(harray hval_array, t_value &target)
const t_entry_type * get_next_val() const
boost::variant< uint64_t, uint32_t, uint16_t, uint8_t, int64_t, int32_t, int16_t, int8_t, double, bool, std::string, section, array_entry > storage_entry
Non-owning sequence of data. Does not deep copy.
void convert_t(const from_type &from, to_type &to)
bool load_from_binary(const epee::span< const uint8_t > target)
bool operator()(const array_entry_t< from_type > &a)
harray insert_first_value(const std::string &value_name, const t_value &target, hsection hparent_section)
bool dump_as_json(std::string &targetObj, size_t indent=0, bool insert_newlines=true)
void dump_as_json(t_stream &strm, const array_entry &ae, size_t indent, bool insert_newlines)
bool get_next_section(harray hSecArray, hsection &h_child_section)
harray insert_first_section(const std::string &pSectionName, hsection &hinserted_childsection, hsection hparent_section)
bool load_from_json(const std::string &buff_json, t_storage &stg)
#define PORTABLE_STORAGE_SIGNATUREB
#define CHECK_AND_ASSERT(expr, fail_ret_val)
boost::make_recursive_variant< array_entry_t< section >, array_entry_t< uint64_t >, array_entry_t< uint32_t >, array_entry_t< uint16_t >, array_entry_t< uint8_t >, array_entry_t< int64_t >, array_entry_t< int32_t >, array_entry_t< int16_t >, array_entry_t< int8_t >, array_entry_t< double >, array_entry_t< bool >, array_entry_t< std::string >, array_entry_t< section >, array_entry_t< boost::recursive_variant_ > >::type array_entry
bool pack_entry_to_buff(t_stream &strm, const array_entry &ae)
bool load_from_json(const std::string &source)
void operator()(const from_type &v)
epee::serialization::harray harray
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
bool insert_next_value(harray hval_array, const t_value &target)
void read(void *target, size_t count)
virtual ~portable_storage()
t_entry_type & insert_next_value(const t_entry_type &v)
const t_entry_type * get_first_val() const
bool load_from_binary(const std::string &target)
bool store_to_binary(binarybuffer &target)
get_first_value_visitor(to_type &target)
get_next_value_visitor(to_type &target)
bool insert_next_section(harray hSecArray, hsection &hinserted_childsection)
harray get_first_section(const std::string &pSectionName, hsection &h_child_section, hsection hparent_section)
std::map< std::string, storage_entry > m_entries
get_value_visitor(to_type &target)
#define PORTABLE_STORAGE_FORMAT_VER
bool set_value(const std::string &value_name, const t_value &target, hsection hparent_section)