Merge pull request #9336

04fe6fa split variant into plain and optional, add variant serialization (koe)
This commit is contained in:
luigi1111 2024-12-23 10:26:23 -05:00
commit 44fef7903c
No known key found for this signature in database
GPG key ID: F4ACA0183641E010
2 changed files with 46 additions and 0 deletions

View file

@ -177,6 +177,41 @@ private:
//member variables
/// variant of all value types
VType m_value;
//friend functions
template <class Archive, typename... Ts>
friend bool do_serialize(Archive &ar, variant<Ts...> &v);
};
template <typename... Types>
class optional_variant: public variant<boost::blank, Types...>
{
public:
//constructors
/// default constructor
optional_variant() = default;
/// construct from variant type (use enable_if to avoid issues with copy/move constructor)
template <typename T,
typename std::enable_if<
!std::is_same<
std::remove_cv_t<std::remove_reference_t<T>>,
optional_variant<Types...>
>::value,
bool
>::type = true>
optional_variant(T &&value) : variant<boost::blank, Types...>(std::forward<T>(value)) {}
// construct like boost::optional
optional_variant(boost::none_t) {}
//overloaded operators
/// boolean operator: true if the variant isn't empty/uninitialized
explicit operator bool() const noexcept { return !this->is_empty(); }
//member functions
/// check if empty/uninitialized
bool is_empty() const noexcept { return this->index() == 0; }
};
template <typename... Types>

View file

@ -43,6 +43,7 @@
#include <boost/mpl/if.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/pop_front.hpp>
#include "common/variant.h"
#include "serialization.h"
/*! \struct variant_serialization_triats
@ -144,3 +145,13 @@ static bool do_serialize(Archive<true> &ar, boost::variant<T...> &v)
{
return boost::apply_visitor(variant_write_visitor<Archive>(ar), v);
}
// implementation for tools::variant delegates to internal boost::variant member field
namespace tools
{
template <class Archive, typename... Ts>
bool do_serialize(Archive &ar, variant<Ts...> &v)
{
return do_serialize(ar, v.m_value);
}
}