1 #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
2 #define BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
5 // enable_shared_from_this.hpp
7 // Copyright 2002, 2009 Peter Dimov
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
13 // http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html
16 #include <boost/smart_ptr/weak_ptr.hpp>
17 #include <boost/smart_ptr/shared_ptr.hpp>
18 #include <boost/assert.hpp>
19 #include <boost/config.hpp>
24 template<class T> class enable_shared_from_this
28 enable_shared_from_this() BOOST_NOEXCEPT
32 enable_shared_from_this(enable_shared_from_this const &) BOOST_NOEXCEPT
36 enable_shared_from_this & operator=(enable_shared_from_this const &) BOOST_NOEXCEPT
41 ~enable_shared_from_this() BOOST_NOEXCEPT // ~weak_ptr<T> newer throws, so this call also must not throw
47 shared_ptr<T> shared_from_this()
49 shared_ptr<T> p( weak_this_ );
50 BOOST_ASSERT( p.get() == this );
54 shared_ptr<T const> shared_from_this() const
56 shared_ptr<T const> p( weak_this_ );
57 BOOST_ASSERT( p.get() == this );
61 weak_ptr<T> weak_from_this() BOOST_NOEXCEPT
66 weak_ptr<T const> weak_from_this() const BOOST_NOEXCEPT
71 public: // actually private, but avoids compiler template friendship issues
73 // Note: invoked automatically by shared_ptr; do not call
74 template<class X, class Y> void _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const
76 if( weak_this_.expired() )
78 weak_this_ = shared_ptr<T>( *ppx, py );
84 mutable weak_ptr<T> weak_this_;
89 #endif // #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED