123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- #include<new>
- #ifndef _NANXING_AUTO_PTR_H_
- #define _NANXING_AUTO_PTR_H_
- namespace nanxing
- {
- template<typename _Ptr>
- struct auto_ptr_ref
- {
- _Ptr* point;
- explicit auto_ptr_ref(_Ptr* _p):point(_p){}
- };
- template<typename _Ptr>
- class auto_ptr
- {
- private:
- _Ptr * point;
- public:
-
- explicit auto_ptr(_Ptr* _p=0)throw():point(_p){}
- auto_ptr(auto_ptr<_Ptr>& _p)throw():point(_p.release()){}
- auto_ptr(auto_ptr<_Ptr>&& _p)throw(){}
-
- auto_ptr<_Ptr>& operator=(auto_ptr<_Ptr>& _p){
- if(_p!=this)
- {
- delete this->point;
- }
- this->point=_p.release();
- return *this;
- }
- _Ptr* release()
- {
- _Ptr* tmp=point;
- point=nullptr;
- return tmp;
- }
- #ifdef _ES_UNSAFE_
- _Ptr* get() const
- {
- return point;
- }
- #endif
- void reset(_Ptr* _p=nullptr)throw()
- {
- if(_p!=point)
- {
- delete point;
- }
- point=_p;
- }
-
- operator auto_ptr_ref<_Ptr>()
- {
- return auto_ptr_ref<_Ptr>(this->release());
- }
- operator auto_ptr<_Ptr>()
- {
- return auto_ptr<_Ptr>(this->release());
- }
- ~auto_ptr()
- {
- if(point!=nullptr)
- {
- delete point;
- }
- }
- };
- }
- #endif
|