Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

127 lines
2.7 KiB

  1. #ifndef __HELPER_H__
  2. #define __HELPER_H__
  3. #include <windows.h>
  4. #include <ole2.h>
  5. #include <comdef.h>
  6. #ifndef LENGTH_OF
  7. #define LENGTH_OF(a) (sizeof(a)/sizeof(a[0]))
  8. #endif
  9. #ifndef _DBG_ASSERT
  10. #ifdef DBG
  11. #define _DBG_ASSERT(X) { if (!(X)) { DebugBreak(); } }
  12. #else
  13. #define _DBG_ASSERT(X)
  14. #endif
  15. #endif
  16. inline void RM(IUnknown * p){
  17. p->Release();
  18. };
  19. template <typename FT, FT F> class OnDelete0
  20. {
  21. public:
  22. OnDelete0(){};
  23. ~OnDelete0(){ F(); };
  24. };
  25. template <typename T, typename FT, FT F> class OnDelete
  26. {
  27. private:
  28. T Val_;
  29. public:
  30. OnDelete(T Val):Val_(Val){};
  31. ~OnDelete(){ F(Val_); };
  32. };
  33. template <typename T1, typename T2, typename FT, FT F> class OnDelete2
  34. {
  35. private:
  36. T1 Val1_;
  37. T2 Val2_;
  38. public:
  39. OnDelete2(T1 Val1,T2 Val2):Val1_(Val1),Val2_(Val2){};
  40. ~OnDelete2(){ F(Val1_,Val2_); };
  41. };
  42. template <typename T, typename FT, FT F> class OnDeleteIf
  43. {
  44. private:
  45. T Val_;
  46. bool bDismiss_;
  47. public:
  48. OnDeleteIf(T Val):Val_(Val),bDismiss_(false){};
  49. void dismiss(bool b = true){ bDismiss_ = b; };
  50. ~OnDeleteIf(){ if (!bDismiss_) F(Val_); };
  51. };
  52. template <typename C, typename FT, FT F> class OnDeleteObj0
  53. {
  54. private:
  55. C * pObj_;
  56. public:
  57. OnDeleteObj0(C * pObj):pObj_(pObj){};
  58. ~OnDeleteObj0(){ (pObj_->*F)();};
  59. };
  60. template <typename C, typename FT, FT F> class OnDeleteObjIf0
  61. {
  62. private:
  63. C * pObj_;
  64. bool bDismiss_;
  65. public:
  66. OnDeleteObjIf0(C * pObj):pObj_(pObj),bDismiss_(false){};
  67. void dismiss(bool b = true){ bDismiss_ = b; };
  68. ~OnDeleteObjIf0(){ if (!bDismiss_) { (pObj_->*F)(); }; };
  69. };
  70. template <typename T, typename C, typename FT, FT F> class OnDeleteObj
  71. {
  72. private:
  73. C * pObj_;
  74. T Val_;
  75. public:
  76. OnDeleteObj(C * pObj,T Val):pObj_(pObj),Val_(Val){};
  77. ~OnDeleteObj(){ (pObj_->*F)(Val_);};
  78. };
  79. template <typename T, typename C, typename FT, FT F> class OnDeleteObjIf
  80. {
  81. private:
  82. C * pObj_;
  83. T Val_;
  84. bool bDismiss_;
  85. public:
  86. OnDeleteObjIf(C * pObj,T Val):pObj_(pObj),Val_(Val),bDismiss_(false){};
  87. void dismiss(bool b = true){ bDismiss_ = b; };
  88. ~OnDeleteObjIf(){ if (!bDismiss_){ (pObj_->*F)(Val_);};};
  89. };
  90. #define RETURN_ON_FALSE( a ) \
  91. { BOOL bDoNotReuse = ( a ); if (!bDoNotReuse) return MAKE_HRESULT(SEVERITY_ERROR,FACILITY_WIN32,GetLastError()); }
  92. #define RETURN_ON_ERR( a ) \
  93. { HRESULT hrDoNotReuse = ( a ); if (FAILED(hrDoNotReuse)) return hrDoNotReuse; }
  94. #define THROW_ON_ERR( a ) \
  95. { HRESULT hrDoNotReuse = ( a ); if (FAILED(hrDoNotReuse)) _com_issue_error(hrDoNotReuse); }
  96. #define THROW_ON_RES( a ) \
  97. { LONG lResDoNotReuse = ( a ); if (ERROR_SUCCESS != lResDoNotReuse) _com_issue_error(MAKE_HRESULT(SEVERITY_ERROR,FACILITY_WIN32,lResDoNotReuse)); }
  98. typedef void (__stdcall *issue_error)(HRESULT);
  99. inline
  100. void no_error(HRESULT hr){
  101. return;
  102. }
  103. #endif /*_HELPER_H__*/