shared_ptr_helper.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #ifndef BOOST_SERIALIZATION_SHARED_PTR_HELPER_HPP
  2. #define BOOST_SERIALIZATION_SHARED_PTR_HELPER_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_helper.hpp: serialization for boost shared pointern
  9. // (C) Copyright 2004-2009 Robert Ramey, Martin Ecker and Takatoshi Kondo
  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 <list>
  16. #include <utility>
  17. #include <cstddef> // NULL
  18. #include <boost/config.hpp>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/type_traits/is_polymorphic.hpp>
  21. #include <boost/mpl/if.hpp>
  22. #include <boost/serialization/singleton.hpp>
  23. #include <boost/serialization/extended_type_info.hpp>
  24. #include <boost/serialization/throw_exception.hpp>
  25. #include <boost/serialization/type_info_implementation.hpp>
  26. #include <boost/archive/archive_exception.hpp>
  27. #include <boost/archive/detail/decl.hpp>
  28. #include <boost/archive/detail/abi_prefix.hpp> // must be the last headern
  29. namespace boost_132 {
  30. template<class T> class shared_ptr;
  31. }
  32. namespace boost {
  33. namespace serialization {
  34. #ifndef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  35. template<class Archive, template<class U> class SPT >
  36. void load(
  37. Archive & ar,
  38. SPT< class U > &t,
  39. const unsigned int file_version
  40. );
  41. #endif
  42. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  43. // a common class for holding various types of shared pointers
  44. template<template<class T> class SPT>
  45. class shared_ptr_helper {
  46. typedef std::map<
  47. const void *, // address of object
  48. SPT<const void> // address shared ptr to single instance
  49. > object_shared_pointer_map;
  50. // list of shared_pointers create accessable by raw pointer. This
  51. // is used to "match up" shared pointers loaded at different
  52. // points in the archive. Note, we delay construction until
  53. // it is actually used since this is by default included as
  54. // a "mix-in" even if shared_ptr isn't used.
  55. object_shared_pointer_map * m_o_sp;
  56. struct null_deleter {
  57. void operator()(void const *) const {}
  58. };
  59. #if defined(BOOST_NO_MEMBER_TEMPLATE_FRIENDS) \
  60. || defined(BOOST_MSVC) \
  61. || defined(__SUNPRO_CC)
  62. public:
  63. #else
  64. template<class Archive, class U>
  65. friend void boost::serialization::load(
  66. Archive & ar,
  67. SPT< U > &t,
  68. const unsigned int file_version
  69. );
  70. #endif
  71. #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
  72. // list of loaded pointers. This is used to be sure that the pointers
  73. // stay around long enough to be "matched" with other pointers loaded
  74. // by the same archive. These are created with a "null_deleter" so that
  75. // when this list is destroyed - the underlaying raw pointers are not
  76. // destroyed. This has to be done because the pointers are also held by
  77. // new system which is disjoint from this set. This is implemented
  78. // by a change in load_construct_data below. It makes this file suitable
  79. // only for loading pointers into a 1.33 or later boost system.
  80. std::list<boost_132::shared_ptr<const void> > * m_pointers_132;
  81. BOOST_ARCHIVE_DECL void
  82. append(const boost_132::shared_ptr<const void> & t){
  83. if(NULL == m_pointers_132)
  84. m_pointers_132 = new std::list<boost_132::shared_ptr<const void> >;
  85. m_pointers_132->push_back(t);
  86. }
  87. #endif
  88. struct non_polymorphic {
  89. template<class U>
  90. static const boost::serialization::extended_type_info *
  91. get_object_type(U & ){
  92. return & boost::serialization::singleton<
  93. typename
  94. boost::serialization::type_info_implementation< U >::type
  95. >::get_const_instance();
  96. }
  97. };
  98. struct polymorphic {
  99. template<class U>
  100. static const boost::serialization::extended_type_info *
  101. get_object_type(U & u){
  102. return boost::serialization::singleton<
  103. typename
  104. boost::serialization::type_info_implementation< U >::type
  105. >::get_const_instance().get_derived_extended_type_info(u);
  106. }
  107. };
  108. public:
  109. template<class T>
  110. void reset(SPT< T > & s, T * t){
  111. if(NULL == t){
  112. s.reset();
  113. return;
  114. }
  115. const boost::serialization::extended_type_info * this_type
  116. = & boost::serialization::type_info_implementation< T >::type
  117. ::get_const_instance();
  118. // get pointer to the most derived object's eti. This is effectively
  119. // the object type identifer
  120. typedef typename mpl::if_<
  121. is_polymorphic< T >,
  122. polymorphic,
  123. non_polymorphic
  124. >::type type;
  125. const boost::serialization::extended_type_info * true_type
  126. = type::get_object_type(*t);
  127. // note:if this exception is thrown, be sure that derived pointern
  128. // is either registered or exported.
  129. if(NULL == true_type)
  130. boost::serialization::throw_exception(
  131. boost::archive::archive_exception(
  132. boost::archive::archive_exception::unregistered_class,
  133. this_type->get_debug_info()
  134. )
  135. );
  136. // get void pointer to the most derived type
  137. // this uniquely identifies the object referred to
  138. // oid = "object identifier"
  139. const void * oid = void_downcast(
  140. *true_type,
  141. *this_type,
  142. t
  143. );
  144. if(NULL == oid)
  145. boost::serialization::throw_exception(
  146. boost::archive::archive_exception(
  147. boost::archive::archive_exception::unregistered_cast,
  148. true_type->get_debug_info(),
  149. this_type->get_debug_info()
  150. )
  151. );
  152. // make tracking array if necessary
  153. if(NULL == m_o_sp)
  154. m_o_sp = new object_shared_pointer_map;
  155. typename object_shared_pointer_map::iterator i = m_o_sp->find(oid);
  156. // if it's a new object
  157. if(i == m_o_sp->end()){
  158. s.reset(t);
  159. std::pair<typename object_shared_pointer_map::iterator, bool> result;
  160. result = m_o_sp->insert(std::make_pair(oid, s));
  161. BOOST_ASSERT(result.second);
  162. }
  163. // if the object has already been seen
  164. else{
  165. s = SPT<T>(i->second, t);
  166. }
  167. }
  168. shared_ptr_helper() :
  169. m_o_sp(NULL)
  170. #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
  171. , m_pointers_132(NULL)
  172. #endif
  173. {}
  174. virtual ~shared_ptr_helper(){
  175. if(NULL != m_o_sp)
  176. delete m_o_sp;
  177. #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
  178. if(NULL != m_pointers_132)
  179. delete m_pointers_132;
  180. #endif
  181. }
  182. };
  183. } // namespace serialization
  184. } // namespace boost
  185. #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
  186. #endif // BOOST_SERIALIZATION_SHARED_PTR_HELPER_HPP