智能指针(shared_ptr、weak_ptr)的基类—_Ptr_base(STL源码)

// 智能指针模板基类 _Ptr_base
template<class _Ty>
class _Ptr_base
{	// shared_ptr 和 weak_ptr 的基类
public:
	typedef _Ptr_base<_Ty> _Myt;	// 定义模板基类 类型
	typedef _Ty element_type;		// 定义元素类型

	_Ptr_base()
		: _Ptr(0), _Rep(0)
	{	// construct
	}
	// 从_Right中获取资源,构造_Ptr_base
	_Ptr_base(_Myt&& _Right)
		: _Ptr(0), _Rep(0)
	{	// construct _Ptr_base object that takes resource from _Right
		_Assign_rv(_STD forward<_Myt>(_Right));// 右值转发
	}
	// 从其他的_Ptr_base模板类中构造
	template<class _Ty2>
	_Ptr_base(_Ptr_base<_Ty2>&& _Right)
		: _Ptr(_Right._Ptr), _Rep(_Right._Rep)// friend class _Ptr_base;
	{	// construct _Ptr_base object that takes resource from _Right
		_Right._Ptr = 0;
		_Right._Rep = 0;
	}
	// 赋值构造,返回赋左值的引用
	_Myt& operator=(_Myt&& _Right)
	{	// construct _Ptr_base object that takes resource from _Right
		_Assign_rv(_STD forward<_Myt>(_Right));
		return (*this);
	}
	// 通过移动_Right资源进行分配
	void _Assign_rv(_Myt&& _Right)
	{	// assign by moving _Right
		if (this != &_Right)	// 非自身引用
			_Swap(_Right);
	}
	// Get _Uses值
	long use_count() const _NOEXCEPT
	{	// return use count
		return (_Rep ? _Rep->_Use_count() : 0);
	}
		void _Swap(_Ptr_base& _Right)
	{	// swap pointers
		_STD swap(_Rep, _Right._Rep);
		_STD swap(_Ptr, _Right._Ptr);
	}
	// 比较引用计数管理器对象的地址
	template<class _Ty2>
	bool owner_before(const _Ptr_base<_Ty2>& _Right) const
	{	// compare addresses of manager objects
		return (_Rep < _Right._Rep);
	}
	// return 0;
	void *_Get_deleter(const _XSTD2 type_info& _Typeid) const
	{	// return pointer to deleter object if its type is _Typeid
		return (_Rep ? _Rep->_Get_deleter(_Typeid) : 0);
	}
	// Get _Ptr
	_Ty *_Get() const
	{	// return pointer to resource
		return (_Ptr);
	}
	// 无效判断
	bool _Expired() const
	{	// test if expired
		return (!_Rep || _Rep->_Expired());
	}

//
// 赋值操作,引用计数(_Uses)操作
	// 减少引用计数_Uses
	void _Decref()
	{	// decrement reference count
		if (_Rep != 0)
			_Rep->_Decref();
	}

	// 释放资源
	void _Reset()
	{	// release resource
		_Reset(0, 0); // 调用void _Reset(_Ty *_Other_ptr, _Ref_count_base *_Other_rep)
	}
//从其他对象获取资源所有权///
	// 释放资源并取得_Other(shared_ptr)的所有权
	template<class _Ty2>
	void _Reset(const _Ptr_base<_Ty2>& _Other)
	{	// release resource and take ownership of _Other._Ptr
		_Reset(_Other._Ptr, _Other._Rep);
	}
	// 释放资源并取得_Other(weak_ptr)的所有权
	template<class _Ty2>
	void _Reset(const _Ptr_base<_Ty2>& _Other, bool _Throw)
	{	// release resource and take ownership from weak_ptr _Other._Ptr
		_Reset(_Other._Ptr, _Other._Rep, _Throw);
	}
	// 参数三标记内部转换类型
	template<class _Ty2>
	void _Reset(const _Ptr_base<_Ty2>& _Other, const _Static_tag&)
	{	// release resource and take ownership of _Other._Ptr
		_Reset(static_cast<_Ty *>(_Other._Ptr), _Other._Rep);
	}

	template<class _Ty2>
	void _Reset(const _Ptr_base<_Ty2>& _Other, const _Const_tag&)
	{	// release resource and take ownership of _Other._Ptr
		_Reset(const_cast<_Ty *>(_Other._Ptr), _Other._Rep);
	}

