shared_ptr由成员模板参数类型的指针和shared_count对象组成,shared_count表示为引用计数对象,主要是通过shared_count对象的拷贝构造函数或者赋值运行符来实现引用 计数的增加,shared_count对象的析构函数将引用计数减1,如果发现引用计数为0,就回收对象。
shared_ptr的reset()方法是通过与临时对象交换,但是当临时对象析构时,对象的引用 计数减1
shared_ptr的类结构
其类定义为
template class enable_shared_from_this
{
protected:
BOOST_CONSTEXPR enable_shared_from_this() BOOST_SP_NOEXCEPT
{
}
BOOST_CONSTEXPR enable_shared_from_this(enable_shared_from_this const &) BOOST_SP_NOEXCEPT
{
}
enable_shared_from_this & operator=(enable_shared_from_this const &) BOOST_SP_NOEXCEPT
{
return *this;
}
~enable_shared_from_this() BOOST_SP_NOEXCEPT // ~weak_ptr newer throws, so this call also must not throw
{
}
public:
shared_ptr shared_from_this()
{
shared_ptr p( weak_this_ );
BOOST_ASSERT( p.get() == this );
return p;
}
shared_ptr shared_from_this() const
{
shared_ptr p( weak_this_ );
BOOST_ASSERT( p.get() == this );
return p;
}
weak_ptr weak_from_this() BOOST_SP_NOEXCEPT
{
return weak_this_;
}
weak_ptr weak_from_this() const BOOST_SP_NOEXCEPT
{
return weak_this_;
}
public: // actually private, but avoids compiler template friendship issues
// Note: invoked automatically by shared_ptr; do not call
template void _internal_accept_owner( shared_ptr const * ppx, Y * py ) const BOOST_SP_NOEXCEPT
{
if( weak_this_.expired() )
{
weak_this_ = shared_ptr( *ppx, py );
}
}
private:
mutable weak_ptr weak_this_;
}
其包含类型为weak_ptr的成员weak_this_。
在share_ptr构造函数中,如果指针对应类型是enable_shared_from_this,会调用_internal_accept_owner来初始化weak_this_。在构造函数初始化列表中,调用 shared_counte的默认构造函数,此时 pi=0。在构造函数中,调用sp_pointer_construct,会创建临时shared_count对象与pn作交换,临时对象的use_count_和weak_count_为1,交换后,pn的两个计数都为1.
template
explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
{
boost::detail::sp_pointer_construct( this, p, pn );
}
template< class T, class Y > inline void sp_pointer_construct( boost::shared_ptr< T > * ppx, Y * p, boost::detail::shared_count & pn )
{
boost::detail::shared_count( p ).swap( pn );
boost::detail::sp_enable_shared_from_this( ppx, p, p );
}
template< class X, class Y, class T > inline void sp_enable_shared_from_this( boost::shared_ptr const * ppx, Y const * py, boost::enable_shared_from_this< T > const * pe )
{
if( pe != 0 )
{
pe->_internal_accept_owner( ppx, const_cast< Y* >( py ) );
}
}
在_internal_accept_owner函数中,因为weak_this_对象的use_count_为0,其expired()方法返回的为true,先调用构造函数将p赋值给指针,引用计数使用r的pn。在调用pn的拷贝构造函数时,pn的use_count_会加1,又因为是创建的临时对象,use_count会减1,又恢复为1。
在赋值给weak_this_时,weak_count_会加1,变为2
template< class Y >
shared_ptr( shared_ptr const & r, element_type * p ) BOOST_SP_NOEXCEPT : px( p ), pn( r.pn )
{
}
shared_count(shared_count const & r) BOOST_SP_NOEXCEPT: pi_(r.pi_)
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
, id_(shared_count_id)
#endif
{
if( pi_ != 0 ) pi_->add_ref_copy();
}
template
weak_ptr & operator=( shared_ptr const & r ) BOOST_SP_NOEXCEPT
{
boost::detail::sp_assert_convertible< Y, T >();
px = r.px;
pn = r.pn;
return *this;
}
weak_count & operator= (shared_count const & r) BOOST_SP_NOEXCEPT
{
sp_counted_base * tmp = r.pi_;
if( tmp != pi_ )
{
if(tmp != 0) tmp->weak_add_ref();
if(pi_ != 0) pi_->weak_release();
pi_ = tmp;
}
return *this;
}
shared_from_this方法通过weak_this_来创建shared_ptr。此时会将use_count_l加1
shared_ptr shared_from_this()
{
shared_ptr p( weak_this_ );
BOOST_ASSERT( p.get() == this );
return p;
}
template
explicit shared_ptr( weak_ptr const & r ): pn( r.pn ) // may throw
{
boost::detail::sp_assert_convertible< Y, T >();
// it is now safe to copy r.px, as pn(r.pn) did not throw
px = r.px;
}
inline shared_count::shared_count( weak_count const & r ): pi_( r.pi_ )
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
, id_(shared_count_id)
#endif
{
if( pi_ == 0 || !pi_->add_ref_lock() )
{
boost::throw_exception( boost::bad_weak_ptr() );
}
}
weak_from_this方法直接返回weak_this_。
weak_ptr的类结构图