如何养成良好的C++编程习惯(一)―― 内存管理 (五)

2014-11-24 12:08:21 · 作者: · 浏览: 5

{
if (_Ptr != _Myptr)
{
if(_Myptr)
_Deleter::delete_ptr(_Myptr);

_Myptr = _Ptr;
}

return *this;
}

smart_ptr<_Ty, _Deleter>& reset(smart_ptr<_Ty, _Deleter>& _Right)
{
if (this != &_Right)
reset(_Right.release());

return *this;
}

_Ty* release()
{
_Ty* _Ptr = _Myptr;
_Myptr = 0;

return _Ptr;
}

smart_ptr<_Ty, _Deleter>& operator = (_Ty* _Ptr) {return reset(_Ptr);}
smart_ptr<_Ty, _Deleter>& operator = (smart_ptr<_Ty, _Deleter>& _Right) {return reset(_Right);}

bool is_valid () const {return _Myptr != 0;}
_Ty& operator * () const {return *_Myptr;}
_Ty* get () const {return _Myptr;}
_Ty* operator -> () const {return _Myptr;}
operator _Ty* () const {return _Myptr;}

private:
template smart_ptr<_Ty, _Deleter> (const smart_ptr<_Ty, _Other>&);
template smart_ptr<_Ty, _Deleter>& reset (const smart_ptr<_Ty, _Other>&);
template smart_ptr<_Ty, _Deleter>& operator = (const smart_ptr<_Ty, _Other>&);

template smart_ptr<_Ty, _Deleter> (const smart_ptr<_Other, _Deleter>&);
template smart_ptr<_Ty, _Deleter>& reset (const smart_ptr<_Other, _Deleter>&);
template smart_ptr<_Ty, _Deleter>& operator = (const smart_ptr<_Other, _Deleter>&);

protected:
_Ty* _Myptr;
};


/************************************************************************/
/* smart_simple_ptr 单实体智能指针 */
/************************************************************************/

template
class smart_simple_ptr : public smart_ptr<_Ty, simple_deleter<_Ty>>
{
public:
smart_simple_ptr(_Ty* _Ptr = 0) : smart_ptr(_Ptr) {}
smart_simple_ptr(smart_simple_ptr<_Ty>& _Right) : smart_ptr(_Right) {}
smart_simple_ptr(smart_ptr<_Ty, simple_deleter<_Ty>>& _Right) : smart_ptr(_Right) {}

smart_simple_ptr<_Ty>& operator = (smart_ptr<_Ty, simple_deleter<_Ty>>& _Right)
{return (smart_simple_ptr<_Ty>&)__super::operator = (_Right);}

smart_simple_ptr<_Ty>& operator = (smart_simple_ptr<_Ty>& _Right)
{return (smart_simple_ptr<_Ty>&)__super::operator = (_Right);}

smart_simple_ptr<_Ty>& operator = (_Ty* _Ptr)
{return (smart_simple_ptr<_Ty>&)__super::operator = (_Ptr);}

private:
template smart_simple_ptr<_Ty> (const smart_ptr<_Ty, _Other>&);
template smart_simple_ptr<_Ty>& operator = (const smart_ptr<_Ty, _Other>&);

template smart_simple_ptr<_Ty> (const smart_simple_ptr<_Other>&);
template smart_simple_ptr<_Ty>& operator = (const smart_simple_ptr<_Other>&);
};

/************************************************************************/
/* smart_gd_simple_ptr 单实体智能指针 (使用全局 delete) */
/************************************************************************/

template
class smart_gd_simple_ptr : public smart_ptr<_Ty, global_simple_deleter<_Ty>>
{
public:
smart_gd_simple_ptr(_Ty* _Ptr = 0) : smart_ptr(_Ptr) {}
smart_gd_simple_ptr(smart_gd_simple_ptr<_Ty>& _Right) : smart_ptr(_Right) {}
smart_gd_simple_ptr(smart_ptr<_Ty, global_simple_deleter<_Ty>>& _Right) : smart_ptr(_Right) {}

smart_gd_simple_ptr<_Ty>& operator = (smart_ptr<_Ty, global_simple_deleter<_Ty>>& _Right)
{return (smart_gd_simple_ptr<_Ty>&)__super::operator = (_Right);}

smart_gd_simple_ptr<_Ty>& operator = (smart_gd_simple_ptr<_Ty>& _Right)
{return (smart_gd_simple_ptr<_Ty>&)__super::operator = (_Right);}

smart_gd_simple_ptr