上次剖析了shared_ptr类的源码,本来肯定也是要说shared_count的,不过由于篇幅,shared_count在这篇博客分析。
shared_ptr类自身有两个成员,一个就是T类型指针,另一个就是shared_count对象了。shared_ptr把所有的计数任务都交给了该成员,最终指针的销毁也是由该对象去执行的(底层实际还有sp_counted_base)。这是一种解耦的思想。
源码分析:
class weak_count;
class shared_count
{
private:
sp_counted_base * pi_;
friend class weak_count;
public:
shared_count(): pi_(0) // nothrow
{
}
template<class Y> explicit shared_count( Y * p ): pi_( 0 )
{
pi_ = new sp_counted_impl_p<Y>( p ); //new一个impl派生类
if( pi_ == 0 ) //如果失败,就摧毁,这是在定义了BOOST_NO_EXCEPTION情况下,new失败返回值为0。宏定义被我略去。
{
boost::checked_delete( p );
boost::throw_exception( std::bad_alloc() );
}
}
//删除器版本
template<class P, class D> shared_count( P p, D d ): pi_(0)
{
pi_ = new sp_counted_impl_pd<P, D>(p, d);
if(pi_ == 0)
{
d(p); // delete p
boost::throw_exception(std::bad_alloc());
}
}
//分配器版本
template<class P, class D, class A> shared_count( P p, D d, A a ): pi_( 0 )
{
typedef sp_counted_impl_pda<P, D, A> impl_type;
typedef typename A::template rebind< impl_type >::other A2;
A2 a2( a );
pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
if( pi_ != 0 )
{
new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
}
else
{
d( p ); //失败要执行d(),销毁操作。
boost::throw_exception( std::bad_alloc() );
}
}
#ifndef BOOST_NO_AUTO_PTR
// auto_ptr<Y> is special cased to provide the strong guarantee
//auto_ptr版本,不过C++11已经弃用auto_ptr了
template<class Y>
explicit shared_count( std::auto_ptr<Y> & r ): pi_( new sp_counted_impl_p<Y>( r.get() ) )
{
r.release(); //在这里release的,呵呵
}
#endif
#if !defined( BOOST_NO_CXX11_SMART_PTR )
//C++11的unique_ptr版本。
template<class Y, class D>
explicit shared_count( std::unique_ptr<Y, D> & r ): pi_( 0 )
{
typedef typename sp_convert_reference<D>::type D2;
D2 d2( r.get_deleter() );
pi_ = new sp_counted_impl_pd< typename std::unique_ptr<Y, D>::pointer, D2 >( r.get(), d2 );
r.release();
}
#endif
~shared_count() // nothrow
{