forked from boostorg/smart_ptr
Refactored and optimized enable_shared_from_this
[SVN r44782]
This commit is contained in:
@@ -373,6 +373,11 @@ public:
|
|||||||
return pi_ != 0? pi_->use_count(): 0;
|
return pi_ != 0? pi_->use_count(): 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool empty() const // nothrow
|
||||||
|
{
|
||||||
|
return pi_ == 0;
|
||||||
|
}
|
||||||
|
|
||||||
friend inline bool operator==(weak_count const & a, weak_count const & b)
|
friend inline bool operator==(weak_count const & a, weak_count const & b)
|
||||||
{
|
{
|
||||||
return a.pi_ == b.pi_;
|
return a.pi_ == b.pi_;
|
||||||
|
@@ -13,7 +13,6 @@
|
|||||||
// http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html
|
// http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html
|
||||||
//
|
//
|
||||||
|
|
||||||
#include <boost/detail/shared_count.hpp>
|
|
||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
#include <boost/assert.hpp>
|
#include <boost/assert.hpp>
|
||||||
#include <boost/config.hpp>
|
#include <boost/config.hpp>
|
||||||
@@ -21,35 +20,19 @@
|
|||||||
namespace boost
|
namespace boost
|
||||||
{
|
{
|
||||||
|
|
||||||
template<class T> class enable_shared_from_this
|
template< class T > class enable_shared_from_this;
|
||||||
|
template< class T, class Y > void sp_accept_owner( shared_ptr<Y> * ptr, enable_shared_from_this<T> const * pe );
|
||||||
|
template< class T, class Y > void sp_accept_owner( shared_ptr<Y> * ptr, enable_shared_from_this<T> const * pe, void * /*pd*/ );
|
||||||
|
|
||||||
|
template< class T > class enable_shared_from_this
|
||||||
{
|
{
|
||||||
void init_internal_shared_once() const
|
|
||||||
{
|
|
||||||
if( !owned() && _internal_shared_count.empty() )
|
|
||||||
{
|
|
||||||
detail::shared_count( (void*)0, detail::sp_deleter_wrapper() ).swap(_internal_shared_count);
|
|
||||||
_internal_weak_count = _internal_shared_count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool owned() const
|
|
||||||
{
|
|
||||||
return _owned;
|
|
||||||
}
|
|
||||||
|
|
||||||
mutable detail::shared_count _internal_shared_count;
|
|
||||||
mutable detail::weak_count _internal_weak_count;
|
|
||||||
mutable bool _owned;
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
enable_shared_from_this():
|
enable_shared_from_this()
|
||||||
_owned(false)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
enable_shared_from_this(enable_shared_from_this const &):
|
enable_shared_from_this(enable_shared_from_this const &)
|
||||||
_owned(false)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -64,69 +47,77 @@ protected:
|
|||||||
// make sure no dangling shared_ptr objects were created by the
|
// make sure no dangling shared_ptr objects were created by the
|
||||||
// user calling shared_from_this() but never passing ownership of the object
|
// user calling shared_from_this() but never passing ownership of the object
|
||||||
// to a shared_ptr.
|
// to a shared_ptr.
|
||||||
BOOST_ASSERT(owned() || _internal_shared_count.use_count() <= 1);
|
BOOST_ASSERT( _shared_count.use_count() <= 1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
shared_ptr<T> shared_from_this()
|
shared_ptr<T> shared_from_this()
|
||||||
{
|
{
|
||||||
init_internal_shared_once();
|
init_weak_once();
|
||||||
T * p = dynamic_cast<T *>(this);
|
T * p = dynamic_cast<T *>( this );
|
||||||
return shared_ptr<T>( detail::shared_count( _internal_weak_count ), p );
|
return shared_ptr<T>( detail::shared_count( _weak_count ), p );
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<T const> shared_from_this() const
|
shared_ptr<T const> shared_from_this() const
|
||||||
{
|
{
|
||||||
init_internal_shared_once();
|
init_weak_once();
|
||||||
T const * p = dynamic_cast<T const *>(this);
|
T const * p = dynamic_cast<T const *>( this );
|
||||||
return shared_ptr<T const>( detail::shared_count( _internal_weak_count ), p );
|
return shared_ptr<T const>( detail::shared_count( _weak_count ), p );
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
mutable detail::weak_count _weak_count;
|
||||||
|
mutable detail::shared_count _shared_count;
|
||||||
|
|
||||||
|
void init_weak_once() const
|
||||||
|
{
|
||||||
|
if( _weak_count.empty() )
|
||||||
|
{
|
||||||
|
detail::shared_count( (void*)0, detail::sp_deleter_wrapper() ).swap( _shared_count );
|
||||||
|
_weak_count = _shared_count;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
void _internal_accept_owner(shared_ptr<U> &owner) const
|
void sp_accept_owner( shared_ptr<U> & owner ) const
|
||||||
{
|
{
|
||||||
if( !_owned )
|
if( _weak_count.use_count() == 0 )
|
||||||
{
|
{
|
||||||
if( _internal_shared_count.empty() )
|
_weak_count = owner.get_shared_count();
|
||||||
|
}else if( !_shared_count.empty() )
|
||||||
{
|
{
|
||||||
_internal_weak_count = owner.get_shared_count();
|
BOOST_ASSERT( owner.unique() ); // no weak_ptrs to owner should exist either, but there's no way to check that
|
||||||
}else
|
|
||||||
{
|
|
||||||
BOOST_ASSERT(owner.unique()); // no weak_ptrs to owner should exist either, but there's no way to check that
|
|
||||||
typedef detail::sp_deleter_wrapper D;
|
typedef detail::sp_deleter_wrapper D;
|
||||||
D * pd = static_cast<D *>(_internal_shared_count.get_deleter(BOOST_SP_TYPEID(D)));
|
D * pd = static_cast<D *>( _shared_count.get_deleter( BOOST_SP_TYPEID(D) ) );
|
||||||
BOOST_ASSERT( pd != 0 );
|
BOOST_ASSERT( pd != 0 );
|
||||||
pd->set_deleter(owner);
|
pd->set_deleter( owner );
|
||||||
|
owner.reset( _shared_count, owner.get() );
|
||||||
|
detail::shared_count().swap( _shared_count );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
shared_ptr<U>( _internal_shared_count, owner.get() ).swap( owner );
|
template< class U, class Y > friend void sp_accept_owner( shared_ptr<Y> * ptr, enable_shared_from_this<U> const * pe );
|
||||||
detail::shared_count().swap(_internal_shared_count);
|
template< class U, class Y > friend void sp_accept_owner( shared_ptr<Y> * ptr, enable_shared_from_this<U> const * pe, void * /*pd*/ );
|
||||||
}
|
|
||||||
_owned = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template< class T, class Y > inline void sp_accept_owner( boost::shared_ptr<Y> * ptr, boost::enable_shared_from_this<T> const * pe )
|
template< class T, class Y > inline void sp_accept_owner( shared_ptr<Y> * ptr, enable_shared_from_this<T> const * pe )
|
||||||
{
|
{
|
||||||
if( pe != 0 )
|
if( pe != 0 )
|
||||||
{
|
{
|
||||||
pe->_internal_accept_owner( *ptr );
|
pe->sp_accept_owner( *ptr );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template< class T, class Y > inline void sp_accept_owner( boost::shared_ptr<Y> * ptr, boost::enable_shared_from_this<T> const * pe, void * /*pd*/ )
|
template< class T, class Y > inline void sp_accept_owner( shared_ptr<Y> * ptr, enable_shared_from_this<T> const * pe, void * /*pd*/ )
|
||||||
{
|
{
|
||||||
if( pe != 0 )
|
if( pe != 0 )
|
||||||
{
|
{
|
||||||
pe->_internal_accept_owner( *ptr );
|
pe->sp_accept_owner( *ptr );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
template< class T, class Y > inline void sp_accept_owner( boost::shared_ptr<Y> * /*ptr*/, boost::enable_shared_from_this<T> const * /*pe*/, boost::detail::sp_deleter_wrapper * /*pd*/ )
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace boost
|
} // namespace boost
|
||||||
|
|
||||||
#endif // #ifndef BOOST_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
|
#endif // #ifndef BOOST_ENABLE_SHARED_FROM_THIS_HPP_INCLUDED
|
||||||
|
Reference in New Issue
Block a user