简介
boost中的智能指针有
- shared_ptr
- weak_ptr
- scoped_ptr
- intrusive_ptr
shared_ptr
结构为
sp_counted_base
的dispose,get_deleter和get_untyped_deleter为纯虚函数,其默认构造函数将use_count_和weak_count_初始化为1
sp_counted_base(): use_count_( 1 ), weak_count_( 1 )
{
}
shared_count(shared_count const & r)
构造函数会创建sp_counted_base 的子类,其拷贝构造函数会将引用计数+1
shared_count(shared_count const & r): pi_(r.pi_) // nothrow
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
, id_(shared_count_id)
#endif
{
if( pi_ != 0 ) pi_->add_ref_copy();
}
class sp_counted_base
{
void add_ref_copy()
{
atomic_increment( &use_count_ );
}
}
shared_count(weak_count const & r)
构造函数作sp_counted_base的赋值,同时会增加引用计数。如果指针为空或者引用计数为0会抛出异常
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() );
}
}
sp_counted_impl_p,内部存储对象的指针
shared_ptr( Y * p )
会调用sp_pointer_construct,对于实现了enable_shared_from_this的类,会调用_internal_accept_owner
//shared_ptr类
template<class Y>
explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
{
boost::detail::sp_pointer_construct( this, p, pn );
}
//sp_pointer_construct函数模板
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 );
}
//sp_enable_shared_from_this函数模板
template< class X, class Y, class T > inline void sp_enable_shared_from_this( boost::shared_ptr<X> 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 ) );
}
}
//enable_shared_from_this类模板
template<class X, class Y> void _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const
{
if( weak_this_.expired() )
{
weak_this_ = shared_ptr<T>( *ppx, py );
}
}
enable_shared_from_this:类在调用_internal_accept_owner时,会使用下面的shared_tp复制构造函数
template< class Y >
shared_ptr( shared_ptr<Y> const & r, element_type * p ) BOOST_NOEXCEPT : px( p ), pn( r.pn )
{
}
shared_ptr( weak_ptr<Y> const & r )
将指针赋值给shared_ptr,同时将weak_count赋值给shared_count
template<class Y>
explicit shared_ptr( weak_ptr<Y> 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;
}
weak_ptr
结构为
构造函数weak_count(shared_count const & r)
会增加sp_counted_base 的弱引用计数
weak_count(shared_count const & r): pi_(r.pi_) // nothrow
#if defined(BOOST_SP_ENABLE_DEBUG_HOOKS)
, id_(weak_count_id)
#endif
{
if(pi_ != 0) pi_->weak_add_ref();
}
void weak_add_ref() // nothrow
{
atomic_increment( &weak_count_ );
}
赋值构造函数weak_count & operator= (shared_count const & r)
,会将当前的sp_counted_base销毁,同时增加来源的弱引用计数
weak_count & operator= (shared_count const & r) // nothrow
{
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;
}
intrusive_ptr
需要对象自己实现友元函数
void intrusive_ptr_add_ref(T * p);
void intrusive_ptr_release(T * p);
类结构为