home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / atltangram / util.h < prev   
C/C++ Source or Header  |  1998-03-26  |  3KB  |  96 lines

  1. // util.h - shared utilities
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #ifndef __Util_h__
  14. #define __Util_h__
  15.  
  16. #include <assert.h>
  17.  
  18. // Displays a message box with an error string in it.
  19. void ErrorMessage(LPCTSTR str, HRESULT hr) ;
  20.  
  21. // Determine if two interfaces below to the same component.
  22. BOOL InterfacesAreOnSameComponent(IUnknown* pI1, IUnknown* pI2) ;
  23.  
  24. // Displays messages using OutputDebugString
  25. void __cdecl MyTrace(LPCTSTR lpszFormat, ...);
  26.  
  27. // Determine if an address is accessable.
  28. BOOL IsValidAddress(const void* lp, UINT nBytes = 1, BOOL bReadWrite = FALSE) ;
  29.  
  30. // Determine if interface pointer is accessable.
  31. inline BOOL IsValidInterface(IUnknown* p)
  32. {
  33.     return (p != NULL) && IsValidAddress(p, sizeof(IUnknown*), FALSE) ;
  34. }
  35.  
  36. // Determine if the out parameter for an interface pointer is accessable.
  37. template <class T>
  38. inline BOOL IsValidInterfaceOutParam(T** p)
  39. {
  40.     return (p != NULL) && IsValidAddress(p, sizeof(IUnknown*), TRUE) ;
  41. }
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // Diagnostic support
  45. // Helper function for checking HRESULTs.
  46.  
  47. #ifdef _DEBUG
  48. inline void CheckResult(HRESULT hr)
  49. {
  50.     if (FAILED(hr))
  51.     {
  52.         ErrorMessage(NULL, hr) ;
  53.         assert(FAILED(hr)) ;
  54.     }
  55. }
  56.  
  57. #define ASSERT_HRESULT      CheckResult
  58. #else
  59. #define ASSERT_HRESULT
  60. #endif
  61.  
  62. /////////////////////////////////////////////////////////////////////////////
  63. // More Diagnostic support which mimics MFC
  64.  
  65. #ifndef __AFX_H__   // Only define these if MFC has not already been included
  66. #ifdef _DEBUG
  67.  
  68. #define ASSERT              assert
  69. #define VERIFY(f)           ASSERT(f)
  70. #define DEBUG_ONLY(f)       (f)
  71.  
  72. #define TRACE               ::MyTrace
  73. #define TRACE0(sz)          ::MyTrace(_T("%s"), _T(sz))
  74.  
  75. #else   // _DEBUG
  76.  
  77. #define ASSERT
  78. #define VERIFY(f)           ((void)(f))
  79. #define ASSERT_VALID(pOb)   ((void)0)
  80. #define DEBUG_ONLY(f)       ((void)0)
  81.  
  82. #define TRACE
  83. #define TRACE0(sz)
  84.  
  85. #endif // !_DEBUG
  86.  
  87. #define ASSERT_POINTER(p, type) \
  88.     ASSERT(((p) != NULL) && IsValidAddress((p), sizeof(type), FALSE))
  89.  
  90. #define ASSERT_NULL_OR_POINTER(p, type) \
  91.     ASSERT(((p) == NULL) || IsValidAddress((p), sizeof(type), FALSE))
  92.  
  93. #endif // TRACE
  94.  
  95. #endif // __Util_h__
  96.