#ifndef __HELPER_H__ #define __HELPER_H__ #include #include #include #ifndef LENGTH_OF #define LENGTH_OF(a) (sizeof(a)/sizeof(a[0])) #endif #ifndef _DBG_ASSERT #ifdef DBG #define _DBG_ASSERT(X) { if (!(X)) { DebugBreak(); } } #else #define _DBG_ASSERT(X) #endif #endif inline void RM(IUnknown * p){ p->Release(); }; template class OnDelete0 { public: OnDelete0(){}; ~OnDelete0(){ F(); }; }; template class OnDeleteIf0 { private: bool bDismiss_; public: OnDeleteIf0():bDismiss_(false){}; void dismiss(bool b = true){ bDismiss_ = b; }; ~OnDeleteIf0(){ if (!bDismiss_) F(); }; }; template class OnDelete { private: T Val_; public: OnDelete(T Val):Val_(Val){}; ~OnDelete(){ F(Val_); }; }; template class OnDelete2 { private: T1 Val1_; T2 Val2_; public: OnDelete2(T1 Val1,T2 Val2):Val1_(Val1),Val2_(Val2){}; ~OnDelete2(){ F(Val1_,Val2_); }; }; template class OnDelete3 { private: T1 Val1_; T2 Val2_; T3 Val3_; public: OnDelete3(T1 Val1,T2 Val2,T3 Val3):Val1_(Val1),Val2_(Val2),Val3_(Val3){}; ~OnDelete3(){ F(Val1_,Val2_,Val3_); }; }; template class OnDeleteIf3 { private: T1 Val1_; T2 Val2_; T3 Val3_; bool bDismiss_; public: OnDeleteIf3(T1 Val1,T2 Val2,T3 Val3):Val1_(Val1),Val2_(Val2),Val3_(Val3),bDismiss_(false){}; void dismiss(bool b = true){ bDismiss_ = b; }; ~OnDeleteIf3(){ if (!bDismiss_) F(Val1_,Val2_,Val3_); }; }; template class OnDeleteIf2 { private: T1 Val1_; T2 Val2_; bool bDismiss_; public: OnDeleteIf2(T1 Val1,T2 Val2):Val1_(Val1),Val2_(Val2),bDismiss_(false){}; void dismiss(bool b = true){ bDismiss_ = b; }; ~OnDeleteIf2(){ if (!bDismiss_) F(Val1_,Val2_); }; }; template class OnDeleteIf { private: T Val_; bool bDismiss_; public: OnDeleteIf(T Val):Val_(Val),bDismiss_(false){}; void dismiss(bool b = true){ bDismiss_ = b; }; ~OnDeleteIf(){ if (!bDismiss_) F(Val_); }; }; template class OnDeleteObj0 { private: C * pObj_; public: OnDeleteObj0(C * pObj):pObj_(pObj){}; ~OnDeleteObj0(){ (pObj_->*F)();}; }; template class OnDeleteObjIf0 { private: C * pObj_; bool bDismiss_; public: OnDeleteObjIf0(C * pObj):pObj_(pObj),bDismiss_(false){}; void dismiss(bool b = true){ bDismiss_ = b; }; ~OnDeleteObjIf0(){ if (!bDismiss_) { (pObj_->*F)(); }; }; }; template class OnDeleteObj { private: C * pObj_; T Val_; public: OnDeleteObj(C * pObj,T Val):pObj_(pObj),Val_(Val){}; ~OnDeleteObj(){ (pObj_->*F)(Val_);}; }; template class OnDeleteObj2 { private: C * pObj_; T1 Val1_; T2 Val2_; public: OnDeleteObj2(C * pObj,T1 Val1, T2 Val2):pObj_(pObj),Val1_(Val1),Val2_(Val2){}; ~OnDeleteObj2(){ (pObj_->*F)(Val1_,Val2_);}; }; template class OnDeleteObjIf { private: C * pObj_; T Val_; bool bDismiss_; public: OnDeleteObjIf(C * pObj,T Val):pObj_(pObj),Val_(Val),bDismiss_(false){}; void dismiss(bool b = true){ bDismiss_ = b; }; ~OnDeleteObjIf(){ if (!bDismiss_){ (pObj_->*F)(Val_);};}; }; #define RETURN_ON_FALSE( a ) \ { BOOL bDoNotReuse = ( a ); if (!bDoNotReuse) return MAKE_HRESULT(SEVERITY_ERROR,FACILITY_WIN32,GetLastError()); } #define RETURN_ON_ERR( a ) \ { HRESULT hrDoNotReuse = ( a ); if (FAILED(hrDoNotReuse)) return hrDoNotReuse; } #define THROW_ON_ERR( a ) \ { HRESULT hrDoNotReuse = ( a ); if (FAILED(hrDoNotReuse)) _com_issue_error(hrDoNotReuse); } #define THROW_ON_RES( a ) \ { LONG lResDoNotReuse = ( a ); if (ERROR_SUCCESS != lResDoNotReuse) _com_issue_error(MAKE_HRESULT(SEVERITY_ERROR,FACILITY_WIN32,lResDoNotReuse)); } typedef void (__stdcall *issue_error)(HRESULT); #endif /*_HELPER_H__*/