shared_ptr.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. #ifndef BOOST_SERIALIZATION_SHARED_PTR_HPP
  2. #define BOOST_SERIALIZATION_SHARED_PTR_HPP
  3. // MS compatible compilers support #pragma once
  4. #if defined(_MSC_VER) && (_MSC_VER >= 1020)
  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 2004 Robert Ramey and Martin Ecker
  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. #include <cstddef> // NULL
  15. #include <boost/config.hpp>
  16. #include <boost/mpl/integral_c.hpp>
  17. #include <boost/mpl/integral_c_tag.hpp>
  18. #include <boost/detail/workaround.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/serialization/split_free.hpp>
  21. #include <boost/serialization/nvp.hpp>
  22. #include <boost/serialization/version.hpp>
  23. #include <boost/serialization/tracking.hpp>
  24. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  25. // shared_ptr serialization traits
  26. // version 1 to distinguish from boost 1.32 version. Note: we can only do this
  27. // for a template when the compiler supports partial template specialization
  28. #ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
  29. namespace boost {
  30. namespace serialization{
  31. template<class T>
  32. struct version< ::boost::shared_ptr< T > > {
  33. typedef mpl::integral_c_tag tag;
  34. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
  35. typedef BOOST_DEDUCED_TYPENAME mpl::int_<1> type;
  36. #else
  37. typedef mpl::int_<1> type;
  38. #endif
  39. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570))
  40. BOOST_STATIC_CONSTANT(int, value = 1);
  41. #else
  42. BOOST_STATIC_CONSTANT(int, value = type::value);
  43. #endif
  44. };
  45. // don't track shared pointers
  46. template<class T>
  47. struct tracking_level< ::boost::shared_ptr< T > > {
  48. typedef mpl::integral_c_tag tag;
  49. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3206))
  50. typedef BOOST_DEDUCED_TYPENAME mpl::int_< ::boost::serialization::track_never> type;
  51. #else
  52. typedef mpl::int_< ::boost::serialization::track_never> type;
  53. #endif
  54. #if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x570))
  55. BOOST_STATIC_CONSTANT(int, value = ::boost::serialization::track_never);
  56. #else
  57. BOOST_STATIC_CONSTANT(int, value = type::value);
  58. #endif
  59. };
  60. }}
  61. #define BOOST_SERIALIZATION_SHARED_PTR(T)
  62. #else
  63. // define macro to let users of these compilers do this
  64. #define BOOST_SERIALIZATION_SHARED_PTR(T) \
  65. BOOST_CLASS_VERSION( \
  66. ::boost::shared_ptr< T >, \
  67. 1 \
  68. ) \
  69. BOOST_CLASS_TRACKING( \
  70. ::boost::shared_ptr< T >, \
  71. ::boost::serialization::track_never \
  72. ) \
  73. /**/
  74. #endif
  75. namespace boost {
  76. namespace serialization{
  77. struct null_deleter {
  78. void operator()(void const *) const {}
  79. };
  80. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  81. // serialization for shared_ptr
  82. template<class Archive, class T>
  83. inline void save(
  84. Archive & ar,
  85. const boost::shared_ptr< T > &t,
  86. const unsigned int /* file_version */
  87. ){
  88. // The most common cause of trapping here would be serializing
  89. // something like shared_ptr<int>. This occurs because int
  90. // is never tracked by default. Wrap int in a trackable type
  91. BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
  92. const T * t_ptr = t.get();
  93. ar << boost::serialization::make_nvp("px", t_ptr);
  94. }
  95. #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
  96. template<class Archive, class T>
  97. inline void load(
  98. Archive & ar,
  99. boost::shared_ptr< T > &t,
  100. const unsigned int file_version
  101. ){
  102. // The most common cause of trapping here would be serializing
  103. // something like shared_ptr<int>. This occurs because int
  104. // is never tracked by default. Wrap int in a trackable type
  105. BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
  106. T* r;
  107. if(file_version < 1){
  108. //ar.register_type(static_cast<
  109. // boost_132::detail::sp_counted_base_impl<T *, boost::checked_deleter< T > > *
  110. //>(NULL));
  111. ar.register_type(static_cast<
  112. boost_132::detail::sp_counted_base_impl<T *, null_deleter > *
  113. >(NULL));
  114. boost_132::shared_ptr< T > sp;
  115. ar >> boost::serialization::make_nvp("px", sp.px);
  116. ar >> boost::serialization::make_nvp("pn", sp.pn);
  117. // got to keep the sps around so the sp.pns don't disappear
  118. ar.append(sp);
  119. r = sp.get();
  120. }
  121. else{
  122. ar >> boost::serialization::make_nvp("px", r);
  123. }
  124. ar.reset(t,r);
  125. }
  126. #else
  127. template<class Archive, class T>
  128. inline void load(
  129. Archive & ar,
  130. boost::shared_ptr< T > &t,
  131. const unsigned int /*file_version*/
  132. ){
  133. // The most common cause of trapping here would be serializing
  134. // something like shared_ptr<int>. This occurs because int
  135. // is never tracked by default. Wrap int in a trackable type
  136. BOOST_STATIC_ASSERT((tracking_level< T >::value != track_never));
  137. T* r;
  138. ar >> boost::serialization::make_nvp("px", r);
  139. ar.reset(t,r);
  140. }
  141. #endif
  142. template<class Archive, class T>
  143. inline void serialize(
  144. Archive & ar,
  145. boost::shared_ptr< T > &t,
  146. const unsigned int file_version
  147. ){
  148. // correct shared_ptr serialization depends upon object tracking
  149. // being used.
  150. BOOST_STATIC_ASSERT(
  151. boost::serialization::tracking_level< T >::value
  152. != boost::serialization::track_never
  153. );
  154. boost::serialization::split_free(ar, t, file_version);
  155. }
  156. } // namespace serialization
  157. } // namespace boost
  158. #endif // BOOST_SERIALIZATION_SHARED_PTR_HPP