upstream: remove serialization for tools::variant and add std::variant

This commit is contained in:
jeffro256
2025-04-19 21:29:26 -05:00
committed by akildemir
parent 871a687b52
commit b7488f7735
+43 -10
View File
@@ -38,11 +38,10 @@
#include <boost/variant/variant.hpp> #include <boost/variant/variant.hpp>
#include <boost/variant/apply_visitor.hpp> #include <boost/variant/apply_visitor.hpp>
#include <boost/variant/static_visitor.hpp> #include <boost/mpl/vector.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/if.hpp> #include <variant>
#include <boost/mpl/front.hpp>
#include <boost/mpl/pop_front.hpp>
#include "serialization.h" #include "serialization.h"
/*! \struct variant_serialization_triats /*! \struct variant_serialization_triats
@@ -100,13 +99,41 @@ struct variant_reader<Archive, Variant, TBegin, TBegin>
} }
}; };
template <template <bool> class Archive, typename... T> namespace detail
static bool do_serialize(Archive<false> &ar, boost::variant<T...> &v) { {
using types = typename boost::variant<T...>::types; template <typename T>
struct is_variant_like: std::false_type {};
template <typename... T>
struct is_variant_like<boost::variant<T...>>: std::true_type {};
template <typename... T>
struct is_variant_like<std::variant<T...>>: std::true_type {};
template <typename T>
struct get_variant_types {};
template <typename... T>
struct get_variant_types<boost::variant<T...>>
{ using types = typename boost::variant<T...>::types; };
template <typename... T>
struct get_variant_types<std::variant<T...>>
{ using types = boost::mpl::vector<T...>; };
} //namespace detail
template <
template <bool> class Archive,
typename Variant,
typename = std::enable_if_t<detail::is_variant_like<Variant>::value>
>
static bool do_serialize(Archive<false> &ar, Variant &v) {
using types = typename detail::get_variant_types<Variant>::types;
typename Archive<false>::variant_tag_type t; typename Archive<false>::variant_tag_type t;
ar.begin_variant(); ar.begin_variant();
ar.read_variant_tag(t); ar.read_variant_tag(t);
if(!variant_reader<Archive<false>, boost::variant<T...>, if(!variant_reader<Archive<false>, Variant,
typename boost::mpl::begin<types>::type, typename boost::mpl::begin<types>::type,
typename boost::mpl::end<types>::type>::read(ar, v, t)) typename boost::mpl::end<types>::type>::read(ar, v, t))
{ {
@@ -118,7 +145,7 @@ static bool do_serialize(Archive<false> &ar, boost::variant<T...> &v) {
} }
template <template <bool> class Archive> template <template <bool> class Archive>
struct variant_write_visitor : public boost::static_visitor<bool> struct variant_write_visitor
{ {
Archive<true> &ar; Archive<true> &ar;
@@ -144,3 +171,9 @@ static bool do_serialize(Archive<true> &ar, boost::variant<T...> &v)
{ {
return boost::apply_visitor(variant_write_visitor<Archive>(ar), v); return boost::apply_visitor(variant_write_visitor<Archive>(ar), v);
} }
template <template <bool> class Archive, typename... T>
static bool do_serialize(Archive<true> &ar, std::variant<T...> &v)
{
return std::visit(variant_write_visitor<Archive>(ar), v);
}