blob: 0cca0afeba7ed0f816887e63bf7d60aa7a4ac7f4 [file] [log] [blame]
Brian Silverman88678712018-08-04 23:56:48 -07001/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
2// demo_auto_ptr.cpp
3
4// (C) Copyright 2002-4 Robert Ramey - http://www.rrsd.com .
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#include <list>
10#include <memory>
11#include <fstream>
12#include <string>
13
14#include <cstdio> // remove, std::autoptr inteface wrong in dinkumware
15#include <boost/config.hpp>
16#if defined(BOOST_NO_STDC_NAMESPACE)
17namespace std{
18 using ::remove;
19}
20#endif
21
22#include <boost/archive/tmpdir.hpp>
23#include <boost/archive/text_oarchive.hpp>
24#include <boost/archive/text_iarchive.hpp>
25
26#include <boost/serialization/split_free.hpp>
27
28namespace boost {
29namespace serialization {
30
31/////////////////////////////////////////////////////////////
32// implement serialization for auto_ptr< T >
33// note: this must be added to the boost namespace in order to
34// be called by the library
35template<class Archive, class T>
36inline void save(
37 Archive & ar,
38 const std::auto_ptr< T > &t,
39 const unsigned int file_version
40){
41 // only the raw pointer has to be saved
42 // the ref count is rebuilt automatically on load
43 const T * const tx = t.get();
44 ar << tx;
45}
46
47template<class Archive, class T>
48inline void load(
49 Archive & ar,
50 std::auto_ptr< T > &t,
51 const unsigned int file_version
52){
53 T *pTarget;
54 ar >> pTarget;
55 // note that the reset automagically maintains the reference count
56 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
57 t.release();
58 t = std::auto_ptr< T >(pTarget);
59 #else
60 t.reset(pTarget);
61 #endif
62}
63
64// split non-intrusive serialization function member into separate
65// non intrusive save/load member functions
66template<class Archive, class T>
67inline void serialize(
68 Archive & ar,
69 std::auto_ptr< T > &t,
70 const unsigned int file_version
71){
72 boost::serialization::split_free(ar, t, file_version);
73}
74
75} // namespace serialization
76} // namespace boost
77
78/////////////////////////////////////////////////////////////
79// test auto_ptr serialization
80class A
81{
82private:
83 friend class boost::serialization::access;
84 int x;
85 template<class Archive>
86 void serialize(Archive &ar, const unsigned int /* file_version */){
87 ar & x;
88 }
89public:
90 A(){} // default constructor
91 ~A(){} // default destructor
92};
93
94void save(const std::auto_ptr<A> & spa, const char *filename)
95{
96 std::ofstream ofs(filename);
97 boost::archive::text_oarchive oa(ofs);
98 oa << spa;
99}
100
101void load(std::auto_ptr<A> & spa, const char *filename)
102{
103 // open the archive
104 std::ifstream ifs(filename);
105 boost::archive::text_iarchive ia(ifs);
106
107 // restore the schedule from the archive
108 ia >> spa;
109}
110
111int main(int argc, char *argv[])
112{
113 std::string filename = boost::archive::tmpdir();
114 filename += "/testfile";
115
116 // create a new auto pointer to ta new object of type A
117 std::auto_ptr<A> spa(new A);
118 // serialize it
119 save(spa, filename.c_str());
120 // reset the auto pointer to NULL
121 // thereby destroying the object of type A
122 // note that the reset automagically maintains the reference count
123 #if BOOST_WORKAROUND(BOOST_DINKUMWARE_STDLIB, == 1)
124 spa.release();
125 #else
126 spa.reset();
127 #endif
128 // restore state to one equivalent to the original
129 // creating a new type A object
130 load(spa, filename.c_str());
131 // obj of type A gets destroyed
132 // as auto_ptr goes out of scope
133 std::remove(filename.c_str());
134 return 0;
135}