1 #ifndef BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED
2 #define BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED
5 // enable_shared_from_raw.hpp
7 // Copyright 2002, 2009, 2014 Peter Dimov
8 // Copyright 2008-2009 Frank Mori Hess
10 // Distributed under the Boost Software License, Version 1.0.
11 // See accompanying file LICENSE_1_0.txt or copy at
12 // http://www.boost.org/LICENSE_1_0.txt
15 #include <boost/config.hpp>
16 #include <boost/shared_ptr.hpp>
17 #include <boost/weak_ptr.hpp>
18 #include <boost/assert.hpp>
19 #include <boost/detail/workaround.hpp>
23 template<typename T> boost::shared_ptr<T> shared_from_raw(T *);
24 template<typename T> boost::weak_ptr<T> weak_from_raw(T *);
28 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 );
32 class enable_shared_from_raw
36 enable_shared_from_raw()
40 enable_shared_from_raw( enable_shared_from_raw const & )
44 enable_shared_from_raw & operator=( enable_shared_from_raw const & )
49 ~enable_shared_from_raw()
51 BOOST_ASSERT( shared_this_.use_count() <= 1 ); // make sure no dangling shared_ptr objects exist
56 void init_if_expired() const
58 if( weak_this_.expired() )
60 shared_this_.reset( static_cast<void*>(0), detail::esft2_deleter_wrapper() );
61 weak_this_ = shared_this_;
65 void init_if_empty() const
67 if( weak_this_._empty() )
69 shared_this_.reset( static_cast<void*>(0), detail::esft2_deleter_wrapper() );
70 weak_this_ = shared_this_;
74 #ifdef BOOST_NO_MEMBER_TEMPLATE_FRIENDS
78 template<class Y> friend class shared_ptr;
79 template<typename T> friend boost::shared_ptr<T> shared_from_raw(T *);
80 template<typename T> friend boost::weak_ptr<T> weak_from_raw(T *);
81 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 );
84 shared_ptr<void const volatile> shared_from_this() const
87 return shared_ptr<void const volatile>( weak_this_ );
90 shared_ptr<void const volatile> shared_from_this() const volatile
92 return const_cast< enable_shared_from_raw const * >( this )->shared_from_this();
95 weak_ptr<void const volatile> weak_from_this() const
101 weak_ptr<void const volatile> weak_from_this() const volatile
103 return const_cast< enable_shared_from_raw const * >( this )->weak_from_this();
106 // Note: invoked automatically by shared_ptr; do not call
107 template<class X, class Y> void _internal_accept_owner( shared_ptr<X> * ppx, Y * ) const
109 BOOST_ASSERT( ppx != 0 );
111 if( weak_this_.expired() )
115 else if( shared_this_.use_count() != 0 )
117 BOOST_ASSERT( ppx->unique() ); // no weak_ptrs should exist either, but there's no way to check that
119 detail::esft2_deleter_wrapper * pd = boost::get_deleter<detail::esft2_deleter_wrapper>( shared_this_ );
120 BOOST_ASSERT( pd != 0 );
122 pd->set_deleter( *ppx );
124 ppx->reset( shared_this_, ppx->get() );
125 shared_this_.reset();
129 mutable weak_ptr<void const volatile> weak_this_;
133 mutable shared_ptr<void const volatile> shared_this_;
137 boost::shared_ptr<T> shared_from_raw(T *p)
139 BOOST_ASSERT(p != 0);
140 return boost::shared_ptr<T>(p->enable_shared_from_raw::shared_from_this(), p);
144 boost::weak_ptr<T> weak_from_raw(T *p)
146 BOOST_ASSERT(p != 0);
147 boost::weak_ptr<T> result;
148 result._internal_aliasing_assign(p->enable_shared_from_raw::weak_from_this(), p);
154 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 )
158 pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
161 } // namepsace detail
165 #endif // #ifndef BOOST_ENABLE_SHARED_FROM_RAW_HPP_INCLUDED