blob: fb347a7c75c2a4732c6e29cf8c5751fc2ec2bb41 [file] [log] [blame]
Brian Silverman88678712018-08-04 23:56:48 -07001// demo_shared_ptr.cpp : demonstrates adding serialization to a template
2
3// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . Polymorphic
4// derived pointer example by David Tonge.
5
6// Use, modification and distribution is subject to the Boost Software
7// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
8// http://www.boost.org/LICENSE_1_0.txt)
9//
10// See http://www.boost.org for updates, documentation, and revision history.
11
12#include <iomanip>
13#include <iostream>
14#include <cstddef> // NULL
15#include <fstream>
16#include <string>
17
18#include <cstdio> // remove
19#include <boost/config.hpp>
20#if defined(BOOST_NO_STDC_NAMESPACE)
21namespace std{
22 using ::remove;
23}
24#endif
25
26#include <boost/archive/text_oarchive.hpp>
27#include <boost/archive/text_iarchive.hpp>
28#include <boost/archive/tmpdir.hpp>
29
30#include <boost/serialization/shared_ptr.hpp>
31
32///////////////////////////
33// test shared_ptr serialization
34class A
35{
36private:
37 friend class boost::serialization::access;
38 int x;
39 template<class Archive>
40 void serialize(Archive & ar, const unsigned int /* file_version */){
41 ar & x;
42 }
43public:
44 static int count;
45 A(){++count;} // default constructor
46 virtual ~A(){--count;} // default destructor
47};
48
49BOOST_SERIALIZATION_SHARED_PTR(A)
50
51/////////////////
52// ADDITION BY DT
53class B : public A
54{
55private:
56 friend class boost::serialization::access;
57 int x;
58 template<class Archive>
59 void serialize(Archive & ar, const unsigned int /* file_version */){
60 ar & boost::serialization::base_object<A>(*this);
61 }
62public:
63 static int count;
64 B() : A() {};
65 virtual ~B() {};
66};
67
68BOOST_SERIALIZATION_SHARED_PTR(B)
69
70/////////////////
71
72int A::count = 0;
73
74void display(boost::shared_ptr<A> &spa, boost::shared_ptr<A> &spa1)
75{
76 std::cout << "a = 0x" << std::hex << spa.get() << " ";
77 if (spa.get()) std::cout << "is a " << typeid(*(spa.get())).name() << "* ";
78 std::cout << "use count = " << std::dec << spa.use_count() << std::endl;
79 std::cout << "a1 = 0x" << std::hex << spa1.get() << " ";
80 if (spa1.get()) std::cout << "is a " << typeid(*(spa1.get())).name() << "* ";
81 std::cout << "use count = " << std::dec << spa1.use_count() << std::endl;
82 std::cout << "unique element count = " << A::count << std::endl;
83}
84
85int main(int /* argc */, char * /*argv*/[])
86{
87 std::string filename(boost::archive::tmpdir());
88 filename += "/testfile";
89
90 // create a new shared pointer to ta new object of type A
91 boost::shared_ptr<A> spa(new A);
92 boost::shared_ptr<A> spa1;
93 spa1 = spa;
94 display(spa, spa1);
95 // serialize it
96 {
97 std::ofstream ofs(filename.c_str());
98 boost::archive::text_oarchive oa(ofs);
99 oa << spa;
100 oa << spa1;
101 }
102 // reset the shared pointer to NULL
103 // thereby destroying the object of type A
104 spa.reset();
105 spa1.reset();
106 display(spa, spa1);
107 // restore state to one equivalent to the original
108 // creating a new type A object
109 {
110 // open the archive
111 std::ifstream ifs(filename.c_str());
112 boost::archive::text_iarchive ia(ifs);
113
114 // restore the schedule from the archive
115 ia >> spa;
116 ia >> spa1;
117 }
118 display(spa, spa1);
119 spa.reset();
120 spa1.reset();
121
122 std::cout << std::endl;
123 std::cout << std::endl;
124 std::cout << "New tests" << std::endl;
125
126 /////////////////
127 // ADDITION BY DT
128 // create a new shared pointer to ta new object of type A
129 spa = boost::shared_ptr<A>(new B);
130 spa1 = spa;
131 display(spa, spa1);
132 // serialize it
133 {
134 std::ofstream ofs(filename.c_str());
135 boost::archive::text_oarchive oa(ofs);
136 oa.register_type(static_cast<B *>(NULL));
137 oa << spa;
138 oa << spa1;
139 }
140 // reset the shared pointer to NULL
141 // thereby destroying the object of type B
142 spa.reset();
143 spa1.reset();
144 display(spa, spa1);
145 // restore state to one equivalent to the original
146 // creating a new type B object
147 {
148 // open the archive
149 std::ifstream ifs(filename.c_str());
150 boost::archive::text_iarchive ia(ifs);
151
152 // restore the schedule from the archive
153 ia.register_type(static_cast<B *>(NULL));
154 ia >> spa;
155 ia >> spa1;
156 }
157 display(spa, spa1);
158 ///////////////
159 std::remove(filename.c_str());
160
161 // obj of type A gets destroyed
162 // as smart_ptr goes out of scope
163 return 0;
164}