/Src/Dependencies/Boost/boost/archive/shared_ptr_helper.hpp

http://hadesmem.googlecode.com/ · C++ Header · 219 lines · 160 code · 26 blank · 33 comment · 6 complexity · b3942be2b079db7fa01c9205769875de MD5 · raw file

  1. #ifndef BOOST_ARCHIVE_SHARED_PTR_HELPER_HPP
  2. #define BOOST_ARCHIVE_SHARED_PTR_HELPER_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_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 <set>
  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/serialization/type_info_implementation.hpp>
  22. #include <boost/serialization/shared_ptr_132.hpp>
  23. #include <boost/serialization/throw_exception.hpp>
  24. #include <boost/archive/archive_exception.hpp>
  25. #include <boost/archive/detail/decl.hpp>
  26. #include <boost/archive/detail/abi_prefix.hpp> // must be the last headern
  27. namespace boost_132 {
  28. template<class T> class shared_ptr;
  29. }
  30. namespace boost {
  31. template<class T> class shared_ptr;
  32. namespace serialization {
  33. class extended_type_info;
  34. template<class Archive, class T>
  35. inline void load(
  36. Archive & ar,
  37. boost::shared_ptr< T > &t,
  38. const unsigned int file_version
  39. );
  40. }
  41. namespace archive{
  42. namespace detail {
  43. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  44. // a common class for holding various types of shared pointers
  45. class shared_ptr_helper {
  46. struct collection_type_compare {
  47. bool operator()(
  48. const shared_ptr<const void> &lhs,
  49. const shared_ptr<const void> &rhs
  50. )const{
  51. return lhs.get() < rhs.get();
  52. }
  53. };
  54. typedef std::set<
  55. boost::shared_ptr<const void>,
  56. collection_type_compare
  57. > collection_type;
  58. typedef collection_type::const_iterator iterator_type;
  59. // list of shared_pointers create accessable by raw pointer. This
  60. // is used to "match up" shared pointers loaded at different
  61. // points in the archive. Note, we delay construction until
  62. // it is actually used since this is by default included as
  63. // a "mix-in" even if shared_ptr isn't used.
  64. collection_type * m_pointers;
  65. struct null_deleter {
  66. void operator()(void const *) const {}
  67. };
  68. struct void_deleter {
  69. const boost::serialization::extended_type_info * m_eti;
  70. void_deleter(const boost::serialization::extended_type_info *eti) :
  71. m_eti(eti)
  72. {}
  73. void operator()(void *vp) const {
  74. m_eti->destroy(vp);
  75. }
  76. };
  77. #ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  78. public:
  79. #else
  80. template<class Archive, class T>
  81. friend inline void boost::serialization::load(
  82. Archive & ar,
  83. boost::shared_ptr< T > &t,
  84. const unsigned int file_version
  85. );
  86. #endif
  87. // #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
  88. // list of loaded pointers. This is used to be sure that the pointers
  89. // stay around long enough to be "matched" with other pointers loaded
  90. // by the same archive. These are created with a "null_deleter" so that
  91. // when this list is destroyed - the underlaying raw pointers are not
  92. // destroyed. This has to be done because the pointers are also held by
  93. // new system which is disjoint from this set. This is implemented
  94. // by a change in load_construct_data below. It makes this file suitable
  95. // only for loading pointers into a 1.33 or later boost system.
  96. std::list<boost_132::shared_ptr<const void> > * m_pointers_132;
  97. // #endif
  98. // returns pointer to object and an indicator whether this is a
  99. // new entry (true) or a previous one (false)
  100. BOOST_ARCHIVE_DECL(shared_ptr<void>)
  101. get_od(
  102. const void * od,
  103. const boost::serialization::extended_type_info * true_type,
  104. const boost::serialization::extended_type_info * this_type
  105. );
  106. BOOST_ARCHIVE_DECL(void)
  107. append(const boost::shared_ptr<const void> &);
  108. template<class T>
  109. struct non_polymorphic {
  110. static const boost::serialization::extended_type_info *
  111. get_object_identifier(T & t){
  112. return & boost::serialization::singleton<
  113. BOOST_DEDUCED_TYPENAME
  114. boost::serialization::type_info_implementation< T >::type
  115. >::get_const_instance();
  116. }
  117. };
  118. template<class T>
  119. struct polymorphic {
  120. static const boost::serialization::extended_type_info *
  121. get_object_identifier(T & t){
  122. return boost::serialization::singleton<
  123. BOOST_DEDUCED_TYPENAME
  124. boost::serialization::type_info_implementation< T >::type
  125. >::get_const_instance().get_derived_extended_type_info(t);
  126. }
  127. };
  128. public:
  129. template<class T>
  130. void reset(shared_ptr< T > & s, T * t){
  131. if(NULL == t){
  132. s.reset();
  133. return;
  134. }
  135. const boost::serialization::extended_type_info * this_type
  136. = & boost::serialization::type_info_implementation< T >::type
  137. ::get_const_instance();
  138. // get pointer to the most derived object. This is effectively
  139. // the object identifern
  140. typedef BOOST_DEDUCED_TYPENAME mpl::eval_if<
  141. is_polymorphic< T >,
  142. mpl::identity<polymorphic< T > >,
  143. mpl::identity<non_polymorphic< T > >
  144. >::type type;
  145. const boost::serialization::extended_type_info * true_type
  146. = type::get_object_identifier(*t);
  147. // note:if this exception is thrown, be sure that derived pointern
  148. // is either registered or exported.
  149. if(NULL == true_type)
  150. boost::serialization::throw_exception(
  151. archive_exception(
  152. archive_exception::unregistered_class,
  153. this_type->get_debug_info()
  154. )
  155. );
  156. shared_ptr<void> r =
  157. get_od(
  158. static_cast<const void *>(t),
  159. true_type,
  160. this_type
  161. );
  162. if(!r){
  163. s.reset(t);
  164. const void * od = void_downcast(
  165. *true_type,
  166. *this_type,
  167. static_cast<const void *>(t)
  168. );
  169. shared_ptr<const void> sp(s, od);
  170. append(sp);
  171. }
  172. else{
  173. s = shared_ptr< T >(
  174. r,
  175. static_cast<T *>(r.get())
  176. );
  177. }
  178. }
  179. // #ifdef BOOST_SERIALIZATION_SHARED_PTR_132_HPP
  180. BOOST_ARCHIVE_DECL(void)
  181. append(const boost_132::shared_ptr<const void> & t);
  182. // #endif
  183. public:
  184. BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY())
  185. shared_ptr_helper();
  186. BOOST_ARCHIVE_DECL(BOOST_PP_EMPTY())
  187. ~shared_ptr_helper();
  188. };
  189. } // namespace detail
  190. } // namespace archive
  191. } // namespace boost
  192. #include <boost/archive/detail/abi_suffix.hpp> // pops abi_suffix.hpp pragmas
  193. #endif // BOOST_ARCHIVE_SHARED_PTR_HELPER_HPP