diff --git a/src/common/variant.h b/src/common/variant.h index bc2da5dbf..003b48bda 100644 --- a/src/common/variant.h +++ b/src/common/variant.h @@ -177,6 +177,41 @@ private: //member variables /// variant of all value types VType m_value; + +//friend functions +template +friend bool do_serialize(Archive &ar, variant &v); +}; + +template +class optional_variant: public variant +{ +public: +//constructors + /// default constructor + optional_variant() = default; + + /// construct from variant type (use enable_if to avoid issues with copy/move constructor) + template >, + optional_variant + >::value, + bool + >::type = true> + optional_variant(T &&value) : variant(std::forward(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 diff --git a/src/serialization/variant.h b/src/serialization/variant.h index 0e4e1e7c8..9674083dc 100644 --- a/src/serialization/variant.h +++ b/src/serialization/variant.h @@ -43,6 +43,7 @@ #include #include #include +#include "common/variant.h" #include "serialization.h" /*! \struct variant_serialization_triats @@ -144,3 +145,13 @@ static bool do_serialize(Archive &ar, boost::variant &v) { return boost::apply_visitor(variant_write_visitor(ar), v); } + +// implementation for tools::variant delegates to internal boost::variant member field +namespace tools +{ +template +bool do_serialize(Archive &ar, variant &v) +{ + return do_serialize(ar, v.m_value); +} +}