#ifndef BOOST_SERIALIZATION_VARIANT_HPP #define BOOST_SERIALIZATION_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) 2005 // troy d. straszheim // http://www.resophonic.com // // copyright (c) 2019 Samuel Debionne, ESRF // // copyright (c) 2023 // Robert Ramey // http://www.rrsd.com // // 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. // // thanks to Robert Ramey, Peter Dimov, and Richard Crossley. // #include #include #include #include #include #include #include // Boost Variant supports all C++ versions back to C++03 #include #include // Boost Variant2 supports all C++ versions back to C++11 #if BOOST_CXX_VERSION >= 201103L #include #include #endif // Boost Variant2 supports all C++ versions back to C++11 #ifndef BOOST_NO_CXX17_HDR_VARIANT #include //#include #endif #include #include #include #include // use visitor from boost::variant template typename Visitor::result_type visit( Visitor visitor, const boost::variant & t ){ return boost::apply_visitor(visitor, t); } template typename Visitor::result_type visit( Visitor visitor, const boost::variant & t, const boost::variant & u ){ return boost::apply_visitor(visitor, t, u); } namespace boost { namespace serialization { template struct variant_save_visitor : boost::static_visitor { 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 void save( Archive & ar, boost::variant const & v, unsigned int /*version*/ ){ int which = v.which(); ar << BOOST_SERIALIZATION_NVP(which); variant_save_visitor visitor(ar); visit(visitor, v); } #if BOOST_CXX_VERSION >= 201103L template void save( Archive & ar, boost::variant2::variant const & v, unsigned int /*version*/ ){ int which = v.index(); ar << BOOST_SERIALIZATION_NVP(which); const variant_save_visitor visitor(ar); visit(visitor, v); } #endif #ifndef BOOST_NO_CXX17_HDR_VARIANT template void save( Archive & ar, std::variant const & v, unsigned int /*version*/ ){ int which = v.index(); ar << BOOST_SERIALIZATION_NVP(which); const variant_save_visitor visitor(ar); visit(visitor, v); } #endif template struct variant_impl { struct load_null { template static void invoke( Archive & /*ar*/, std::size_t /*which*/, V & /*v*/, const unsigned int /*version*/ ){} }; struct load_member { template static void invoke( Archive & ar, std::size_t which, V & v, const unsigned int version ){ if(which == 0){ // note: A non-intrusive implementation (such as this one) // necessary has to copy the value. This wouldn't be necessary // with an implementation that de-serialized to the address of the // aligned storage included in the variant. typedef typename mpl::front::type head_type; head_type value; ar >> BOOST_SERIALIZATION_NVP(value); v = std::move(value);; head_type * new_address = & get(v); ar.reset_object_address(new_address, & value); return; } typedef typename mpl::pop_front::type type; variant_impl::load_impl(ar, which - 1, v, version); } }; template static void load_impl( Archive & ar, std::size_t which, V & v, const unsigned int version ){ typedef typename mpl::eval_if, mpl::identity, mpl::identity >::type typex; typex::invoke(ar, which, v, version); } }; // variant_impl template void load( Archive & ar, boost::variant& v, const unsigned int version ){ int which; typedef typename boost::variant::types types; ar >> BOOST_SERIALIZATION_NVP(which); if(which >= mpl::size::value){ // this might happen if a type was removed from the list of variant types boost::serialization::throw_exception( boost::archive::archive_exception( boost::archive::archive_exception::unsupported_version ) ); } variant_impl::load_impl(ar, which, v, version); } #if BOOST_CXX_VERSION >= 201103L template void load( Archive & ar, boost::variant2::variant & v, const unsigned int version ){ int which; typedef typename boost::variant::types types; ar >> BOOST_SERIALIZATION_NVP(which); if(which >= sizeof...(Types)){ // this might happen if a type was removed from the list of variant types boost::serialization::throw_exception( boost::archive::archive_exception( boost::archive::archive_exception::unsupported_version ) ); } variant_impl::load_impl(ar, which, v, version); } #endif #ifndef BOOST_NO_CXX17_HDR_VARIANT template void load( Archive & ar, std::variant & v, const unsigned int version ){ int which; typedef typename boost::variant::types types; ar >> BOOST_SERIALIZATION_NVP(which); if(which >= sizeof...(Types)){ // this might happen if a type was removed from the list of variant types boost::serialization::throw_exception( boost::archive::archive_exception( boost::archive::archive_exception::unsupported_version ) ); } variant_impl::load_impl(ar, which, v, version); } #endif template inline void serialize( Archive & ar, boost::variant & v, const unsigned int file_version ){ boost::serialization::split_free(ar,v,file_version); } #if BOOST_CXX_VERSION >= 201103L template inline void serialize( Archive & ar, boost::variant2::variant & v, const unsigned int file_version ){ boost::serialization::split_free(ar,v,file_version); } #endif #ifndef BOOST_NO_CXX17_HDR_VARIANT template inline void serialize( Archive & ar, std::variant & v, const unsigned int file_version ){ boost::serialization::split_free(ar,v,file_version); } #endif } // namespace serialization } // namespace boost #include namespace boost { namespace serialization { template struct tracking_level< variant >{ typedef mpl::integral_c_tag tag; typedef mpl::int_< ::boost::serialization::track_always> type; BOOST_STATIC_CONSTANT(int, value = type::value); }; #ifndef BOOST_NO_CXX17_HDR_VARIANT template struct tracking_level< std::variant >{ typedef mpl::integral_c_tag tag; typedef mpl::int_< ::boost::serialization::track_always> type; BOOST_STATIC_CONSTANT(int, value = type::value); }; #endif } // namespace serialization } // namespace boost #endif //BOOST_SERIALIZATION_VARIANT_HPP