Skip to content

Commit

Permalink
split variant into plain and optional, add variant serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
UkoeHB committed May 21, 2024
1 parent 35eb5c1 commit 5bfab6e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 11 deletions.
49 changes: 38 additions & 11 deletions src/common/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,14 @@ struct variant_static_visitor : public boost::static_visitor<ResultT>
};

template <typename... Types>
class variant final
class variant
{
using VType = boost::variant<boost::blank, Types...>;
using VType = boost::variant<Types...>;

public:
//constructors
/// default constructor
variant() = default;
variant(boost::none_t) : variant{} {} //act like boost::optional

/// construct from variant type (use enable_if to avoid issues with copy/move constructor)
template <typename T,
Expand All @@ -95,14 +94,7 @@ class variant final
>::type = true>
variant(T &&value) : m_value{std::forward<T>(value)} {}

//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 m_value.which() == 0; }

/// check the variant type
template <typename T>
bool is_type() const noexcept { return this->index() == this->type_index_of<T>(); }
Expand Down Expand Up @@ -136,7 +128,7 @@ class variant final
template <typename T>
static constexpr int type_index_of() noexcept
{
using types = boost::mpl::vector<boost::blank, Types...>;
using types = typename VType::types;
using elem = typename boost::mpl::find<types, T>::type;
using begin = typename boost::mpl::begin<types>::type;
return boost::mpl::distance<begin, elem>::value;
Expand All @@ -162,6 +154,41 @@ class variant final
//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; }
};

} //namespace tools
11 changes: 11 additions & 0 deletions src/serialization/variant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 5bfab6e

Please sign in to comment.