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.

258 lines
6.0 KiB

  1. //***************************************************************************
  2. //
  3. // Copyright � Microsoft Corporation. All rights reserved.
  4. //
  5. // SmartPtr.h
  6. //
  7. // Purpose: Declare smartpointer typedefs
  8. //
  9. //***************************************************************************
  10. #pragma once
  11. #include <io.h>
  12. _COM_SMARTPTR_TYPEDEF(IWbemClassObject, __uuidof(IWbemClassObject));
  13. _COM_SMARTPTR_TYPEDEF(IWbemObjectSink, __uuidof(IWbemObjectSink));
  14. _COM_SMARTPTR_TYPEDEF(IWbemQualifierSet, __uuidof(IWbemQualifierSet));
  15. _COM_SMARTPTR_TYPEDEF(IWbemObjectAccess, __uuidof(IWbemObjectAccess));
  16. _COM_SMARTPTR_TYPEDEF(IWbemServices, __uuidof(IWbemServices));
  17. _COM_SMARTPTR_TYPEDEF(IWbemQuery, __uuidof(IWbemQuery));
  18. _COM_SMARTPTR_TYPEDEF(IWbemContext, __uuidof(IWbemContext));
  19. _COM_SMARTPTR_TYPEDEF(IWbemCallResult, __uuidof(IWbemCallResult));
  20. _COM_SMARTPTR_TYPEDEF(IWbemObjectSink, __uuidof(IWbemObjectSink));
  21. _COM_SMARTPTR_TYPEDEF(IEnumWbemClassObject, __uuidof(IEnumWbemClassObject));
  22. _COM_SMARTPTR_TYPEDEF(IWbemLocator, __uuidof(IWbemLocator));
  23. _COM_SMARTPTR_TYPEDEF(IUnsecuredApartment, __uuidof(IUnsecuredApartment));
  24. _COM_SMARTPTR_TYPEDEF(IWbemStatusCodeText, __uuidof(IWbemStatusCodeText));
  25. //_COM_SMARTPTR_TYPEDEF(IWbemBackupRestore, __uuidof(IWbemBackupRestore));
  26. _COM_SMARTPTR_TYPEDEF(IWbemRefresher, __uuidof(IWbemRefresher));
  27. _COM_SMARTPTR_TYPEDEF(IWbemHiPerfEnum, __uuidof(IWbemHiPerfEnum));
  28. _COM_SMARTPTR_TYPEDEF(IWbemConfigureRefresher, __uuidof(IWbemConfigureRefresher));
  29. _COM_SMARTPTR_TYPEDEF(IMofCompiler, __uuidof(IMofCompiler));
  30. _COM_SMARTPTR_TYPEDEF(ExternalMethodContext, __uuidof(ExternalMethodContext));
  31. _COM_SMARTPTR_TYPEDEF(InternalMethodContext, __uuidof(InternalMethodContext));
  32. _COM_SMARTPTR_TYPEDEF(InternalMethodContextAsynch, __uuidof(InternalMethodContextAsynch));
  33. _COM_SMARTPTR_TYPEDEF(CInstance, __uuidof(CInstance));
  34. class SmartCloseHandle
  35. {
  36. private:
  37. HANDLE m_h;
  38. public:
  39. SmartCloseHandle():m_h(INVALID_HANDLE_VALUE){}
  40. SmartCloseHandle(HANDLE h):m_h(h){}
  41. ~SmartCloseHandle(){if (m_h!=INVALID_HANDLE_VALUE) CloseHandle(m_h);}
  42. HANDLE operator =(HANDLE h) {if (m_h!=INVALID_HANDLE_VALUE) CloseHandle(m_h); m_h=h; return h;}
  43. operator HANDLE() const {return m_h;}
  44. HANDLE* operator &() {if (m_h!=INVALID_HANDLE_VALUE) CloseHandle(m_h); m_h = INVALID_HANDLE_VALUE; return &m_h;}
  45. };
  46. class Smart_findclose
  47. {
  48. private:
  49. intptr_t m_h;
  50. public:
  51. Smart_findclose():m_h(0){}
  52. Smart_findclose(intptr_t h):m_h(h){}
  53. ~Smart_findclose(){if (m_h!=0) _findclose(m_h);}
  54. intptr_t operator =(intptr_t h) {if (m_h) _findclose(m_h); m_h=h; return h;}
  55. operator intptr_t() const {return m_h;}
  56. intptr_t* operator &() {if (m_h) _findclose(m_h); m_h = 0; return &m_h;}
  57. };
  58. class SmartFindClose
  59. {
  60. private:
  61. HANDLE m_h;
  62. public:
  63. SmartFindClose():m_h(INVALID_HANDLE_VALUE){}
  64. SmartFindClose(HANDLE h):m_h(h){}
  65. ~SmartFindClose(){if (m_h!=INVALID_HANDLE_VALUE) FindClose(m_h);}
  66. HANDLE operator =(HANDLE h) {if (m_h!=INVALID_HANDLE_VALUE) FindClose(m_h); m_h=h; return h;}
  67. operator HANDLE() const {return m_h;}
  68. HANDLE* operator &() {if (m_h!=INVALID_HANDLE_VALUE) FindClose(m_h); m_h = INVALID_HANDLE_VALUE; return &m_h;}
  69. };
  70. class SmartCloseServiceHandle
  71. {
  72. private:
  73. SC_HANDLE m_h;
  74. public:
  75. SmartCloseServiceHandle():m_h(NULL){}
  76. SmartCloseServiceHandle(SC_HANDLE h):m_h(h){}
  77. ~SmartCloseServiceHandle(){if (m_h!=NULL) CloseServiceHandle(m_h);}
  78. SC_HANDLE operator =(SC_HANDLE h) {if (m_h!=NULL) CloseServiceHandle(m_h); m_h=h; return h;}
  79. operator SC_HANDLE() const {return m_h;}
  80. SC_HANDLE* operator &() {if (m_h!=NULL) CloseServiceHandle(m_h); m_h = NULL; return &m_h;}
  81. };
  82. class CSmartCreatedDC
  83. {
  84. public:
  85. CSmartCreatedDC(HDC hdc) { m_hdc = hdc;}
  86. operator HDC() const {return m_hdc;}
  87. ~CSmartCreatedDC()
  88. {
  89. if (m_hdc)
  90. DeleteDC(m_hdc);
  91. }
  92. protected:
  93. HDC m_hdc;
  94. };
  95. class CSmartBuffer
  96. {
  97. private:
  98. LPBYTE m_pBuffer;
  99. public:
  100. CSmartBuffer() : m_pBuffer(NULL) {}
  101. CSmartBuffer(LPBYTE pBuffer) : m_pBuffer(pBuffer) {}
  102. CSmartBuffer(DWORD dwSize)
  103. {
  104. m_pBuffer = new BYTE[dwSize];
  105. if (m_pBuffer == NULL)
  106. {
  107. throw CHeap_Exception ( CHeap_Exception :: E_ALLOCATION_ERROR ) ;
  108. }
  109. }
  110. ~CSmartBuffer()
  111. {
  112. Free();
  113. }
  114. LPBYTE operator =(LPBYTE pBuffer)
  115. {
  116. Free();
  117. m_pBuffer = pBuffer;
  118. return m_pBuffer;
  119. }
  120. operator LPBYTE() const { return m_pBuffer; }
  121. LPBYTE* operator &()
  122. {
  123. Free();
  124. m_pBuffer = NULL;
  125. return &m_pBuffer;
  126. }
  127. protected:
  128. void Free()
  129. {
  130. if (m_pBuffer != NULL)
  131. {
  132. delete [] m_pBuffer;
  133. }
  134. }
  135. };
  136. template <typename O, typename FT, FT F> class FrameDynOnDeleteObjVoid
  137. {
  138. private:
  139. O* Obj_;
  140. BOOL bExec;
  141. public:
  142. FrameDynOnDeleteObjVoid ( O* Val ): Obj_ ( Val ), bExec ( FALSE )
  143. {
  144. };
  145. void Exec ( BOOL bSetExecFlag = TRUE )
  146. {
  147. (Obj_->*F)();
  148. if ( bSetExecFlag )
  149. {
  150. bExec = TRUE;
  151. }
  152. }
  153. void SetExecFlag ( BOOL bSetExecFlag = TRUE )
  154. {
  155. bExec = bSetExecFlag ;
  156. }
  157. void ReSetExecFlag ( )
  158. {
  159. SetExecFlag ( FALSE ) ;
  160. }
  161. ~FrameDynOnDeleteObjVoid ( )
  162. {
  163. if ( !bExec )
  164. {
  165. Exec ();
  166. }
  167. };
  168. };
  169. template <typename T, typename FT, FT F> class FrameDynOnDelete
  170. {
  171. private:
  172. T Val_;
  173. BOOL bExec;
  174. public:
  175. FrameDynOnDelete ( T Val ): Val_ ( Val ), bExec ( FALSE )
  176. {
  177. };
  178. void Exec ( )
  179. {
  180. F(Val_);
  181. bExec = TRUE;
  182. }
  183. ~FrameDynOnDelete ( )
  184. {
  185. if ( !bExec )
  186. {
  187. Exec ();
  188. }
  189. };
  190. };
  191. #ifndef __WAITEX__
  192. #define __WAITEX__
  193. template < typename T, typename FT, FT F, int iTime >
  194. class WaitException
  195. {
  196. public:
  197. WaitException ( T Val_ )
  198. {
  199. BOOL bResult = FALSE;
  200. while ( ! bResult )
  201. {
  202. try
  203. {
  204. F ( Val_ );
  205. bResult = TRUE;
  206. }
  207. catch ( ... )
  208. {
  209. }
  210. if ( ! bResult )
  211. {
  212. ::Sleep ( iTime );
  213. }
  214. }
  215. }
  216. };
  217. #endif __WAITEX__