Chris@16: #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED Chris@16: #define BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED Chris@16: Chris@16: // Chris@16: // enable_shared_from_this.hpp Chris@16: // Chris@16: // Copyright 2002, 2009 Peter Dimov Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt Chris@16: // Chris@16: // http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html Chris@16: // Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: Chris@16: template class enable_shared_from_this Chris@16: { Chris@16: protected: Chris@16: Chris@16: enable_shared_from_this() BOOST_NOEXCEPT Chris@16: { Chris@16: } Chris@16: Chris@16: enable_shared_from_this(enable_shared_from_this const &) BOOST_NOEXCEPT Chris@16: { Chris@16: } Chris@16: Chris@16: enable_shared_from_this & operator=(enable_shared_from_this const &) BOOST_NOEXCEPT Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: ~enable_shared_from_this() BOOST_NOEXCEPT // ~weak_ptr newer throws, so this call also must not throw Chris@16: { Chris@16: } Chris@16: Chris@16: public: Chris@16: Chris@16: shared_ptr shared_from_this() Chris@16: { Chris@16: shared_ptr p( weak_this_ ); Chris@16: BOOST_ASSERT( p.get() == this ); Chris@16: return p; Chris@16: } Chris@16: Chris@16: shared_ptr shared_from_this() const Chris@16: { Chris@16: shared_ptr p( weak_this_ ); Chris@16: BOOST_ASSERT( p.get() == this ); Chris@16: return p; Chris@16: } Chris@16: Chris@101: weak_ptr weak_from_this() BOOST_NOEXCEPT Chris@101: { Chris@101: return weak_this_; Chris@101: } Chris@101: Chris@101: weak_ptr weak_from_this() const BOOST_NOEXCEPT Chris@101: { Chris@101: return weak_this_; Chris@101: } Chris@101: Chris@16: public: // actually private, but avoids compiler template friendship issues Chris@16: Chris@16: // Note: invoked automatically by shared_ptr; do not call Chris@16: template void _internal_accept_owner( shared_ptr const * ppx, Y * py ) const Chris@16: { Chris@16: if( weak_this_.expired() ) Chris@16: { Chris@16: weak_this_ = shared_ptr( *ppx, py ); Chris@16: } Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@16: mutable weak_ptr weak_this_; Chris@16: }; Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // #ifndef BOOST_SMART_PTR_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED