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.

217 lines
4.0 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992-1998
  5. //
  6. // File: smartptr.hxx
  7. //
  8. // Contents: Macros for smart pointers
  9. //
  10. // History: 20-May-98 SitaramR Created
  11. //---------------------------------------------------------------------------
  12. #pragma once
  13. #include "alloc.h"
  14. //+---------------------------------------------------------------------------
  15. //
  16. // Class: XPtr<class CItem>
  17. //
  18. // Purpose: Smart Pointer template
  19. //
  20. // History: 20-May-98 SitaramR Created
  21. //
  22. // Notes: Usage
  23. //
  24. // XPtr<CWidget> xWidget(pWidget);
  25. // xWidget->WidgetMethod(...);
  26. // CWidget pW = xWidget.Acquire();
  27. //
  28. //----------------------------------------------------------------------------
  29. template<class CItem> class XPtr
  30. {
  31. public:
  32. XPtr(CItem* p = 0) : _p( p )
  33. {
  34. }
  35. XPtr ( XPtr<CItem> & x )
  36. {
  37. _p = x.Acquire();
  38. }
  39. ~XPtr() { delete _p; }
  40. CItem* operator->() { return _p; }
  41. CItem const * operator->() const { return _p; }
  42. BOOL IsNull() const { return (0 == _p); }
  43. void Set ( CItem* p )
  44. {
  45. Assert (0 == _p);
  46. _p = p;
  47. }
  48. CItem * Acquire()
  49. {
  50. CItem * pTemp = _p;
  51. _p = 0;
  52. return pTemp;
  53. }
  54. CItem & GetReference() const
  55. {
  56. Assert( 0 != _p );
  57. return *_p;
  58. }
  59. CItem * GetPointer() const { return _p; }
  60. void Free() { delete Acquire(); }
  61. private:
  62. XPtr<CItem>& operator=( const XPtr<CItem> & x );
  63. CItem * _p;
  64. };
  65. //+---------------------------------------------------------------------------
  66. //
  67. // Class: XPtrST<class CItem>
  68. //
  69. // Purpose: Smart Pointer template for Simple Types
  70. //
  71. // History: 20-Mar-98 SitaramR Created
  72. //
  73. //----------------------------------------------------------------------------
  74. template<class CItem> class XPtrST
  75. {
  76. public:
  77. XPtrST(CItem* p = 0) : _p( p )
  78. {
  79. }
  80. ~XPtrST() { delete _p; }
  81. BOOL IsNull() const { return ( 0 == _p ); }
  82. void Set ( CItem* p )
  83. {
  84. Assert( 0 == _p );
  85. _p = p;
  86. }
  87. CItem * Acquire()
  88. {
  89. CItem * pTemp = _p;
  90. _p = 0;
  91. return pTemp;
  92. }
  93. CItem & GetReference() const
  94. {
  95. Assert( 0 != _p );
  96. return *_p;
  97. }
  98. CItem * GetPointer() const { return _p ; }
  99. void Free() { delete Acquire(); }
  100. private:
  101. XPtrST (const XPtrST<CItem> & x);
  102. XPtrST<CItem> & operator=( const XPtrST<CItem> & x);
  103. CItem * _p;
  104. };
  105. //+---------------------------------------------------------------------------
  106. //
  107. // Class: XAllocPtr
  108. //
  109. // Purpose: Smart Pointer to void, which is FREE'd, not delete'd
  110. //
  111. // History: 03-Aug-98 SitaramR Created
  112. //
  113. //----------------------------------------------------------------------------
  114. class XAllocPtr
  115. {
  116. public:
  117. XAllocPtr(void* p = 0) : _p( p )
  118. {
  119. }
  120. ~XAllocPtr() { FREE( _p ); }
  121. BOOL IsNull() const { return (0 == _p); }
  122. void Set ( void* p )
  123. {
  124. Assert (0 == _p);
  125. _p = p;
  126. }
  127. void * Acquire()
  128. {
  129. void * pTemp = _p;
  130. _p = 0;
  131. return pTemp;
  132. }
  133. void * GetPointer() const { return _p; }
  134. void Free() { FREE ( Acquire() ); }
  135. private:
  136. void * _p;
  137. };
  138. //+---------------------------------------------------------------------------
  139. //
  140. // Class: XRegKey
  141. //
  142. // Purpose: Smart registy key
  143. //
  144. // History: 20-May-98 SitaramR Created
  145. //
  146. //----------------------------------------------------------------------------
  147. class XRegKey
  148. {
  149. public:
  150. XRegKey( HKEY key ) : _key( key ) {}
  151. XRegKey() : _key( 0 ) {}
  152. ~XRegKey() { Free(); }
  153. void Set( HKEY key ) { Assert( 0 == _key ); _key = key; }
  154. HKEY * GetPointer() { Assert( 0 == _key ); return &_key; }
  155. HKEY Get() { return _key; }
  156. BOOL IsNull() { return ( 0 == _key ); }
  157. HKEY Acquire() { HKEY tmp = _key; _key = 0; return tmp; }
  158. void Free() { if ( 0 != _key ) { RegCloseKey( _key ); _key = 0; } }
  159. private:
  160. HKEY _key;
  161. };