Chris@16: #ifndef BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED Chris@16: #define BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED Chris@16: Chris@16: // Chris@16: // enable_shared_from_raw.hpp Chris@16: // Chris@101: // Copyright 2002, 2009, 2014 Peter Dimov Chris@16: // Copyright 2008-2009 Frank Mori Hess 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: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost Chris@16: { Chris@16: template boost::shared_ptr shared_from_raw(T *); Chris@16: template boost::weak_ptr weak_from_raw(T *); Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr * ppx, Y const * py, boost::enable_shared_from_raw const * pe ); Chris@16: Chris@16: } // namespace detail Chris@16: Chris@16: class enable_shared_from_raw Chris@16: { Chris@16: protected: Chris@16: Chris@16: enable_shared_from_raw() Chris@16: { Chris@16: } Chris@16: Chris@16: enable_shared_from_raw( enable_shared_from_raw const & ) Chris@16: { Chris@16: } Chris@16: Chris@16: enable_shared_from_raw & operator=( enable_shared_from_raw const & ) Chris@16: { Chris@16: return *this; Chris@16: } Chris@16: Chris@16: ~enable_shared_from_raw() Chris@16: { Chris@16: BOOST_ASSERT( shared_this_.use_count() <= 1 ); // make sure no dangling shared_ptr objects exist Chris@16: } Chris@16: Chris@16: private: Chris@16: Chris@101: void init_if_expired() const Chris@16: { Chris@16: if( weak_this_.expired() ) Chris@16: { Chris@16: shared_this_.reset( static_cast(0), detail::esft2_deleter_wrapper() ); Chris@16: weak_this_ = shared_this_; Chris@16: } Chris@16: } Chris@16: Chris@101: void init_if_empty() const Chris@101: { Chris@101: if( weak_this_._empty() ) Chris@101: { Chris@101: shared_this_.reset( static_cast(0), detail::esft2_deleter_wrapper() ); Chris@101: weak_this_ = shared_this_; Chris@101: } Chris@101: } Chris@101: Chris@16: #ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS Chris@16: public: Chris@16: #else Chris@16: private: Chris@16: template friend class shared_ptr; Chris@16: template friend boost::shared_ptr shared_from_raw(T *); Chris@16: template friend boost::weak_ptr weak_from_raw(T *); Chris@16: template< class X, class Y > friend inline void detail::sp_enable_shared_from_this( boost::shared_ptr * ppx, Y const * py, boost::enable_shared_from_raw const * pe ); Chris@16: #endif Chris@16: Chris@101: shared_ptr shared_from_this() const Chris@16: { Chris@101: init_if_expired(); Chris@101: return shared_ptr( weak_this_ ); Chris@16: } Chris@16: Chris@101: shared_ptr shared_from_this() const volatile Chris@16: { Chris@101: return const_cast< enable_shared_from_raw const * >( this )->shared_from_this(); Chris@101: } Chris@101: Chris@101: weak_ptr weak_from_this() const Chris@101: { Chris@101: init_if_empty(); Chris@101: return weak_this_; Chris@101: } Chris@101: Chris@101: weak_ptr weak_from_this() const volatile Chris@101: { Chris@101: return const_cast< enable_shared_from_raw const * >( this )->weak_from_this(); Chris@16: } Chris@16: Chris@16: // Note: invoked automatically by shared_ptr; do not call Chris@16: template void _internal_accept_owner( shared_ptr * ppx, Y * py ) const Chris@16: { Chris@16: BOOST_ASSERT( ppx != 0 ); Chris@16: Chris@16: if( weak_this_.expired() ) Chris@16: { Chris@16: weak_this_ = *ppx; Chris@16: } Chris@16: else if( shared_this_.use_count() != 0 ) Chris@16: { Chris@16: BOOST_ASSERT( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that Chris@16: Chris@16: detail::esft2_deleter_wrapper * pd = boost::get_deleter( shared_this_ ); Chris@16: BOOST_ASSERT( pd != 0 ); Chris@16: Chris@16: pd->set_deleter( *ppx ); Chris@16: Chris@16: ppx->reset( shared_this_, ppx->get() ); Chris@16: shared_this_.reset(); Chris@16: } Chris@16: } Chris@16: Chris@101: mutable weak_ptr weak_this_; Chris@101: Chris@16: private: Chris@101: Chris@101: mutable shared_ptr shared_this_; Chris@16: }; Chris@16: Chris@16: template Chris@16: boost::shared_ptr shared_from_raw(T *p) Chris@16: { Chris@16: BOOST_ASSERT(p != 0); Chris@16: return boost::shared_ptr(p->enable_shared_from_raw::shared_from_this(), p); Chris@16: } Chris@16: Chris@16: template Chris@16: boost::weak_ptr weak_from_raw(T *p) Chris@16: { Chris@16: BOOST_ASSERT(p != 0); Chris@16: boost::weak_ptr result; Chris@101: result._internal_aliasing_assign(p->enable_shared_from_raw::weak_from_this(), p); Chris@16: return result; Chris@16: } Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: template< class X, class Y > inline void sp_enable_shared_from_this( boost::shared_ptr * ppx, Y const * py, boost::enable_shared_from_raw const * pe ) Chris@16: { Chris@16: if( pe != 0 ) Chris@16: { Chris@16: pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) ); Chris@16: } Chris@16: } Chris@16: } // namepsace detail Chris@16: Chris@16: } // namespace boost Chris@16: Chris@16: #endif // #ifndef BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED