forked from boostorg/smart_ptr
Reduce enable_shared_from_this overhead
[SVN r44724]
This commit is contained in:
@ -220,6 +220,18 @@ public:
|
|||||||
if( pi_ != 0 ) pi_->add_ref_copy();
|
if( pi_ != 0 ) pi_->add_ref_copy();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined( BOOST_HAS_RVALUE_REFS )
|
||||||
|
|
||||||
|
shared_count(shared_count && r): pi_(r.pi_) // nothrow
|
||||||
|
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
|
||||||
|
, id_(shared_count_id)
|
||||||
|
#endif
|
||||||
|
{
|
||||||
|
r.pi_ = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
|
explicit shared_count(weak_count const & r); // throws bad_weak_ptr when r.use_count() == 0
|
||||||
shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
|
shared_count( weak_count const & r, sp_nothrow_tag ); // constructs an empty *this when r.use_count() == 0
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
// 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/weak_ptr.hpp>
|
#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>
|
||||||
@ -32,7 +32,7 @@ template<class T> class enable_shared_from_this
|
|||||||
T * p = dynamic_cast<T *>(const_cast<enable_shared_from_this*>(this));
|
T * p = dynamic_cast<T *>(const_cast<enable_shared_from_this*>(this));
|
||||||
_internal_shared_this = shared_ptr<T>( p, detail::sp_deleter_wrapper() );
|
_internal_shared_this = shared_ptr<T>( p, detail::sp_deleter_wrapper() );
|
||||||
BOOST_ASSERT(_internal_shared_this.get() == this);
|
BOOST_ASSERT(_internal_shared_this.get() == this);
|
||||||
_internal_weak_this = _internal_shared_this;
|
_internal_weak_count = _internal_shared_this.get_shared_count();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,7 +43,7 @@ template<class T> class enable_shared_from_this
|
|||||||
|
|
||||||
typedef T _internal_element_type; // for bcc 5.5.1
|
typedef T _internal_element_type; // for bcc 5.5.1
|
||||||
mutable shared_ptr<_internal_element_type> _internal_shared_this;
|
mutable shared_ptr<_internal_element_type> _internal_shared_this;
|
||||||
mutable weak_ptr<_internal_element_type> _internal_weak_this;
|
mutable detail::weak_count _internal_weak_count;
|
||||||
mutable bool _owned;
|
mutable bool _owned;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@ -77,17 +77,15 @@ public:
|
|||||||
shared_ptr<T> shared_from_this()
|
shared_ptr<T> shared_from_this()
|
||||||
{
|
{
|
||||||
init_internal_shared_once();
|
init_internal_shared_once();
|
||||||
shared_ptr<T> p(_internal_weak_this);
|
T * p = dynamic_cast<T *>(this);
|
||||||
BOOST_ASSERT(p.get() == this);
|
return shared_ptr<T>( detail::shared_count( _internal_weak_count ), p );
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
shared_ptr<T const> shared_from_this() const
|
shared_ptr<T const> shared_from_this() const
|
||||||
{
|
{
|
||||||
init_internal_shared_once();
|
init_internal_shared_once();
|
||||||
shared_ptr<T const> p(_internal_weak_this);
|
T const * p = dynamic_cast<T const *>(this);
|
||||||
BOOST_ASSERT(p.get() == this);
|
return shared_ptr<T const>( detail::shared_count( _internal_weak_count ), p );
|
||||||
return p;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename U>
|
template<typename U>
|
||||||
@ -97,8 +95,7 @@ public:
|
|||||||
{
|
{
|
||||||
if( !_internal_shared_this )
|
if( !_internal_shared_this )
|
||||||
{
|
{
|
||||||
T * p = dynamic_cast<T *>(const_cast<enable_shared_from_this*>(this));
|
_internal_weak_count = owner.get_shared_count();
|
||||||
_internal_weak_this = shared_ptr<T>(owner, p);
|
|
||||||
}else
|
}else
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(owner.unique()); // no weak_ptrs to owner should exist either, but there's no way to check that
|
BOOST_ASSERT(owner.unique()); // no weak_ptrs to owner should exist either, but there's no way to check that
|
||||||
|
@ -228,6 +228,11 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class Y>
|
||||||
|
shared_ptr(detail::shared_count const & c, Y * p): px(p), pn(c) // never throws
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
// aliasing
|
// aliasing
|
||||||
template< class Y >
|
template< class Y >
|
||||||
shared_ptr( shared_ptr<Y> const & r, T * p ): px( p ), pn( r.pn ) // never throws
|
shared_ptr( shared_ptr<Y> const & r, T * p ): px( p ), pn( r.pn ) // never throws
|
||||||
@ -341,6 +346,11 @@ public:
|
|||||||
r.px = 0;
|
r.px = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<class Y>
|
||||||
|
shared_ptr(detail::shared_count && c, Y * p): px(p), pn( static_cast< detail::shared_count && >( c ) ) // never throws
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
shared_ptr & operator=( shared_ptr && r ) // never throws
|
shared_ptr & operator=( shared_ptr && r ) // never throws
|
||||||
{
|
{
|
||||||
this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
|
this_type( static_cast< shared_ptr && >( r ) ).swap( *this );
|
||||||
@ -467,6 +477,11 @@ public:
|
|||||||
pn.swap(other.pn);
|
pn.swap(other.pn);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
detail::shared_count const & get_shared_count() const // never throws
|
||||||
|
{
|
||||||
|
return pn;
|
||||||
|
}
|
||||||
|
|
||||||
template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
|
template<class Y> bool _internal_less(shared_ptr<Y> const & rhs) const
|
||||||
{
|
{
|
||||||
return pn < rhs.pn;
|
return pn < rhs.pn;
|
||||||
|
Reference in New Issue
Block a user