enable_shared_from_raw.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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, 2014 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/config/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_if_expired() 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. void init_if_empty() const
  53. {
  54. if( weak_this_._empty() )
  55. {
  56. shared_this_.reset( static_cast<void*>(0), detail::esft2_deleter_wrapper() );
  57. weak_this_ = shared_this_;
  58. }
  59. }
  60. #ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
  61. public:
  62. #else
  63. private:
  64. template<class Y> friend class shared_ptr;
  65. template<typename T> friend boost::shared_ptr<T> shared_from_raw(T *);
  66. template<typename T> friend boost::weak_ptr<T> weak_from_raw(T *);
  67. 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 );
  68. #endif
  69. shared_ptr<void const volatile> shared_from_this() const
  70. {
  71. init_if_expired();
  72. return shared_ptr<void const volatile>( weak_this_ );
  73. }
  74. shared_ptr<void const volatile> shared_from_this() const volatile
  75. {
  76. return const_cast< enable_shared_from_raw const * >( this )->shared_from_this();
  77. }
  78. weak_ptr<void const volatile> weak_from_this() const
  79. {
  80. init_if_empty();
  81. return weak_this_;
  82. }
  83. weak_ptr<void const volatile> weak_from_this() const volatile
  84. {
  85. return const_cast< enable_shared_from_raw const * >( this )->weak_from_this();
  86. }
  87. // Note: invoked automatically by shared_ptr; do not call
  88. template<class X, class Y> void _internal_accept_owner( shared_ptr<X> * ppx, Y * ) const
  89. {
  90. BOOST_ASSERT( ppx != 0 );
  91. if( weak_this_.expired() )
  92. {
  93. weak_this_ = *ppx;
  94. }
  95. else if( shared_this_.use_count() != 0 )
  96. {
  97. BOOST_ASSERT( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that
  98. detail::esft2_deleter_wrapper * pd = boost::get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
  99. BOOST_ASSERT( pd != 0 );
  100. pd->set_deleter( *ppx );
  101. ppx->reset( shared_this_, ppx->get() );
  102. shared_this_.reset();
  103. }
  104. }
  105. mutable weak_ptr<void const volatile> weak_this_;
  106. private:
  107. mutable shared_ptr<void const volatile> shared_this_;
  108. };
  109. template<typename T>
  110. boost::shared_ptr<T> shared_from_raw(T *p)
  111. {
  112. BOOST_ASSERT(p != 0);
  113. return boost::shared_ptr<T>(p->enable_shared_from_raw::shared_from_this(), p);
  114. }
  115. template<typename T>
  116. boost::weak_ptr<T> weak_from_raw(T *p)
  117. {
  118. BOOST_ASSERT(p != 0);
  119. boost::weak_ptr<T> result(p->enable_shared_from_raw::weak_from_this(), p);
  120. return result;
  121. }
  122. namespace detail
  123. {
  124. 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 )
  125. {
  126. if( pe != 0 )
  127. {
  128. pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
  129. }
  130. }
  131. } // namepsace detail
  132. } // namespace boost
  133. #endif // #ifndef BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED