enable_shared_from_raw.hpp 4.3 KB

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