diff --git a/CMake/CMakeLists.txt b/CMake/CMakeLists.txt index d740c2b4..8329a3c3 100644 --- a/CMake/CMakeLists.txt +++ b/CMake/CMakeLists.txt @@ -335,6 +335,7 @@ archive_test(test_unregistered) archive_test(test_unique_ptr) archive_test(test_valarray) archive_test(test_variant A) +archive_test(test_std_variant A) archive_test(test_vector A) polymorphic_archive_test(test_dll_exported polymorphic_derived1) diff --git a/doc/serialization.html b/doc/serialization.html index 72462556..9d0deeb7 100644 --- a/doc/serialization.html +++ b/doc/serialization.html @@ -919,6 +919,7 @@ As of this writing, the library contains serialization of the following boost cl
  • shared_ptr
  • auto_ptr (demo) +C++17 std::variant is supported as well. Others are being added to the list so check the boost files section and headers for new implementations!
    diff --git a/include/boost/serialization/std_variant.hpp b/include/boost/serialization/std_variant.hpp new file mode 100644 index 00000000..cd927072 --- /dev/null +++ b/include/boost/serialization/std_variant.hpp @@ -0,0 +1,204 @@ +#ifndef BOOST_SERIALIZATION_STD_VARIANT_HPP +#define BOOST_SERIALIZATION_STD_VARIANT_HPP + +// MS compatible compilers support #pragma once +#if defined(_MSC_VER) +# pragma once +#endif + +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 +// variant.hpp - non-intrusive serialization of variant types +// +// copyright (c) 2019 Samuel Debionne, ESRF +// +// Use, modification and distribution is subject to the Boost Software +// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) +// +// See http://www.boost.org for updates, documentation, and revision history. +// +// Widely inspired form boost::variant serialization +// + +#include + +#include + +#include + +#include +#include +#include + +namespace boost { +namespace serialization { + +template +struct std_variant_save_visitor +{ + std_variant_save_visitor(Archive& ar) : + m_ar(ar) + {} + template + void operator()(T const & value) const + { + m_ar << BOOST_SERIALIZATION_NVP(value); + } +private: + Archive & m_ar; +}; + + +template +struct std_variant_load_visitor +{ + std_variant_load_visitor(Archive& ar) : + m_ar(ar) + {} + template + void operator()(T & value) const + { + m_ar >> BOOST_SERIALIZATION_NVP(value); + } +private: + Archive & m_ar; +}; + +template +void save( + Archive & ar, + std::variant const & v, + unsigned int /*version*/ +){ + const std::size_t which = v.index(); + ar << BOOST_SERIALIZATION_NVP(which); + std_variant_save_visitor visitor(ar); + std::visit(visitor, v); +} + +// Minimalist metaprogramming for handling parameter pack +namespace mp { + namespace detail { + template + struct front_impl; + + template