📄 wy_atdestroy.h
字号:
/* Copyright is licensed under GNU LGPL. by I.J.Wang 2005 Wy_AtDestroy is a class template that destructor calls the given function. The constructor given function is mostly for releasing that function argument indicated resource. Note: return value of the called function is ignored EX1: T* t= new T; Wy_AtDestroy<void,T*> rrad(Wy::_delete_one,t); EX2: void close_fds(int fd0, int fd1) {...}; int fds[2]; ::pipe(fds); Wy_AtDestroy2<void,int,int> rrad(close_fds,fds[0],fds[1])*/#ifndef WY_ATDESTROY_H__#define WY_ATDESTROY_H__#define WY_ATDESTROY_VERSION 31#include "wyreqs.h"#include "wydefs.h"// [Syn] One argument version//// Note: If FuncArgType is neither pointer nor reference type, construct/// destruct of FuncArgType will occur. This is mostly questionable// except for the language built-in types (copy/destroy no effect)//template<typename Result, typename Arg>class Wy_AtDestroy { public: typedef Result result_type; typedef Arg argument_type; private: typedef result_type(*FuncType)(argument_type); FuncType _fptr; argument_type _arg; void _cleanup(void) { if(_fptr==NULL) { return; } FuncType t_fp(_fptr); _fptr=NULL; t_fp(_arg); }; public: // [Syn] Construct object to call the given function of the indicated // argument at object destruction // explicit // no implicit conversion Wy_AtDestroy(FuncType f, argument_type arg) : _fptr(f), _arg(arg) {}; ~Wy_AtDestroy() { this->_cleanup(); }; void release(void) WY__TSPC() { _fptr=NULL; }; private: // Hidden Wy_AtDestroy(const Wy_AtDestroy&); const Wy_AtDestroy& operator =(const Wy_AtDestroy&); bool operator==(const Wy_AtDestroy&); void* operator new(size_t);};// [Syn] One argument version// (specilizatin for void return type)//template<typename Arg>class Wy_AtDestroy<void,Arg> { public: typedef void result_type; typedef Arg argument_type; private: typedef void(*FuncType)(argument_type); FuncType _fptr; argument_type _arg; void _cleanup(void) { if(_fptr==NULL) { return; } FuncType t_fp(_fptr); _fptr=NULL; t_fp(_arg); }; public: explicit // no implicit conversion Wy_AtDestroy(FuncType f, argument_type arg) : _fptr(f), _arg(arg) {}; ~Wy_AtDestroy() { this->_cleanup(); }; void release(void) WY__TSPC() { _fptr=NULL; }; private: // Hidden Wy_AtDestroy(const Wy_AtDestroy&); const Wy_AtDestroy& operator =(const Wy_AtDestroy&); bool operator==(const Wy_AtDestroy&); void* operator new(size_t);};// [Syn] Two argument version//// Note: If FuncArgType is neither pointer nor reference type, construct/// destruct of FuncArgType will occur.//// Note: result_type is not really used, so no need to specilize for void//template<typename Result, typename Arg1, typename Arg2>class Wy_AtDestroy2 { public: typedef Result result_type; typedef Arg1 first_argument_type; typedef Arg2 second_argument_type; private: typedef result_type(*FuncType)(first_argument_type,second_argument_type); FuncType _fptr; first_argument_type _arg1; second_argument_type _arg2; void _cleanup(void) { if(_fptr==NULL) { return; } FuncType t_fp(_fptr); _fptr=NULL; t_fp(_arg1,_arg2); }; public: // [Syn] Construct object to call the given function of the indicated // argument at object destruction // explicit // no implicit conversion Wy_AtDestroy2(FuncType f, first_argument_type arg1,second_argument_type arg2) : _fptr(f), _arg1(arg1),_arg2(arg2) {}; ~Wy_AtDestroy2() { this->_cleanup(); }; void release(void) WY__TSPC() { _fptr=NULL; }; private: // Hidden Wy_AtDestroy2(const Wy_AtDestroy2&); const Wy_AtDestroy2& operator =(const Wy_AtDestroy2&); bool operator==(const Wy_AtDestroy2&); void* operator new(size_t);};namespace Wy {// [Syn] function form of delete operator//template<typename T>void _delete_one(T* p) // dtor of *p may throw { delete p; }; // but delete will nottemplate<typename T>void _delete_array(T* p) { delete[] p; };};#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -