shared_ptr.hpp 5.7 KB

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