demo_auto_ptr.cpp 3.5 KB

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