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.

207 lines
5.7 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1999-2000
  5. //
  6. // File: minici.hxx
  7. //
  8. // Contents: The CI basics for exception handling and smart pointers
  9. //
  10. // History: 26-Jul-1999 dlee Created
  11. //
  12. //--------------------------------------------------------------------------
  13. #if DBG == 1
  14. inline void __AssertFailure( const char *pszFile,
  15. int iLine,
  16. char const *pszMsg )
  17. {
  18. char ac[200];
  19. sprintf( ac, "assert in %s at line %d: ",
  20. pszFile, iLine );
  21. OutputDebugStringA( ac );
  22. OutputDebugStringA( pszMsg );
  23. OutputDebugStringA( "\n" );
  24. //DebugBreak();
  25. }
  26. #define Win4Assert(x) (void)((x) || (__AssertFailure( __FILE__, __LINE__, #x ),0))
  27. #else
  28. #define Win4Assert(x)
  29. #endif
  30. class CException
  31. {
  32. public:
  33. CException(long lError) : _lError(lError), _dbgInfo(0) {}
  34. CException() :
  35. _dbgInfo( 0 ),
  36. _lError( HRESULT_FROM_WIN32( GetLastError() ) ) {}
  37. long GetErrorCode() { return _lError;}
  38. void SetInfo(unsigned dbgInfo) { _dbgInfo = dbgInfo; }
  39. unsigned long GetInfo(void) const { return _dbgInfo; }
  40. protected:
  41. long _lError;
  42. unsigned long _dbgInfo;
  43. };
  44. #if DBG == 1
  45. inline void __DoThrow( const char * pszFile,
  46. int iLine,
  47. CException & e,
  48. const char * pszMsg )
  49. {
  50. char ac[200];
  51. sprintf( ac, "throw %#x %s line %d of %s\n",
  52. e.GetErrorCode(), pszMsg, iLine, pszFile );
  53. OutputDebugStringA( ac );
  54. throw e;
  55. }
  56. #define THROW( e ) __DoThrow( __FILE__, __LINE__, e, "" )
  57. #define THROWMSG( e, msg ) __DoThrow( __FILE__, __LINE__, e, msg )
  58. #else
  59. #define THROW( e ) throw e
  60. #define THROWMSG( e, msg ) throw e
  61. #endif
  62. #define TRY try
  63. #define CATCH( class, e ) catch( class & e )
  64. #define END_CATCH
  65. inline void * __cdecl operator new( size_t st )
  66. {
  67. void *p = (void *) LocalAlloc( LMEM_FIXED, st );
  68. if ( 0 == p )
  69. THROWMSG( CException(), "out of memory" );
  70. return p;
  71. } //new
  72. inline void __cdecl operator delete( void *pv )
  73. {
  74. if ( 0 != pv )
  75. LocalFree( (HLOCAL) pv );
  76. } //delete
  77. inline void _cdecl SystemExceptionTranslator(
  78. unsigned int uiWhat,
  79. struct _EXCEPTION_POINTERS * pexcept )
  80. {
  81. THROWMSG( CException( uiWhat ), "translated system exception" );
  82. }
  83. #pragma warning(4:4535) // set_se_translator used w/o /EHa
  84. class CTranslateSystemExceptions
  85. {
  86. public:
  87. CTranslateSystemExceptions()
  88. {
  89. _tf = _set_se_translator( SystemExceptionTranslator );
  90. }
  91. ~CTranslateSystemExceptions()
  92. {
  93. _set_se_translator( _tf );
  94. }
  95. private:
  96. _se_translator_function _tf;
  97. };
  98. //+-------------------------------------------------------------------------
  99. //
  100. // Template: XPtr
  101. //
  102. // Synopsis: Template for managing ownership of memory
  103. //
  104. //--------------------------------------------------------------------------
  105. template<class T> class XPtr
  106. {
  107. public:
  108. XPtr( unsigned c = 0 ) : _p(0) { if ( 0 != c ) _p = new T [ c ]; }
  109. XPtr( T * p ) : _p( p ) {}
  110. ~XPtr() { Free(); }
  111. void SetSize( unsigned c ) { Free(); _p = new T [ c ]; }
  112. void Set ( T * p ) { Win4Assert( 0 == _p ); _p = p; }
  113. T * Get() const { return _p ; }
  114. void Free() { delete [] Acquire(); }
  115. T & operator[]( unsigned i ) { return _p[i]; }
  116. T const & operator[]( unsigned i ) const { return _p[i]; }
  117. T * Acquire() { T * p = _p; _p = 0; return p; }
  118. BOOL IsNull() const { return ( 0 == _p ); }
  119. private:
  120. T * _p;
  121. };
  122. //+-------------------------------------------------------------------------
  123. //
  124. // Template: XInterface
  125. //
  126. // Synopsis: Template for managing ownership of interfaces
  127. //
  128. //--------------------------------------------------------------------------
  129. template<class T> class XInterface
  130. {
  131. public:
  132. XInterface( T * p = 0 ) : _p( p ) {}
  133. ~XInterface() { if ( 0 != _p ) _p->Release(); }
  134. T * operator->() { return _p; }
  135. T * GetPointer() const { return _p; }
  136. IUnknown ** GetIUPointer() { return (IUnknown **) &_p; }
  137. T ** GetPPointer() { return &_p; }
  138. void ** GetQIPointer() { return (void **) &_p; }
  139. T * Acquire() { T * p = _p; _p = 0; return p; }
  140. BOOL IsNull() { return ( 0 == _p ); }
  141. void Free() { T * p = Acquire(); if ( 0 != p ) p->Release(); }
  142. private:
  143. T * _p;
  144. };
  145. //+-------------------------------------------------------------------------
  146. //
  147. // Class: XBStr
  148. //
  149. // Synopsis: Class for managing ownership of BSTRings
  150. //
  151. //--------------------------------------------------------------------------
  152. class XBStr
  153. {
  154. public:
  155. XBStr( BSTR p = 0 ) : _p( p ) {}
  156. XBStr( XBStr & x ): _p( x.Acquire() ) {}
  157. ~XBStr() { Free(); }
  158. BOOL IsNull() const { return (0 == _p); }
  159. BSTR SetText( WCHAR const * pStr )
  160. {
  161. Win4Assert( 0 == _p );
  162. _p = SysAllocString( pStr );
  163. if ( 0 == _p )
  164. THROWMSG( CException( E_OUTOFMEMORY ), "can't allocate bstr" );
  165. return _p;
  166. }
  167. void Set( BSTR pOleStr ) { _p = pOleStr; }
  168. BSTR Acquire() { BSTR p = _p; _p = 0; return p; }
  169. BSTR GetPointer() const { return _p; }
  170. void Free() { SysFreeString(Acquire()); }
  171. private:
  172. BSTR _p;
  173. };
  174. inline ULONG CiPtrToUlong( ULONG_PTR p )
  175. {
  176. Win4Assert( p <= ULONG_MAX );
  177. return PtrToUlong( (PVOID)p );
  178. }
  179. #define CiPtrToUint( p ) CiPtrToUlong( p )