| 1 | // 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 <fstream> |
|---|
| 15 | #include <string> |
|---|
| 16 | |
|---|
| 17 | #include <cstdio> // remove |
|---|
| 18 | #include <boost/config.hpp> |
|---|
| 19 | #if defined(BOOST_NO_STDC_NAMESPACE) |
|---|
| 20 | namespace std{ |
|---|
| 21 | using ::remove; |
|---|
| 22 | } |
|---|
| 23 | #endif |
|---|
| 24 | |
|---|
| 25 | #include <boost/archive/text_oarchive.hpp> |
|---|
| 26 | #include <boost/archive/text_iarchive.hpp> |
|---|
| 27 | #include <boost/archive/tmpdir.hpp> |
|---|
| 28 | |
|---|
| 29 | #include <boost/serialization/shared_ptr.hpp> |
|---|
| 30 | |
|---|
| 31 | /////////////////////////// |
|---|
| 32 | // test shared_ptr serialization |
|---|
| 33 | class A |
|---|
| 34 | { |
|---|
| 35 | private: |
|---|
| 36 | friend class boost::serialization::access; |
|---|
| 37 | int x; |
|---|
| 38 | template<class Archive> |
|---|
| 39 | void serialize(Archive & ar, const unsigned int /* file_version */){ |
|---|
| 40 | ar & x; |
|---|
| 41 | } |
|---|
| 42 | public: |
|---|
| 43 | static int count; |
|---|
| 44 | A::A(){++count;} // default constructor |
|---|
| 45 | virtual A::~A(){--count;} // default destructor |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | BOOST_SERIALIZATION_SHARED_PTR(A) |
|---|
| 49 | |
|---|
| 50 | ///////////////// |
|---|
| 51 | // ADDITION BY DT |
|---|
| 52 | class B : public A |
|---|
| 53 | { |
|---|
| 54 | private: |
|---|
| 55 | friend class boost::serialization::access; |
|---|
| 56 | int x; |
|---|
| 57 | template<class Archive> |
|---|
| 58 | void serialize(Archive & ar, const unsigned int /* file_version */){ |
|---|
| 59 | ar & boost::serialization::base_object<A>(*this); |
|---|
| 60 | } |
|---|
| 61 | public: |
|---|
| 62 | static int count; |
|---|
| 63 | B::B() : A() {}; |
|---|
| 64 | virtual B::~B() {}; |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | BOOST_SERIALIZATION_SHARED_PTR(B) |
|---|
| 68 | |
|---|
| 69 | ///////////////// |
|---|
| 70 | |
|---|
| 71 | int A::count = 0; |
|---|
| 72 | |
|---|
| 73 | void display(boost::shared_ptr<A> &spa, boost::shared_ptr<A> &spa1) |
|---|
| 74 | { |
|---|
| 75 | std::cout << "a = 0x" << std::hex << spa.get() << " "; |
|---|
| 76 | if (spa.get()) std::cout << "is a " << typeid(*(spa.get())).name() << "* "; |
|---|
| 77 | std::cout << "use count = " << std::dec << spa.use_count() << std::endl; |
|---|
| 78 | std::cout << "a1 = 0x" << std::hex << spa1.get() << " "; |
|---|
| 79 | if (spa1.get()) std::cout << "is a " << typeid(*(spa1.get())).name() << "* "; |
|---|
| 80 | std::cout << "use count = " << std::dec << spa1.use_count() << std::endl; |
|---|
| 81 | std::cout << "unique element count = " << A::count << std::endl; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | int main(int argc, char *argv[]) |
|---|
| 85 | { |
|---|
| 86 | std::string filename(boost::archive::tmpdir()); |
|---|
| 87 | filename += "/testfile"; |
|---|
| 88 | |
|---|
| 89 | // create a new shared pointer to ta new object of type A |
|---|
| 90 | boost::shared_ptr<A> spa(new A); |
|---|
| 91 | boost::shared_ptr<A> spa1; |
|---|
| 92 | spa1 = spa; |
|---|
| 93 | display(spa, spa1); |
|---|
| 94 | // serialize it |
|---|
| 95 | { |
|---|
| 96 | std::ofstream ofs(filename.c_str()); |
|---|
| 97 | boost::archive::text_oarchive oa(ofs); |
|---|
| 98 | oa << spa; |
|---|
| 99 | oa << spa1; |
|---|
| 100 | } |
|---|
| 101 | // reset the shared pointer to NULL |
|---|
| 102 | // thereby destroying the object of type A |
|---|
| 103 | spa.reset(); |
|---|
| 104 | spa1.reset(); |
|---|
| 105 | display(spa, spa1); |
|---|
| 106 | // restore state to one equivalent to the original |
|---|
| 107 | // creating a new type A object |
|---|
| 108 | { |
|---|
| 109 | // open the archive |
|---|
| 110 | std::ifstream ifs(filename.c_str()); |
|---|
| 111 | boost::archive::text_iarchive ia(ifs); |
|---|
| 112 | |
|---|
| 113 | // restore the schedule from the archive |
|---|
| 114 | ia >> spa; |
|---|
| 115 | ia >> spa1; |
|---|
| 116 | } |
|---|
| 117 | display(spa, spa1); |
|---|
| 118 | spa.reset(); |
|---|
| 119 | spa1.reset(); |
|---|
| 120 | |
|---|
| 121 | std::cout << std::endl; |
|---|
| 122 | std::cout << std::endl; |
|---|
| 123 | std::cout << "New tests" << std::endl; |
|---|
| 124 | |
|---|
| 125 | ///////////////// |
|---|
| 126 | // ADDITION BY DT |
|---|
| 127 | // create a new shared pointer to ta new object of type A |
|---|
| 128 | spa = boost::shared_ptr<A>(new B); |
|---|
| 129 | spa1 = spa; |
|---|
| 130 | display(spa, spa1); |
|---|
| 131 | // serialize it |
|---|
| 132 | { |
|---|
| 133 | std::ofstream ofs(filename.c_str()); |
|---|
| 134 | boost::archive::text_oarchive oa(ofs); |
|---|
| 135 | oa.register_type(static_cast<B *>(NULL)); |
|---|
| 136 | oa.register_type( |
|---|
| 137 | static_cast< |
|---|
| 138 | boost::detail::sp_counted_base_impl< |
|---|
| 139 | B *, boost::checked_deleter<B> |
|---|
| 140 | > * |
|---|
| 141 | >(NULL) |
|---|
| 142 | ); |
|---|
| 143 | oa << spa; |
|---|
| 144 | oa << spa1; |
|---|
| 145 | } |
|---|
| 146 | // reset the shared pointer to NULL |
|---|
| 147 | // thereby destroying the object of type B |
|---|
| 148 | spa.reset(); |
|---|
| 149 | spa1.reset(); |
|---|
| 150 | display(spa, spa1); |
|---|
| 151 | // restore state to one equivalent to the original |
|---|
| 152 | // creating a new type B object |
|---|
| 153 | { |
|---|
| 154 | // open the archive |
|---|
| 155 | std::ifstream ifs(filename.c_str()); |
|---|
| 156 | boost::archive::text_iarchive ia(ifs); |
|---|
| 157 | |
|---|
| 158 | // restore the schedule from the archive |
|---|
| 159 | ia.register_type(static_cast<B *>(NULL)); |
|---|
| 160 | ia.register_type( |
|---|
| 161 | static_cast< |
|---|
| 162 | boost::detail::sp_counted_base_impl< |
|---|
| 163 | B *, boost::checked_deleter<B> |
|---|
| 164 | > * |
|---|
| 165 | >(NULL) |
|---|
| 166 | ); |
|---|
| 167 | ia >> spa; |
|---|
| 168 | ia >> spa1; |
|---|
| 169 | } |
|---|
| 170 | display(spa, spa1); |
|---|
| 171 | /////////////// |
|---|
| 172 | std::remove(filename.c_str()); |
|---|
| 173 | |
|---|
| 174 | // obj of type A gets destroyed |
|---|
| 175 | // as smart_ptr goes out of scope |
|---|
| 176 | return 0; |
|---|
| 177 | } |
|---|