📄 auto.h
字号:
//特注:摘自C++原来的资源^_^
//戒1:不要将不同类型的智能指针相赋值
//戒2:不要将命长的智能指针赋给命短的智能指针
//戒3:不要将智能指针作为值参传给非资源释放子程序,最好也不要用形参
//戒4:非资源申请函数不要以如何形式返回智能指针
//戒5:一个指针如果被智能指针取得就不应该让其释放给非智能指针
//说明:智能指针(引用计数的除外)都以某种形式传递一个令牌
#ifndef auto_ptr_h
#define auto_ptr_h
#ifndef __SGI_STL_MEMORY
#define __SGI_STL_MEMORY
#ifndef __STL_NOTHROW
#define __STL_NOTHROW
#endif
template<class _Ty>
class auto_ptr{
public:
typedef _Ty element_type;
explicit auto_ptr(_Ty *_P = 0) __STL_NOTHROW
: _Owns(_P != 0), _Ptr(_P)
{}
auto_ptr(const auto_ptr<_Ty>& _Y) __STL_NOTHROW
: _Owns(_Y._Owns), _Ptr(_Y.release())
{}
auto_ptr<_Ty>& operator=(const auto_ptr<_Ty>& _Y) __STL_NOTHROW
{
if (this!=&_Y)
{
if(_Ptr!=_Y.get())
{
if (_Owns)
delete _Ptr;
_Owns = _Y._Owns;
}
else if (_Y._Owns)
_Owns = true;
_Ptr = _Y.release();
}
return (*this);
}
~auto_ptr()
{
if (_Owns)
delete _Ptr;
}
_Ty& operator*() const __STL_NOTHROW
{
return (*get());
}
_Ty *operator->() const __STL_NOTHROW
{
return (get());
}
_Ty *get() const __STL_NOTHROW
{
return (_Ptr);
}
_Ty *release() const __STL_NOTHROW
{
((auto_ptr<_Ty> *)this)->_Owns = false;
return (_Ptr);
}
void reset(_Ty* __p = 0) __STL_NOTHROW
{
if(_Ptr!=__p)
{
if(_Owns)
delete _Ptr;
_Owns=true;
}
else
_Owns = true;
_Ptr=__p;
}
private:
bool _Owns;
_Ty *_Ptr;
};
//下面为智能数组
template<class _Ty>
class auto_arr{
public:
typedef _Ty element_type;
explicit auto_arr(_Ty *_P = 0) __STL_NOTHROW
: _Owns(_P != 0), _Ptr(_P)
{}
auto_arr(const auto_arr<_Ty>& _Y) __STL_NOTHROW
: _Owns(_Y._Owns), _Ptr(_Y.release())
{}
auto_arr<_Ty>& operator=(const auto_arr<_Ty>& _Y) __STL_NOTHROW
{
if (this!=&_Y)
{
if(_Ptr!=_Y.get())
{
if (_Owns)
delete[] _Ptr;
_Owns = _Y._Owns;
}
else if (_Y._Owns)
_Owns = true;
_Ptr = _Y.release();
}
return (*this);
}
~auto_arr()
{
if (_Owns)
delete[] _Ptr;
}
_Ty& operator*() const __STL_NOTHROW
{
return (*get());
}
_Ty *get() const __STL_NOTHROW
{
return (_Ptr);
}
_Ty *release() const __STL_NOTHROW
{
((auto_arr<_Ty> *)this)->_Owns = false;
return (_Ptr);
}
void reset(_Ty* __p = 0) __STL_NOTHROW {
if(_Ptr!=__p)
{
if(_Owns)
delete[] _Ptr;
_Owns=true;
}
else
_Owns = true;
_Ptr=__p;
}
_Ty& operator[](int i)
{
return (_Ptr[i]);
}
private:
bool _Owns;
_Ty *_Ptr;
};
/*智能函数*/
template<class _T>class auto_func
{
public:
typedef _T(*_command)();
explicit auto_func(_command func=0)
{_func=func;}
auto_func(auto_func&af)
{_func=af._func;}
auto_func& operator=(auto_func&af)
{_func=af._func;}
bool operator==(auto_func&af)
{return (af._func==_func);}
bool operator!=(auto_func&af)
{return (af._func!=_func);}
_T operator()(int narg=0,...);
_command get()
{return _func;}
void reset(_command func=0){_func=func;}
_command release()
{
_command tempf=_func;
_func=0;
return tempf;
}
virtual ~auto_func(){};
private:
_command _func;
};
template<class _T>
_T auto_func<_T>::operator ()(int narg,...)
{
_T newt;
int* pt=&narg,i,args;
for(i=narg;i>=1;i--)
{
args=*(pt+i);
_asm push args
}
if(_func!=0)
newt=_func();
narg*=4;
_asm add esp,narg
return newt;
}
/*智能过程*/
class auto_proc{
public:
typedef void(*_command)();
explicit auto_proc(_command proc=0){_proc=proc;}
auto_proc(auto_proc&af){_proc=af._proc;}
auto_proc& operator=(auto_proc&af){_proc=af._proc;}
bool operator==(auto_proc&af){return (af._proc==_proc);}
bool operator!=(auto_proc&af){return (af._proc!=_proc);}
void operator()(int narg=0,...)
{
if(_proc==0)return;
int* pt=&narg,i,args;
for(i=narg;i>=1;i--)
{
args=*(pt+i);
_asm push args
}
_proc();
narg*=4;
_asm add esp,narg
return;
}
_command get(){return _proc;}
void reset(_command proc=0){_proc=proc;}
_command release(){_command tempp=_proc;_proc=0;return tempp;}
private:
_command _proc;
};
#endif
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -