1
0
Fork 0
forked from lthn/blockchain

added support for std::optional to src/serialization and boost serialization

This commit is contained in:
sowle 2024-07-17 19:43:53 +02:00
parent 3cb471c46c
commit ef73204960
No known key found for this signature in database
GPG key ID: C07A24B2D89D49FC
2 changed files with 77 additions and 1 deletions

View file

@ -473,6 +473,29 @@ namespace epee
return r;
}
//-------------------------------------------------------------------------------------------------------------------
//std::optional
template<class t_type, class t_storage>
bool kv_serialize(const std::optional<t_type>& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname)
{
if(d.has_value())
{
return kv_serialize(*d, stg, hparent_section, pname);
}
return true;
}
//-------------------------------------------------------------------------------------------------------------------
template<class t_type, class t_storage>
bool kv_unserialize(std::optional<t_type>& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname)
{
d = t_type{};
bool r = kv_unserialize(*d, stg, hparent_section, pname);
if (!r)
{
d = std::nullopt;
}
return r;
}
//-------------------------------------------------------------------------------------------------------------------
//boost::shared_ptr
template<class t_type, class t_storage>
bool kv_serialize(const boost::shared_ptr<t_type>& d, t_storage& stg, typename t_storage::hsection hparent_section, const char* pname)

View file

@ -1,3 +1,4 @@
// Copyright (c) 2018-2024 Zano Project
// Copyright (c) 2014-2017 The The Louisdor Project
// Copyright (c) 2012-2013 The Cryptonote developers
// Distributed under the MIT/X11 software license, see the accompanying
@ -7,7 +8,7 @@
#include <boost/serialization/optional.hpp>
// boost::optional
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, boost::optional<T> &o)
{
@ -58,3 +59,55 @@ bool do_serialize(Archive<true> &ar, boost::optional<T> &v)
return true;
}
// std::optional
template <template <bool> class Archive, class T>
bool do_serialize(Archive<false> &ar, std::optional<T> &o)
{
//reading flag
bool is_none = false;
if (!::do_serialize(ar, is_none))
{
ar.stream().setstate(std::ios::failbit);
return false;
}
if (is_none)
{
o.reset();
return true;
}
o = T();
T& rval = o.value();
//reading value
if (!::do_serialize(ar, rval))
{
ar.stream().setstate(std::ios::failbit);
return false;
}
return true;
}
template <template <bool> class Archive, class T>
bool do_serialize(Archive<true> &ar, std::optional<T> &v)
{
//writing flag
bool is_none = !v.has_value();
if (!::do_serialize(ar, is_none))
{
ar.stream().setstate(std::ios::failbit);
return false;
}
if (is_none)
{
return true;
}
if (!::do_serialize(ar, v.value()))
{
ar.stream().setstate(std::ios::failbit);
return false;
}
return true;
}