// 智能指針模板基類 _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成員
};