diff --git a/include/boost/core/serialization.hpp b/include/boost/core/serialization.hpp new file mode 100644 index 0000000..b432646 --- /dev/null +++ b/include/boost/core/serialization.hpp @@ -0,0 +1,85 @@ +#ifndef BOOST_CORE_SERIALIZATION_HPP_INCLUDED +#define BOOST_CORE_SERIALIZATION_HPP_INCLUDED + +// MS compatible compilers support #pragma once + +#if defined(_MSC_VER) && (_MSC_VER >= 1020) +# pragma once +#endif + +// Copyright 2023 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt +// +// Utilities needed to implement serialization support +// without including a Boost.Serialization header + +#include +#include + +namespace boost +{ + +namespace serialization +{ + +// Forward declarations (needed for specializations) + +template struct version; + +// Our own version_type replacement. This has to be in +// the `serialization` namespace, because its only purpose +// is to add `serialization` as an associated namespace. + +struct core_version_type +{ + unsigned int version_; + + core_version_type( unsigned int version ): version_( version ) {} + operator unsigned int () const { return version_; } +}; + +} // namespace serialization + +namespace core +{ + +// nvp + +using serialization::nvp; +using serialization::make_nvp; + +// split_free + +namespace detail +{ + +template struct load_or_save; + +template<> struct load_or_save +{ + template void operator()( A& a, T& t, unsigned int v ) const + { + save( a, t, serialization::core_version_type( v ) ); + } +}; + +template<> struct load_or_save +{ + template void operator()( A& a, T& t, unsigned int v ) const + { + load( a, t, serialization::core_version_type( v ) ); + } +}; + +} // namespace detail + +template inline void split_free( A& a, T& t, unsigned int v ) +{ + detail::load_or_save< A::is_saving::value >()( a, t, v ); +} + +} // namespace core +} // namespace boost + +#endif // #ifndef BOOST_CORE_SERIALIZATION_HPP_INCLUDED diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 88c0889..327e38e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -31,6 +31,11 @@ set(BOOST_TEST_LINK_LIBRARIES Boost::core Boost::config Boost::move Boost::smart boost_test(TYPE run SOURCES fclose_deleter_test.cpp) +set(BOOST_TEST_LINK_LIBRARIES Boost::core Boost::serialization) + +boost_test(TYPE run SOURCES serialization_nvp_test.cpp) +boost_test(TYPE run SOURCES serialization_split_free_test.cpp) + endif() add_subdirectory(swap) diff --git a/test/Jamfile.v2 b/test/Jamfile.v2 index 6b642aa..f7a7319 100644 --- a/test/Jamfile.v2 +++ b/test/Jamfile.v2 @@ -363,5 +363,8 @@ run memory_resource_test.cpp ; run data_test.cpp ; run size_test.cpp ; +run serialization_nvp_test.cpp : : : /boost//serialization/off ; +run serialization_split_free_test.cpp : : : /boost//serialization/off ; + use-project /boost/core/swap : ./swap ; build-project ./swap ; diff --git a/test/serialization_nvp_test.cpp b/test/serialization_nvp_test.cpp new file mode 100644 index 0000000..cc73d20 --- /dev/null +++ b/test/serialization_nvp_test.cpp @@ -0,0 +1,49 @@ +// Copyright 2023 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include + +struct X +{ + int a, b; + + template void serialize( Ar& ar, unsigned /*v*/ ) + { + ar & boost::core::make_nvp( "a", a ); + ar & boost::core::make_nvp( "b", b ); + } +}; + +#include +#include +#include +#include +#include + +int main() +{ + std::ostringstream os; + + X x1 = { 7, 11 }; + + { + boost::archive::xml_oarchive ar( os ); + ar << boost::core::make_nvp( "x1", x1 ); + } + + std::string s = os.str(); + + X x2 = {}; + + { + std::istringstream is( s ); + boost::archive::xml_iarchive ar( is ); + ar >> boost::make_nvp( "x2", x2 ); + } + + BOOST_TEST_EQ( x1.a, x2.a ); + BOOST_TEST_EQ( x1.b, x2.b ); + + return boost::report_errors(); +} diff --git a/test/serialization_split_free_test.cpp b/test/serialization_split_free_test.cpp new file mode 100644 index 0000000..20fddab --- /dev/null +++ b/test/serialization_split_free_test.cpp @@ -0,0 +1,60 @@ +// Copyright 2023 Peter Dimov +// Distributed under the Boost Software License, Version 1.0. +// https://www.boost.org/LICENSE_1_0.txt + +#include + +struct X +{ + int a, b; +}; + +template void load( Ar& ar, X& x, unsigned /*v*/ ) +{ + ar >> x.a; + ar >> x.b; +} + +template void save( Ar& ar, X const& x, unsigned /*v*/ ) +{ + ar << x.a; + ar << x.b; +} + +template void serialize( Ar& ar, X& x, unsigned v ) +{ + boost::core::split_free( ar, x, v ); +} + +#include +#include +#include +#include +#include + +int main() +{ + std::ostringstream os; + + X x1 = { 7, 11 }; + + { + boost::archive::text_oarchive ar( os ); + ar << x1; + } + + std::string s = os.str(); + + X x2 = {}; + + { + std::istringstream is( s ); + boost::archive::text_iarchive ar( is ); + ar >> x2; + } + + BOOST_TEST_EQ( x1.a, x2.a ); + BOOST_TEST_EQ( x1.b, x2.b ); + + return boost::report_errors(); +}