Leaked source code of windows server 2003
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.

182 lines
4.2 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 FT, FT F> class OnDeleteIf0
  26. {
  27. private:
  28. bool bDismiss_;
  29. public:
  30. OnDeleteIf0():bDismiss_(false){};
  31. void dismiss(bool b = true){ bDismiss_ = b; };
  32. ~OnDeleteIf0(){ if (!bDismiss_) F(); };
  33. };
  34. template <typename T, typename FT, FT F> class OnDelete
  35. {
  36. private:
  37. T Val_;
  38. public:
  39. OnDelete(T Val):Val_(Val){};
  40. ~OnDelete(){ F(Val_); };
  41. };
  42. template <typename T1, typename T2, typename FT, FT F> class OnDelete2
  43. {
  44. private:
  45. T1 Val1_;
  46. T2 Val2_;
  47. public:
  48. OnDelete2(T1 Val1,T2 Val2):Val1_(Val1),Val2_(Val2){};
  49. ~OnDelete2(){ F(Val1_,Val2_); };
  50. };
  51. template <typename T1, typename T2, typename T3, typename FT, FT F> class OnDelete3
  52. {
  53. private:
  54. T1 Val1_;
  55. T2 Val2_;
  56. T3 Val3_;
  57. public:
  58. OnDelete3(T1 Val1,T2 Val2,T3 Val3):Val1_(Val1),Val2_(Val2),Val3_(Val3){};
  59. ~OnDelete3(){ F(Val1_,Val2_,Val3_); };
  60. };
  61. template <typename T1, typename T2, typename T3, typename FT, FT F> class OnDeleteIf3
  62. {
  63. private:
  64. T1 Val1_;
  65. T2 Val2_;
  66. T3 Val3_;
  67. bool bDismiss_;
  68. public:
  69. OnDeleteIf3(T1 Val1,T2 Val2,T3 Val3):Val1_(Val1),Val2_(Val2),Val3_(Val3),bDismiss_(false){};
  70. void dismiss(bool b = true){ bDismiss_ = b; };
  71. ~OnDeleteIf3(){ if (!bDismiss_) F(Val1_,Val2_,Val3_); };
  72. };
  73. template <typename T1, typename T2, typename FT, FT F> class OnDeleteIf2
  74. {
  75. private:
  76. T1 Val1_;
  77. T2 Val2_;
  78. bool bDismiss_;
  79. public:
  80. OnDeleteIf2(T1 Val1,T2 Val2):Val1_(Val1),Val2_(Val2),bDismiss_(false){};
  81. void dismiss(bool b = true){ bDismiss_ = b; };
  82. ~OnDeleteIf2(){ if (!bDismiss_) F(Val1_,Val2_); };
  83. };
  84. template <typename T, typename FT, FT F> class OnDeleteIf
  85. {
  86. private:
  87. T Val_;
  88. bool bDismiss_;
  89. public:
  90. OnDeleteIf(T Val):Val_(Val),bDismiss_(false){};
  91. void dismiss(bool b = true){ bDismiss_ = b; };
  92. ~OnDeleteIf(){ if (!bDismiss_) F(Val_); };
  93. };
  94. template <typename C, typename FT, FT F> class OnDeleteObj0
  95. {
  96. private:
  97. C * pObj_;
  98. public:
  99. OnDeleteObj0(C * pObj):pObj_(pObj){};
  100. ~OnDeleteObj0(){ (pObj_->*F)();};
  101. };
  102. template <typename C, typename FT, FT F> class OnDeleteObjIf0
  103. {
  104. private:
  105. C * pObj_;
  106. bool bDismiss_;
  107. public:
  108. OnDeleteObjIf0(C * pObj):pObj_(pObj),bDismiss_(false){};
  109. void dismiss(bool b = true){ bDismiss_ = b; };
  110. ~OnDeleteObjIf0(){ if (!bDismiss_) { (pObj_->*F)(); }; };
  111. };
  112. template <typename T, typename C, typename FT, FT F> class OnDeleteObj
  113. {
  114. private:
  115. C * pObj_;
  116. T Val_;
  117. public:
  118. OnDeleteObj(C * pObj,T Val):pObj_(pObj),Val_(Val){};
  119. ~OnDeleteObj(){ (pObj_->*F)(Val_);};
  120. };
  121. template <typename T1, typename T2, typename C, typename FT, FT F> class OnDeleteObj2
  122. {
  123. private:
  124. C * pObj_;
  125. T1 Val1_;
  126. T2 Val2_;
  127. public:
  128. OnDeleteObj2(C * pObj,T1 Val1, T2 Val2):pObj_(pObj),Val1_(Val1),Val2_(Val2){};
  129. ~OnDeleteObj2(){ (pObj_->*F)(Val1_,Val2_);};
  130. };
  131. template <typename T, typename C, typename FT, FT F> class OnDeleteObjIf
  132. {
  133. private:
  134. C * pObj_;
  135. T Val_;
  136. bool bDismiss_;
  137. public:
  138. OnDeleteObjIf(C * pObj,T Val):pObj_(pObj),Val_(Val),bDismiss_(false){};
  139. void dismiss(bool b = true){ bDismiss_ = b; };
  140. ~OnDeleteObjIf(){ if (!bDismiss_){ (pObj_->*F)(Val_);};};
  141. };
  142. #define RETURN_ON_FALSE( a ) \
  143. { BOOL bDoNotReuse = ( a ); if (!bDoNotReuse) return MAKE_HRESULT(SEVERITY_ERROR,FACILITY_WIN32,GetLastError()); }
  144. #define RETURN_ON_ERR( a ) \
  145. { HRESULT hrDoNotReuse = ( a ); if (FAILED(hrDoNotReuse)) return hrDoNotReuse; }
  146. #define THROW_ON_ERR( a ) \
  147. { HRESULT hrDoNotReuse = ( a ); if (FAILED(hrDoNotReuse)) _com_issue_error(hrDoNotReuse); }
  148. #define THROW_ON_RES( a ) \
  149. { LONG lResDoNotReuse = ( a ); if (ERROR_SUCCESS != lResDoNotReuse) _com_issue_error(MAKE_HRESULT(SEVERITY_ERROR,FACILITY_WIN32,lResDoNotReuse)); }
  150. typedef void (__stdcall *issue_error)(HRESULT);
  151. #endif /*_HELPER_H__*/