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.

258 lines
7.0 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // File: tmutils.h
  4. //
  5. // Description: Useful Tepmlate Code, debugging stuff, general utilities
  6. // used throughout the termmgr and MST
  7. //
  8. ///////////////////////////////////////////////////////////////////////////////
  9. //
  10. // Note:
  11. //
  12. // use tm.h to define symbols shared throughout modules composing terminal
  13. // manager
  14. //
  15. #ifndef ___TM_UTILS_INCLUDED___
  16. #define ___TM_UTILS_INCLUDED___
  17. #if defined(_DEBUG)
  18. #define new new(_NORMAL_BLOCK, __FILE__, __LINE__)
  19. #endif
  20. #define DECLARE_VQI() \
  21. STDMETHOD(QueryInterface)(REFIID iid, void ** ppvObject) = 0; \
  22. STDMETHOD_(ULONG, AddRef)() = 0; \
  23. STDMETHOD_(ULONG, Release)() = 0;
  24. bool IsSameObject(IUnknown *pUnk1, IUnknown *pUnk2);
  25. STDAPI_(void) TStringFromGUID(const GUID* pguid, LPTSTR pszBuf);
  26. #ifdef UNICODE
  27. #define WStringFromGUID TStringFromGUID
  28. #else
  29. STDAPI_(void) WStringFromGUID(const GUID* pguid, LPWSTR pszBuf);
  30. #endif
  31. void InitMediaType(AM_MEDIA_TYPE *pmt);
  32. // We use ATL for our lists
  33. // #include <atlapp.h>
  34. // #define CList CSimpleArray
  35. // We use our own assertions
  36. #define ASSERT(x) TM_ASSERT(x)
  37. // inline fns, macros
  38. inline BOOL
  39. HRESULT_FAILURE(
  40. IN HRESULT HResult
  41. )
  42. {
  43. // ZoltanS: We now consider S_FALSE to be success. Hopefully nothing
  44. // depends on this...
  45. // return (FAILED(HResult) || (S_FALSE == HResult));
  46. return FAILED(HResult);
  47. }
  48. // ZoltanS: We now consider S_FALSE to be success. Hopefully nothing
  49. // depends on this...
  50. // if ( FAILED(LocalHResult) || (S_FALSE == LocalHResult) )
  51. #define BAIL_ON_FAILURE(MacroHResult) \
  52. { \
  53. HRESULT LocalHResult = MacroHResult ; \
  54. if ( FAILED(LocalHResult) ) \
  55. { \
  56. LOG((MSP_ERROR, "BAIL_ON_FAILURE - error %x", LocalHResult)); \
  57. return LocalHResult; \
  58. } \
  59. }
  60. // NULL is second - this way if an == operator
  61. // is defined on Ptr, the operator may be used
  62. #define BAIL_IF_NULL(Ptr, ReturnValue) \
  63. { \
  64. void *LocalPtr = (void *)Ptr; \
  65. if ( LocalPtr == NULL ) \
  66. { \
  67. LOG((MSP_ERROR, "BAIL_IF_NULL - ret value %x", ReturnValue)); \
  68. return ReturnValue; \
  69. } \
  70. }
  71. // sets the first bit to indicate error
  72. // sets the win32 facility code
  73. // this is used instead of the HRESULT_FROM_WIN32 macro because that clears the customer flag
  74. inline long
  75. HRESULT_FROM_ERROR_CODE(
  76. IN long ErrorCode
  77. )
  78. {
  79. // LOG((MSP_ERROR, "HRESULT_FROM_ERROR_CODE - error %x", (0x80070000 | (0xa000ffff & ErrorCode))));
  80. return ( 0x80070000 | (0xa000ffff & ErrorCode) );
  81. }
  82. /////////////////////////////////////////////////////////////////////////////
  83. /////////////////////////////////////////////////////////////////////////////
  84. // Better auto critical section lock
  85. /////////////////////////////////////////////////////////////////////////////
  86. /////////////////////////////////////////////////////////////////////////////
  87. // locks a critical section, and unlocks it automatically
  88. // when the lock goes out of scope ONLY if (and as many times as)
  89. // it holds it
  90. // It may also be locked and unlocked independently
  91. template <class T>
  92. class LOCAL_CRIT_LOCK
  93. {
  94. public:
  95. LOCAL_CRIT_LOCK(
  96. IN T *plock
  97. )
  98. {
  99. m_pLock = plock;
  100. m_pLock->Lock();
  101. NumLocksHeld = 1;
  102. }
  103. BOOL IsLocked(
  104. )
  105. {
  106. return ( (NumLocksHeld > 0)? TRUE : FALSE );
  107. }
  108. void Lock(
  109. )
  110. {
  111. m_pLock->Lock();
  112. NumLocksHeld++;
  113. }
  114. void Unlock(
  115. )
  116. {
  117. NumLocksHeld--;
  118. m_pLock->Unlock();
  119. }
  120. ~LOCAL_CRIT_LOCK(
  121. )
  122. {
  123. while (IsLocked())
  124. {
  125. Unlock();
  126. }
  127. }
  128. protected:
  129. DWORD NumLocksHeld;
  130. T *m_pLock;
  131. private:
  132. // make copy constructor and assignment operator inaccessible
  133. LOCAL_CRIT_LOCK(
  134. IN const LOCAL_CRIT_LOCK<T> &RefLocalLock
  135. );
  136. LOCAL_CRIT_LOCK<T> &operator=(const LOCAL_CRIT_LOCK<T> &RefLocalLock);
  137. };
  138. typedef LOCAL_CRIT_LOCK<CComObjectRoot> COM_LOCAL_CRIT_LOCK;
  139. #ifdef DBG
  140. //
  141. // Declare methods to log the AddRef/Release calls and values
  142. //
  143. #define DECLARE_DEBUG_ADDREF_RELEASE(x) \
  144. void LogDebugAddRef(DWORD dw) \
  145. { LOG((MSP_TRACE, "%s::AddRef() = %d", _T(#x), dw)); } \
  146. void LogDebugRelease(DWORD dw) \
  147. { LOG((MSP_TRACE, "%s::Release() = %d", _T(#x), dw)); }
  148. //
  149. // Create a template class derived from CComObject to supply
  150. // the debug logic.
  151. //
  152. template <class base>
  153. class CTMComObject : public CComObject<base>
  154. {
  155. typedef CComObject<base> _BaseClass;
  156. STDMETHOD_(ULONG, AddRef)()
  157. {
  158. DWORD dwR = _BaseClass::AddRef();
  159. base::LogDebugAddRef(m_dwRef);
  160. return dwR;
  161. }
  162. STDMETHOD_(ULONG, Release)()
  163. {
  164. DWORD dwRef = m_dwRef;
  165. DWORD dwR = _BaseClass::Release();
  166. LogDebugRelease(--dwRef);
  167. return dwR;
  168. }
  169. };
  170. #else // #ifdef DBG
  171. #define DECLARE_DEBUG_ADDREF_RELEASE(x)
  172. #endif // #ifdef DBG
  173. // ??? why???
  174. #ifndef __WXUTIL__
  175. // locks a critical section, and unlocks it automatically
  176. // when the lock goes out of scope
  177. class CAutoObjectLock {
  178. // make copy constructor and assignment operator inaccessible
  179. CAutoObjectLock(const CAutoObjectLock &refAutoLock);
  180. CAutoObjectLock &operator=(const CAutoObjectLock &refAutoLock);
  181. protected:
  182. CComObjectRoot * m_pObject;
  183. public:
  184. CAutoObjectLock(CComObjectRoot * pobject)
  185. {
  186. m_pObject = pobject;
  187. m_pObject->Lock();
  188. };
  189. ~CAutoObjectLock() {
  190. m_pObject->Unlock();
  191. };
  192. };
  193. #define AUTO_CRIT_LOCK CAutoObjectLock lck(this);
  194. #ifdef _DEBUG
  195. #define EXECUTE_ASSERT(_x_) TM_ASSERT(_x_)
  196. #else
  197. #define EXECUTE_ASSERT(_x_) _x_
  198. #endif
  199. // ??? why???
  200. #endif // #ifndef __WXUTIL__
  201. #endif // ___TM_UTILS_INCLUDED___
  202. // eof