mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-08-02 22:24:26 +02:00
Reduce enable_shared_from_this overhead (replace _internal_shared_ptr by _internal_shared_count)
[SVN r44730]
This commit is contained in:
@@ -27,12 +27,11 @@ template<class T> class enable_shared_from_this
|
|||||||
// to use lazy initialization
|
// to use lazy initialization
|
||||||
void init_internal_shared_once() const
|
void init_internal_shared_once() const
|
||||||
{
|
{
|
||||||
if( !owned() && _internal_shared_this.get() == 0 )
|
if( !owned() && _internal_shared_count.empty() )
|
||||||
{
|
{
|
||||||
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() );
|
detail::shared_count( p, detail::sp_deleter_wrapper() ).swap(_internal_shared_count);
|
||||||
BOOST_ASSERT(_internal_shared_this.get() == this);
|
_internal_weak_count = _internal_shared_count;
|
||||||
_internal_weak_count = _internal_shared_this.get_shared_count();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -41,8 +40,7 @@ template<class T> class enable_shared_from_this
|
|||||||
return _owned;
|
return _owned;
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef T _internal_element_type; // for bcc 5.5.1
|
mutable detail::shared_count _internal_shared_count;
|
||||||
mutable shared_ptr<_internal_element_type> _internal_shared_this;
|
|
||||||
mutable detail::weak_count _internal_weak_count;
|
mutable detail::weak_count _internal_weak_count;
|
||||||
mutable bool _owned;
|
mutable bool _owned;
|
||||||
|
|
||||||
@@ -69,7 +67,7 @@ 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_this.use_count() <= 1);
|
BOOST_ASSERT(owned() || _internal_shared_count.use_count() <= 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
@@ -93,18 +91,19 @@ public:
|
|||||||
{
|
{
|
||||||
if( !_owned )
|
if( !_owned )
|
||||||
{
|
{
|
||||||
if( !_internal_shared_this )
|
if( _internal_shared_count.empty() )
|
||||||
{
|
{
|
||||||
_internal_weak_count = owner.get_shared_count();
|
_internal_weak_count = owner.get_shared_count();
|
||||||
}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
|
||||||
detail::sp_deleter_wrapper * pd = get_deleter<detail::sp_deleter_wrapper>(_internal_shared_this);
|
typedef detail::sp_deleter_wrapper D;
|
||||||
|
D * pd = static_cast<D *>(_internal_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( _internal_shared_this, owner.get() );
|
shared_ptr<U>( _internal_shared_count, owner.get() ).swap( owner );
|
||||||
_internal_shared_this.reset();
|
detail::shared_count().swap(_internal_shared_count);
|
||||||
}
|
}
|
||||||
_owned = true;
|
_owned = true;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user