// Copyright 2020 Peter Dimov // Distributed under the Boost Software License, Version 1.0. // https://www.boost.org/LICENSE_1_0.txt #define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include #include #include namespace app { template, class D2 = boost::describe::describe_bases, class D3 = boost::describe::describe_members, class D4 = boost::describe::describe_members, class En = std::enable_if_t< boost::mp11::mp_empty::value && boost::mp11::mp_empty::value && !std::is_union::value> > void serialize( Archive & ar, T & t, boost::serialization::version_type ) { int k = 0; // public bases: use base_object boost::mp11::mp_for_each([&](auto D){ using B = typename decltype(D)::type; char name[ 32 ]; std::sprintf( name, "base.%d", ++k ); ar & boost::make_nvp( name, boost::serialization::base_object( t ) ); }); // public (and protected) members boost::mp11::mp_for_each([&](auto D){ ar & boost::make_nvp( D.name, t.*D.pointer ); }); } struct A1 { int x; }; BOOST_DESCRIBE_STRUCT(A1, (), (x)) struct A2 { int y; }; BOOST_DESCRIBE_STRUCT(A2, (), (y)) struct B: public A1, public A2 { // these constructors aren't needed in C++17 B(): A1(), A2() {} B( int x, int y ): A1{ x }, A2{ y } {} }; BOOST_DESCRIBE_STRUCT(B, (A1, A2), ()) struct C { std::vector v; }; BOOST_DESCRIBE_STRUCT(C, (), (v)) } // namespace app #include #include #include #include #include #include int main() { app::C c1{ { { 1, 2 }, { 3, 4 }, { 5, 6 } } }; std::ostringstream os; { boost::archive::xml_oarchive ar( os ); ar << boost::make_nvp( "c1", c1 ); } std::string s = os.str(); std::cout << s << std::endl; app::C c2; { std::istringstream is( s ); boost::archive::xml_iarchive ar( is ); ar >> boost::make_nvp( "c2", c2 ); } { boost::archive::text_oarchive ar( std::cout ); ar << c2; } }