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.

136 lines
3.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (c) Microsoft Corporation. All rights reserved.
  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. #pragma once
  14. #include <eh.h>
  15. #include <limits.h>
  16. inline void * __cdecl operator new( size_t st )
  17. {
  18. return (void *) LocalAlloc( LMEM_FIXED, st );
  19. } //new
  20. inline void __cdecl operator delete( void *pv )
  21. {
  22. if ( 0 != pv )
  23. LocalFree( (HLOCAL) pv );
  24. } //delete
  25. //+-------------------------------------------------------------------------
  26. //
  27. // Template: XPtr
  28. //
  29. // Synopsis: Template for managing ownership of memory
  30. //
  31. //--------------------------------------------------------------------------
  32. template<class T> class XPtr
  33. {
  34. public:
  35. XPtr( unsigned c = 0 ) : _p(0) { if ( 0 != c ) _p = new T [ c ]; }
  36. XPtr( T * p ) : _p( p ) {}
  37. ~XPtr() { Free(); }
  38. void SetSize( unsigned c ) { Free(); _p = new T [ c ]; }
  39. void Set ( T * p ) { _p = p; }
  40. T * Get() const { return _p ; }
  41. void Free() { delete [] Acquire(); }
  42. T & operator[]( unsigned i ) { return _p[i]; }
  43. T const & operator[]( unsigned i ) const { return _p[i]; }
  44. T * Acquire() { T * p = _p; _p = 0; return p; }
  45. BOOL IsNull() const { return ( 0 == _p ); }
  46. private:
  47. T * _p;
  48. };
  49. //+-------------------------------------------------------------------------
  50. //
  51. // Template: XInterface
  52. //
  53. // Synopsis: Template for managing ownership of interfaces
  54. //
  55. //--------------------------------------------------------------------------
  56. template<class T> class XInterface
  57. {
  58. public:
  59. XInterface( T * p = 0 ) : _p( p ) {}
  60. ~XInterface() { if ( 0 != _p ) _p->Release(); }
  61. T * operator->() { return _p; }
  62. T * GetPointer() const { return _p; }
  63. IUnknown ** GetIUPointer() { return (IUnknown **) &_p; }
  64. T ** GetPPointer() { return &_p; }
  65. void ** GetQIPointer() { return (void **) &_p; }
  66. T * Acquire() { T * p = _p; _p = 0; return p; }
  67. BOOL IsNull() { return ( 0 == _p ); }
  68. void Free() { T * p = Acquire(); if ( 0 != p ) p->Release(); }
  69. void Set( T * p ) { _p = p; }
  70. T & GetReference() { return *_p; }
  71. private:
  72. T * _p;
  73. };
  74. //+-------------------------------------------------------------------------
  75. //
  76. // Class: XBStr
  77. //
  78. // Synopsis: Class for managing ownership of BSTRings
  79. //
  80. //--------------------------------------------------------------------------
  81. class XBStr
  82. {
  83. public:
  84. XBStr( BSTR p = 0 ) : _p( p ) {}
  85. XBStr( XBStr & x ): _p( x.Acquire() ) {}
  86. ~XBStr() { Free(); }
  87. BOOL IsNull() const { return (0 == _p); }
  88. BSTR SetText( WCHAR const * pStr )
  89. {
  90. _p = SysAllocString( pStr );
  91. return _p;
  92. }
  93. void Set( BSTR pOleStr ) { _p = pOleStr; }
  94. BSTR Acquire() { BSTR p = _p; _p = 0; return p; }
  95. BSTR GetPointer() const { return _p; }
  96. void Free() { SysFreeString(Acquire()); }
  97. private:
  98. BSTR _p;
  99. };
  100. class XWindow
  101. {
  102. public:
  103. XWindow( HWND h = 0 ) : _h( h ) {}
  104. ~XWindow() { Free(); }
  105. void Free() { if ( 0 != _h ) { DestroyWindow( _h ); _h = 0; } }
  106. void Set( HWND h ) { _h = 0; }
  107. HWND Get() { return _h; }
  108. private:
  109. HWND _h;
  110. };
  111. inline ULONG CiPtrToUlong( ULONG_PTR p )
  112. {
  113. return PtrToUlong( (PVOID)p );
  114. }
  115. #define CiPtrToUint( p ) CiPtrToUlong( p )
  116. #define ArraySize( x ) ( sizeof( x ) / sizeof( x[0] ) )