mirror of
https://github.com/boostorg/core.git
synced 2025-05-11 13:13:55 +00:00
Add boost/core/serialization.hpp, serialization_nvp_test, serialization_split_free_test
This commit is contained in:
parent
249c5bece2
commit
89c5a78129
85
include/boost/core/serialization.hpp
Normal file
85
include/boost/core/serialization.hpp
Normal file
@ -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 <boost/core/nvp.hpp>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
|
namespace boost
|
||||||
|
{
|
||||||
|
|
||||||
|
namespace serialization
|
||||||
|
{
|
||||||
|
|
||||||
|
// Forward declarations (needed for specializations)
|
||||||
|
|
||||||
|
template<class T> 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<bool IsSaving> struct load_or_save;
|
||||||
|
|
||||||
|
template<> struct load_or_save<true>
|
||||||
|
{
|
||||||
|
template<class A, class T> void operator()( A& a, T& t, unsigned int v ) const
|
||||||
|
{
|
||||||
|
save( a, t, serialization::core_version_type( v ) );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template<> struct load_or_save<false>
|
||||||
|
{
|
||||||
|
template<class A, class T> void operator()( A& a, T& t, unsigned int v ) const
|
||||||
|
{
|
||||||
|
load( a, t, serialization::core_version_type( v ) );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace detail
|
||||||
|
|
||||||
|
template<class A, class T> 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
|
@ -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)
|
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()
|
endif()
|
||||||
|
|
||||||
add_subdirectory(swap)
|
add_subdirectory(swap)
|
||||||
|
@ -363,5 +363,8 @@ run memory_resource_test.cpp ;
|
|||||||
run data_test.cpp ;
|
run data_test.cpp ;
|
||||||
run size_test.cpp ;
|
run size_test.cpp ;
|
||||||
|
|
||||||
|
run serialization_nvp_test.cpp : : : <library>/boost//serialization/<warnings>off ;
|
||||||
|
run serialization_split_free_test.cpp : : : <library>/boost//serialization/<warnings>off ;
|
||||||
|
|
||||||
use-project /boost/core/swap : ./swap ;
|
use-project /boost/core/swap : ./swap ;
|
||||||
build-project ./swap ;
|
build-project ./swap ;
|
||||||
|
49
test/serialization_nvp_test.cpp
Normal file
49
test/serialization_nvp_test.cpp
Normal file
@ -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 <boost/core/serialization.hpp>
|
||||||
|
|
||||||
|
struct X
|
||||||
|
{
|
||||||
|
int a, b;
|
||||||
|
|
||||||
|
template<class Ar> void serialize( Ar& ar, unsigned /*v*/ )
|
||||||
|
{
|
||||||
|
ar & boost::core::make_nvp( "a", a );
|
||||||
|
ar & boost::core::make_nvp( "b", b );
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
#include <boost/archive/xml_oarchive.hpp>
|
||||||
|
#include <boost/archive/xml_iarchive.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
60
test/serialization_split_free_test.cpp
Normal file
60
test/serialization_split_free_test.cpp
Normal file
@ -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 <boost/core/serialization.hpp>
|
||||||
|
|
||||||
|
struct X
|
||||||
|
{
|
||||||
|
int a, b;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<class Ar> void load( Ar& ar, X& x, unsigned /*v*/ )
|
||||||
|
{
|
||||||
|
ar >> x.a;
|
||||||
|
ar >> x.b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Ar> void save( Ar& ar, X const& x, unsigned /*v*/ )
|
||||||
|
{
|
||||||
|
ar << x.a;
|
||||||
|
ar << x.b;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class Ar> void serialize( Ar& ar, X& x, unsigned v )
|
||||||
|
{
|
||||||
|
boost::core::split_free( ar, x, v );
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <boost/archive/text_oarchive.hpp>
|
||||||
|
#include <boost/archive/text_iarchive.hpp>
|
||||||
|
#include <boost/core/lightweight_test.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user