summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/serialization/example/demo_shared_ptr.cpp
blob: 63410fc88b4a7370bd2c2c4afecac1ddd1f51f86 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// demo_shared_ptr.cpp : demonstrates adding serialization to a template

// (C) Copyright 2002 Robert Ramey - http://www.rrsd.com . Polymorphic
// derived pointer example by David Tonge.

// Use, modification and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
//  See http://www.boost.org for updates, documentation, and revision history.

#include <iomanip>
#include <iostream>
#include <cstddef> // NULL
#include <fstream>
#include <string>

#include <cstdio> // remove
#include <boost/config.hpp>
#if defined(BOOST_NO_STDC_NAMESPACE)
namespace std{
    using ::remove;
}
#endif

#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/tmpdir.hpp>

#include <boost/serialization/shared_ptr.hpp>

///////////////////////////
// test shared_ptr serialization
class A
{
private:
    friend class boost::serialization::access;
    int x;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int /* file_version */){
        ar & x;
    }
public:
    static int count;
    A(){++count;}    // default constructor
    virtual ~A(){--count;}   // default destructor
};

BOOST_SERIALIZATION_SHARED_PTR(A)

/////////////////
// ADDITION BY DT
class B : public A
{
private:
    friend class boost::serialization::access;
    int x;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int /* file_version */){
        ar & boost::serialization::base_object<A>(*this);
    }
public:
    static int count;
    B() : A() {};
    virtual ~B() {};
};

BOOST_SERIALIZATION_SHARED_PTR(B)

/////////////////

int A::count = 0;

void display(boost::shared_ptr<A> &spa, boost::shared_ptr<A> &spa1)
{
    std::cout << "a = 0x" << std::hex << spa.get() << " ";
    if (spa.get()) std::cout << "is a " << typeid(*(spa.get())).name() << "* ";
    std::cout << "use count = " << std::dec << spa.use_count() << std::endl;
    std::cout << "a1 = 0x" << std::hex << spa1.get() << " ";
    if (spa1.get()) std::cout << "is a " << typeid(*(spa1.get())).name() << "* ";
    std::cout << "use count = " << std::dec << spa1.use_count() << std::endl;
    std::cout << "unique element count = " << A::count << std::endl;
}

int main(int /* argc */, char * /*argv*/[])
{
    std::string filename(boost::archive::tmpdir());
    filename += "/testfile";

    // create  a new shared pointer to ta new object of type A
    boost::shared_ptr<A> spa(new A);
    boost::shared_ptr<A> spa1;
    spa1 = spa;
    display(spa, spa1);
    // serialize it
    {
        std::ofstream ofs(filename.c_str());
        boost::archive::text_oarchive oa(ofs);
        oa << spa;
        oa << spa1;
    }
    // reset the shared pointer to NULL
    // thereby destroying the object of type A
    spa.reset();
    spa1.reset();
    display(spa, spa1);
    // restore state to one equivalent to the original
    // creating a new type A object
    {
        // open the archive
        std::ifstream ifs(filename.c_str());
        boost::archive::text_iarchive ia(ifs);

        // restore the schedule from the archive
        ia >> spa;
        ia >> spa1;
    }
    display(spa, spa1);
    spa.reset();
    spa1.reset();

    std::cout << std::endl;
    std::cout << std::endl;
    std::cout << "New tests" << std::endl;

    /////////////////
    // ADDITION BY DT
    // create  a new shared pointer to ta new object of type A
    spa = boost::shared_ptr<A>(new B);
    spa1 = spa;
    display(spa, spa1);
    // serialize it
    {
        std::ofstream ofs(filename.c_str());
        boost::archive::text_oarchive oa(ofs);
        oa.register_type(static_cast<B *>(NULL));
        oa << spa;
        oa << spa1;
    }
    // reset the shared pointer to NULL
    // thereby destroying the object of type B
    spa.reset();
    spa1.reset();
    display(spa, spa1);
    // restore state to one equivalent to the original
    // creating a new type B object
    {
        // open the archive
        std::ifstream ifs(filename.c_str());
        boost::archive::text_iarchive ia(ifs);

        // restore the schedule from the archive
        ia.register_type(static_cast<B *>(NULL));
        ia >> spa;
        ia >> spa1;
    }
    display(spa, spa1);
    ///////////////
    std::remove(filename.c_str());

    // obj of type A gets destroyed
    // as smart_ptr goes out of scope
    return 0;
}