demo_shared_ptr.cpp 4.4 KB

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