	template<class _Ty2>
	void _Reset(const _Ptr_base<_Ty2>& _Other, const _Dynamic_tag&)
	{	// release resource and take ownership of _Other._Ptr
		_Ty *_Ptr = dynamic_cast<_Ty *>(_Other._Ptr);
		if (_Ptr)
			_Reset(_Ptr, _Other._Rep);
		else
			_Reset();
	}
	
	template<class _Ty2>
	void _Reset(auto_ptr<_Ty2>&& _Other)
	{	// release resource and take _Other.get()
		_Ty2 *_Px = _Other.get();	// auto_ptr的原始指针
		_Reset0(_Px, new _Ref_count<_Ty>(_Px));
		_Other.release();// 释放占有的资源
		_Enable_shared(_Px, _Rep); // std::enable_shared_from_this 能让一个对象(假设其名为t,且已被一个 std::shared_ptr 对象 pt 管理)安全地生成其他额外的 std::shared_ptr 实例(假设名为 pt1, pt2, ... ) ,它们与 pt 共享对象 t 的所有权。
	}

	template<class _Ty2>
	void _Reset(_Ty *_Ptr, const _Ptr_base<_Ty2>& _Other)
	{	// release resource and alias _Ptr with _Other_rep
		_Reset(_Ptr, _Other._Rep);
	}
//从原始指针中获取资源所有权///
	// 底层_Reset(上述_Reset统一调用,递增、减引用计数)
	void _Reset(_Ty *_Other_ptr, _Ref_count_base *_Other_rep)
	{	// release resource and take _Other_ptr through _Other_rep
		if (_Other_rep)
			_Other_rep->_Incref();			// 递增右边(如赋值操作=)的智能指针的引用计数
		_Reset0(_Other_ptr, _Other_rep);	// 调用void _Reset0(_Ty *_Other_ptr, _Ref_count_base *_Other_rep)
	}

	void _Reset(_Ty *_Other_ptr, _Ref_count_base *_Other_rep, bool _Throw)
	{	// take _Other_ptr through _Other_rep from weak_ptr if not expired
		// otherwise, leave in default state if !_Throw,
		// otherwise throw exception
		if (_Other_rep && _Other_rep->_Incref_nz())
			_Reset0(_Other_ptr, _Other_rep);
		else if (_Throw)
			_THROW_NCEE(bad_weak_ptr, 0);
	}
	// 从原始指针重新赋值数据
	void _Reset0(_Ty *_Other_ptr, _Ref_count_base *_Other_rep)
	{	// release resource and take new resource
		if (_Rep != 0)
			_Rep->_Decref();// 递减左边(如赋值操作=)的智能指针的引用计数
		_Rep = _Other_rep;
		_Ptr = _Other_ptr;
	}

//
// 赋值操作,弱引用计数(_Weaks)操作
	// 递减弱引用计数_Weaks
	void _Decwref()
	{	// decrement weak reference count
		if (_Rep != 0)
			_Rep->_Decwref();
	}

	void _Resetw()
	{	// release weak reference to resource
		_Resetw((_Ty *)0, 0);
	}

	template<class _Ty2>
	void _Resetw(const _Ptr_base<_Ty2>& _Other)
	{	// release weak reference to resource and take _Other._Ptr
		_Resetw(_Other._Ptr, _Other._Rep);
	}

	template<class _Ty2>
	void _Resetw(const _Ty2 *_Other_ptr, _Ref_count_base *_Other_rep)
	{	// point to _Other_ptr through _Other_rep
		_Resetw(const_cast<_Ty2*>(_Other_ptr), _Other_rep);
	}
	// 底层_Resetw(上述_Resetw统一调用,递增、减弱引用计数)
	template<class _Ty2>
	void _Resetw(_Ty2 *_Other_ptr, _Ref_count_base *_Other_rep)
	{	// point to _Other_ptr through _Other_rep
		if (_Other_rep)
			_Other_rep->_Incwref();	// 递增右(如赋值操作=)边的智能指针的弱引用计数_Weaks
		if (_Rep != 0)
			_Rep->_Decwref();		// 递减左边(如赋值操作=)的智能指针的弱引用计数_Weaks
		_Rep = _Other_rep;
		_Ptr = _Other_ptr;
	}

private:
	_Ty *_Ptr;				// 数据 指针
	_Ref_count_base *_Rep;	// 引用计数基类 指针
	template<class _Ty0>
	friend class _Ptr_base;	// 外部能访问同类的其他对象的private成员
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值