1 /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2 // test_non_default_ctor2.cpp
3
4 // (C) Copyright 2002 Martin Ecker.
5 // Use, modification and distribution is subject to the Boost Software
6 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8
9 // should pass compilation and execution
10
11 // this tests complex usage of non-default constructor. Specifically
12 // the case where a constructor serializes a pointer member.
13
14 #include <fstream>
15
16 #include <boost/config.hpp>
17
18 #include "test_tools.hpp"
19
20 class IntValueHolder
21 {
22 public:
IntValueHolder()23 IntValueHolder()
24 : value(0)
25 {}
26
IntValueHolder(int newvalue)27 IntValueHolder(int newvalue)
28 : value(newvalue)
29 {}
30
GetValue() const31 int GetValue() const { return value; }
32
33 private:
34 int value;
35
36 friend class boost::serialization::access;
37
38 template <class ArchiveT>
serialize(ArchiveT & ar,const unsigned int)39 void serialize(ArchiveT& ar, const unsigned int /* file_version */)
40 {
41 ar & BOOST_SERIALIZATION_NVP(value);
42 }
43 };
44
45 class FloatValueHolder
46 {
47 public:
FloatValueHolder()48 FloatValueHolder()
49 : value(0)
50 {}
51
FloatValueHolder(float newvalue)52 FloatValueHolder(float newvalue)
53 : value(newvalue)
54 {}
55
GetValue() const56 float GetValue() const { return value; }
57
58 private:
59 float value;
60
61 friend class boost::serialization::access;
62
63 template <class ArchiveT>
serialize(ArchiveT & ar,const unsigned int)64 void serialize(ArchiveT& ar, const unsigned int /* file_version */)
65 {
66 ar & BOOST_SERIALIZATION_NVP(value);
67 }
68 };
69
70 class A
71 {
72 public:
A(const IntValueHolder & initialValue)73 A(const IntValueHolder& initialValue)
74 : value(initialValue), floatValue(new FloatValueHolder(10.0f))
75 {}
76
~A()77 ~A()
78 {
79 delete floatValue;
80 }
81
82 IntValueHolder value;
83 FloatValueHolder* floatValue;
84
85 private:
86 friend class boost::serialization::access;
87
88 template <class ArchiveT>
serialize(ArchiveT & ar,const unsigned int)89 void serialize(ArchiveT& ar, const unsigned int /* file_version */)
90 {
91 ar & BOOST_SERIALIZATION_NVP(floatValue);
92 }
93 };
94
95 namespace boost {
96 namespace serialization {
97
98 template <class ArchiveT>
save_construct_data(ArchiveT & archive,const A * p,const unsigned int)99 void save_construct_data(
100 ArchiveT& archive,
101 const A* p,
102 const unsigned int /*version*/
103 ){
104 archive & boost::serialization::make_nvp("initialValue", p->value);
105 }
106
107 template <class ArchiveT>
load_construct_data(ArchiveT & archive,A * p,const unsigned int)108 void load_construct_data(
109 ArchiveT& archive,
110 A* p,
111 const unsigned int /*version*/
112 ){
113 IntValueHolder initialValue;
114 archive & boost::serialization::make_nvp("initialValue", initialValue);
115
116 ::new (p) A(initialValue);
117 }
118
119 } // serialization
120 } // namespace boost
121
test_main(int,char * [])122 int test_main( int /* argc */, char* /* argv */[] )
123 {
124 const char * testfile = boost::archive::tmpnam(NULL);
125 BOOST_REQUIRE(NULL != testfile);
126 A* a = new A(5);
127
128 {
129 test_ostream os(testfile, TEST_STREAM_FLAGS);
130 test_oarchive oa(os, TEST_ARCHIVE_FLAGS);
131 oa << BOOST_SERIALIZATION_NVP(a);
132 }
133
134 A* a_new;
135
136 {
137 test_istream is(testfile, TEST_STREAM_FLAGS);
138 test_iarchive ia(is, TEST_ARCHIVE_FLAGS);
139 ia >> BOOST_SERIALIZATION_NVP(a_new);
140 }
141 delete a;
142 delete a_new;
143 return EXIT_SUCCESS;
144 }
145