1 #ifndef BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED
\r
2 #define BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED
\r
5 // enable_shared_from_this2.hpp
\r
7 // Copyright 2002, 2009 Peter Dimov
\r
8 // Copyright 2008 Frank Mori Hess
\r
10 // Distributed under the Boost Software License, Version 1.0.
\r
11 // See accompanying file LICENSE_1_0.txt or copy at
\r
12 // http://www.boost.org/LICENSE_1_0.txt
\r
15 #include <boost/config.hpp>
\r
16 #include <boost/shared_ptr.hpp>
\r
17 #include <boost/assert.hpp>
\r
18 #include <boost/detail/workaround.hpp>
\r
26 class esft2_deleter_wrapper
\r
30 shared_ptr<void> deleter_;
\r
34 esft2_deleter_wrapper()
\r
38 template< class T > void set_deleter( shared_ptr<T> const & deleter )
\r
43 template< class T> void operator()( T* )
\r
45 BOOST_ASSERT( deleter_.use_count() <= 1 );
\r
50 } // namespace detail
\r
52 template< class T > class enable_shared_from_this2
\r
56 enable_shared_from_this2()
\r
60 enable_shared_from_this2( enable_shared_from_this2 const & )
\r
64 enable_shared_from_this2 & operator=( enable_shared_from_this2 const & )
\r
69 ~enable_shared_from_this2()
\r
71 BOOST_ASSERT( shared_this_.use_count() <= 1 ); // make sure no dangling shared_ptr objects exist
\r
76 mutable weak_ptr<T> weak_this_;
\r
77 mutable shared_ptr<T> shared_this_;
\r
81 shared_ptr<T> shared_from_this()
\r
84 return shared_ptr<T>( weak_this_ );
\r
87 shared_ptr<T const> shared_from_this() const
\r
90 return shared_ptr<T>( weak_this_ );
\r
95 void init_weak_once() const
\r
97 if( weak_this_._empty() )
\r
99 shared_this_.reset( static_cast< T* >( 0 ), detail::esft2_deleter_wrapper() );
\r
100 weak_this_ = shared_this_;
\r
104 public: // actually private, but avoids compiler template friendship issues
\r
106 // Note: invoked automatically by shared_ptr; do not call
\r
107 template<class X, class Y> void _internal_accept_owner( shared_ptr<X> * ppx, Y * py ) const
\r
109 BOOST_ASSERT( ppx != 0 );
\r
111 if( weak_this_.use_count() == 0 )
\r
113 weak_this_ = shared_ptr<T>( *ppx, py );
\r
115 else if( shared_this_.use_count() != 0 )
\r
117 BOOST_ASSERT( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that
\r
119 detail::esft2_deleter_wrapper * pd = boost::get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
\r
120 BOOST_ASSERT( pd != 0 );
\r
122 pd->set_deleter( *ppx );
\r
124 ppx->reset( shared_this_, ppx->get() );
\r
125 shared_this_.reset();
\r
130 } // namespace boost
\r
132 #endif // #ifndef BOOST_ENABLE_SHARED_FROM_THIS2_HPP_INCLUDED
\r