1// Copyright 2023 Peter Dimov
2// Distributed under the Boost Software License, Version 1.0.
3// https://www.boost.org/LICENSE_1_0.txt
4
5#if defined(__clang__) && defined(__has_warning)
6# if __has_warning( "-Wdeprecated-copy" )
7# pragma clang diagnostic ignored "-Wdeprecated-copy"
8# endif
9#endif
10
11#include <boost/core/serialization.hpp>
12#include <new>
13
14struct X
15{
16 int v_;
17 explicit X( int v ): v_( v ) {}
18
19 template<class Ar> void serialize( Ar& /*ar*/, unsigned /*v*/ )
20 {
21 }
22};
23
24template<class Ar> void save_construct_data( Ar& ar, X const* t, unsigned /*v*/ )
25{
26 ar << t->v_;
27}
28
29template<class Ar> void load_construct_data( Ar& ar, X* t, unsigned /*v*/ )
30{
31 int v;
32 ar >> v;
33
34 ::new( t ) X( v );
35}
36
37struct Y
38{
39 X x1, x2;
40
41 explicit Y( int v1, int v2 ): x1( v1 ), x2( v2 ) {}
42
43private:
44
45 friend class boost::serialization::access;
46
47 template<class Ar> void load( Ar& ar, unsigned v )
48 {
49 boost::core::load_construct_data_adl( ar, &x1, v );
50 ar >> x1;
51
52 boost::core::load_construct_data_adl( ar, &x2, v );
53 ar >> x2;
54 }
55
56 template<class Ar> void save( Ar& ar, unsigned v ) const
57 {
58 boost::core::save_construct_data_adl( ar, &x1, v );
59 ar << x1;
60
61 boost::core::save_construct_data_adl( ar, &x2, v );
62 ar << x2;
63 }
64
65 template<class Ar> void serialize( Ar& ar, unsigned v )
66 {
67 boost::core::split_member( ar, *this, v );
68 }
69};
70
71#include <boost/archive/text_oarchive.hpp>
72#include <boost/archive/text_iarchive.hpp>
73#include <boost/core/lightweight_test.hpp>
74#include <sstream>
75#include <string>
76
77int main()
78{
79 std::ostringstream os;
80
81 Y y1( 7, 11 );
82
83 {
84 boost::archive::text_oarchive ar( os );
85 ar << y1;
86 }
87
88 std::string s = os.str();
89
90 Y y2( 0, 0 );
91
92 {
93 std::istringstream is( s );
94 boost::archive::text_iarchive ar( is );
95 ar >> y2;
96 }
97
98 BOOST_TEST_EQ( y1.x1.v_, y2.x1.v_ );
99 BOOST_TEST_EQ( y1.x2.v_, y2.x2.v_ );
100
101 return boost::report_errors();
102}
103

source code of boost/libs/core/test/serialization_construct_data_test.cpp