enable_shared_from_raw.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #ifndef BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED
  2. #define BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED
  3. //
  4. // enable_shared_from_raw.hpp
  5. //
  6. // Copyright 2002, 2009 Peter Dimov
  7. // Copyright 2008-2009 Frank Mori Hess
  8. //
  9. // Distributed under the Boost Software License, Version 1.0.
  10. // See accompanying file LICENSE_1_0.txt or copy at
  11. // http://www.boost.org/LICENSE_1_0.txt
  12. //
  13. #include <boost/config.hpp>
  14. #include <boost/shared_ptr.hpp>
  15. #include <boost/weak_ptr.hpp>
  16. #include <boost/assert.hpp>
  17. #include <boost/detail/workaround.hpp>
  18. namespace boost
  19. {
  20. template<typename T> boost::shared_ptr<T> shared_from_raw(T *);
  21. template<typename T> boost::weak_ptr<T> weak_from_raw(T *);
  22. namespace detail
  23. {
  24. template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
  25. } // namespace detail
  26. class enable_shared_from_raw
  27. {
  28. protected:
  29. enable_shared_from_raw()
  30. {
  31. }
  32. enable_shared_from_raw( enable_shared_from_raw const & )
  33. {
  34. }
  35. enable_shared_from_raw & operator=( enable_shared_from_raw const & )
  36. {
  37. return *this;
  38. }
  39. ~enable_shared_from_raw()
  40. {
  41. BOOST_ASSERT( shared_this_.use_count() <= 1 ); // make sure no dangling shared_ptr objects exist
  42. }
  43. private:
  44. void init_weak_once() const
  45. {
  46. if( weak_this_.expired() )
  47. {
  48. shared_this_.reset( static_cast<void*>(0), detail::esft2_deleter_wrapper() );
  49. weak_this_ = shared_this_;
  50. }
  51. }
  52. #ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  53. public:
  54. #else
  55. private:
  56. template<class Y> friend class shared_ptr;
  57. template<typename T> friend boost::shared_ptr<T> shared_from_raw(T *);
  58. template<typename T> friend boost::weak_ptr<T> weak_from_raw(T *);
  59. template< class X, class Y > friend inline void detail::sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe );
  60. #endif
  61. shared_ptr<void> shared_from_this()
  62. {
  63. init_weak_once();
  64. return shared_ptr<void>( weak_this_ );
  65. }
  66. shared_ptr<const void> shared_from_this() const
  67. {
  68. init_weak_once();
  69. return shared_ptr<const void>( weak_this_ );
  70. }
  71. // Note: invoked automatically by shared_ptr; do not call
  72. template<class X, class Y> void _internal_accept_owner( shared_ptr<X> * ppx, Y * py ) const
  73. {
  74. BOOST_ASSERT( ppx != 0 );
  75. if( weak_this_.expired() )
  76. {
  77. weak_this_ = *ppx;
  78. }
  79. else if( shared_this_.use_count() != 0 )
  80. {
  81. BOOST_ASSERT( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that
  82. detail::esft2_deleter_wrapper * pd = boost::get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
  83. BOOST_ASSERT( pd != 0 );
  84. pd->set_deleter( *ppx );
  85. ppx->reset( shared_this_, ppx->get() );
  86. shared_this_.reset();
  87. }
  88. }
  89. mutable weak_ptr<void> weak_this_;
  90. private:
  91. mutable shared_ptr<void> shared_this_;
  92. };
  93. template<typename T>
  94. boost::shared_ptr<T> shared_from_raw(T *p)
  95. {
  96. BOOST_ASSERT(p != 0);
  97. return boost::shared_ptr<T>(p->enable_shared_from_raw::shared_from_this(), p);
  98. }
  99. template<typename T>
  100. boost::weak_ptr<T> weak_from_raw(T *p)
  101. {
  102. BOOST_ASSERT(p != 0);
  103. boost::weak_ptr<T> result;
  104. result._internal_aliasing_assign(p->enable_shared_from_raw::weak_this_, p);
  105. return result;
  106. }
  107. namespace detail
  108. {
  109. template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr<X> * ppx, Y const * py, boost::enable_shared_from_raw const * pe )
  110. {
  111. if( pe != 0 )
  112. {
  113. pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
  114. }
  115. }
  116. } // namepsace detail
  117. } // namespace boost
  118. #endif // #ifndef BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED