shared_ptr_132.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #ifndef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
  2. #define BOOST_SERIALIZATION_SHARED_PTR_132_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER)
  5. # pragma once
  6. #endif
  7. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  8. // shared_ptr.hpp: serialization for boost shared pointer
  9. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  10. // Use, modification and distribution is subject to the Boost Software
  11. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  12. // http://www.boost.org/LICENSE_1_0.txt)
  13. // See http://www.boost.org for updates, documentation, and revision history.
  14. // note: totally unadvised hack to gain access to private variables
  15. // in shared_ptr and shared_count. Unfortunately its the only way to
  16. // do this without changing shared_ptr and shared_count
  17. // the best we can do is to detect a conflict here
  18. #include <boost/config.hpp>
  19. #include <list>
  20. #include <cstddef> // NULL
  21. #include <boost/serialization/assume_abstract.hpp>
  22. #include <boost/serialization/split_free.hpp>
  23. #include <boost/serialization/nvp.hpp>
  24. #include <boost/serialization/tracking.hpp>
  25. #include <boost/serialization/void_cast.hpp>
  26. // mark base class as an (uncreatable) base class
  27. #include <boost/serialization/detail/shared_ptr_132.hpp>
  28. /////////////////////////////////////////////////////////////
  29. // Maintain a couple of lists of loaded shared pointers of the old previous
  30. // version (1.32)
  31. namespace boost_132 {
  32. namespace serialization {
  33. namespace detail {
  34. struct null_deleter {
  35. void operator()(void const *) const {}
  36. };
  37. } // namespace detail
  38. } // namespace serialization
  39. } // namespace boost_132
  40. /////////////////////////////////////////////////////////////
  41. // sp_counted_base_impl serialization
  42. namespace boost {
  43. namespace serialization {
  44. template<class Archive, class P, class D>
  45. inline void serialize(
  46. Archive & /* ar */,
  47. boost_132::detail::sp_counted_base_impl<P, D> & /* t */,
  48. const unsigned int /*file_version*/
  49. ){
  50. // register the relationship between each derived class
  51. // its polymorphic base
  52. boost::serialization::void_cast_register<
  53. boost_132::detail::sp_counted_base_impl<P, D>,
  54. boost_132::detail::sp_counted_base
  55. >(
  56. static_cast<boost_132::detail::sp_counted_base_impl<P, D> *>(NULL),
  57. static_cast<boost_132::detail::sp_counted_base *>(NULL)
  58. );
  59. }
  60. template<class Archive, class P, class D>
  61. inline void save_construct_data(
  62. Archive & ar,
  63. const
  64. boost_132::detail::sp_counted_base_impl<P, D> *t,
  65. const unsigned int /* file_version */
  66. ){
  67. // variables used for construction
  68. ar << boost::serialization::make_nvp("ptr", t->ptr);
  69. }
  70. template<class Archive, class P, class D>
  71. inline void load_construct_data(
  72. Archive & ar,
  73. boost_132::detail::sp_counted_base_impl<P, D> * t,
  74. const unsigned int /* file_version */
  75. ){
  76. P ptr_;
  77. ar >> boost::serialization::make_nvp("ptr", ptr_);
  78. // ::new(t)boost_132::detail::sp_counted_base_impl<P, D>(ptr_, D());
  79. // placement
  80. // note: the original ::new... above is replaced by the one here. This one
  81. // creates all new objects with a null_deleter so that after the archive
  82. // is finished loading and the shared_ptrs are destroyed - the underlying
  83. // raw pointers are NOT deleted. This is necessary as they are used by the
  84. // new system as well.
  85. ::new(t)boost_132::detail::sp_counted_base_impl<
  86. P,
  87. boost_132::serialization::detail::null_deleter
  88. >(
  89. ptr_, boost_132::serialization::detail::null_deleter()
  90. ); // placement new
  91. // compensate for that fact that a new shared count always is
  92. // initialized with one. the add_ref_copy below will increment it
  93. // every time its serialized so without this adjustment
  94. // the use and weak counts will be off by one.
  95. t->use_count_ = 0;
  96. }
  97. } // serialization
  98. } // namespace boost
  99. /////////////////////////////////////////////////////////////
  100. // shared_count serialization
  101. namespace boost {
  102. namespace serialization {
  103. template<class Archive>
  104. inline void save(
  105. Archive & ar,
  106. const boost_132::detail::shared_count &t,
  107. const unsigned int /* file_version */
  108. ){
  109. ar << boost::serialization::make_nvp("pi", t.pi_);
  110. }
  111. template<class Archive>
  112. inline void load(
  113. Archive & ar,
  114. boost_132::detail::shared_count &t,
  115. const unsigned int /* file_version */
  116. ){
  117. ar >> boost::serialization::make_nvp("pi", t.pi_);
  118. if(NULL != t.pi_)
  119. t.pi_->add_ref_copy();
  120. }
  121. } // serialization
  122. } // namespace boost
  123. BOOST_SERIALIZATION_SPLIT_FREE(boost_132::detail::shared_count)
  124. /////////////////////////////////////////////////////////////
  125. // implement serialization for shared_ptr< T >
  126. namespace boost {
  127. namespace serialization {
  128. template<class Archive, class T>
  129. inline void save(
  130. Archive & ar,
  131. const boost_132::shared_ptr< T > &t,
  132. const unsigned int /* file_version */
  133. ){
  134. // only the raw pointer has to be saved
  135. // the ref count is maintained automatically as shared pointers are loaded
  136. ar.register_type(static_cast<
  137. boost_132::detail::sp_counted_base_impl<T *, boost::checked_deleter< T > > *
  138. >(NULL));
  139. ar << boost::serialization::make_nvp("px", t.px);
  140. ar << boost::serialization::make_nvp("pn", t.pn);
  141. }
  142. template<class Archive, class T>
  143. inline void load(
  144. Archive & ar,
  145. boost_132::shared_ptr< T > &t,
  146. const unsigned int /* file_version */
  147. ){
  148. // only the raw pointer has to be saved
  149. // the ref count is maintained automatically as shared pointers are loaded
  150. ar.register_type(static_cast<
  151. boost_132::detail::sp_counted_base_impl<T *, boost::checked_deleter< T > > *
  152. >(NULL));
  153. ar >> boost::serialization::make_nvp("px", t.px);
  154. ar >> boost::serialization::make_nvp("pn", t.pn);
  155. }
  156. template<class Archive, class T>
  157. inline void serialize(
  158. Archive & ar,
  159. boost_132::shared_ptr< T > &t,
  160. const unsigned int file_version
  161. ){
  162. // correct shared_ptr serialization depends upon object tracking
  163. // being used.
  164. BOOST_STATIC_ASSERT(
  165. boost::serialization::tracking_level< T >::value
  166. != boost::serialization::track_never
  167. );
  168. boost::serialization::split_free(ar, t, file_version);
  169. }
  170. } // serialization
  171. } // namespace boost
  172. // note: change below uses null_deleter
  173. // This macro is used to export GUIDS for shared pointers to allow
  174. // the serialization system to export them properly. David Tonge
  175. #define BOOST_SHARED_POINTER_EXPORT_GUID(T, K) \
  176. typedef boost_132::detail::sp_counted_base_impl< \
  177. T *, \
  178. boost::checked_deleter< T > \
  179. > __shared_ptr_ ## T; \
  180. BOOST_CLASS_EXPORT_GUID(__shared_ptr_ ## T, "__shared_ptr_" K) \
  181. BOOST_CLASS_EXPORT_GUID(T, K) \
  182. /**/
  183. #define BOOST_SHARED_POINTER_EXPORT(T) \
  184. BOOST_SHARED_POINTER_EXPORT_GUID( \
  185. T, \
  186. BOOST_PP_STRINGIZE(T) \
  187. ) \
  188. /**/
  189. #endif // BOOST_SERIALIZATION_SHARED_PTR_132_HPP