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.

131 lines
2.4 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998-1999 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // SmartPointer.h
  7. //
  8. // Description:
  9. // Smart pointer template class
  10. //
  11. // Author:
  12. // Galen Barbee (galenb) 19-Oct-1998
  13. //
  14. // Revision History:
  15. //
  16. // Notes:
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19. #ifndef __SMARTPOINTER_H__
  20. #define __SMARTPOINTER_H__
  21. /////////////////////////////////////////////////////////////////////////////
  22. //+++
  23. //
  24. // class: CSmartPtr
  25. //
  26. // Description:
  27. // This class template is used to encapsulate pointers to interfaces,
  28. // but in simpler way than com_ptr_t. We do not want exceptions
  29. // to be thrown on com errors (as com_ptr_t does) (except Release).
  30. // Instead, we want to process them on our own, yet still have an advantage
  31. // of proper cleaning upon destruction. Using it significantly simplifies
  32. // test code.
  33. //
  34. // Inheritance:
  35. // None.
  36. //---
  37. /////////////////////////////////////////////////////////////////////////////
  38. template< class T > class CSmartPtr
  39. {
  40. private:
  41. T * m_tPtr;
  42. void * operator new( size_t );
  43. void * operator new( size_t, void * );
  44. void operator delete( void * );
  45. public:
  46. __declspec(nothrow) CSmartPtr( T * ptr )
  47. {
  48. if ( ( m_tPtr = ptr) != NULL )
  49. {
  50. m_tPtr->AddRef();
  51. }
  52. }
  53. __declspec(nothrow) CSmartPtr( const CSmartPtr< T > * ptr )
  54. {
  55. if ( ( m_tPtr = ptr->m_tPtr ) != NULL )
  56. {
  57. m_tPtr->AddRef();
  58. }
  59. }
  60. __declspec(nothrow) CSmartPtr( void )
  61. {
  62. m_tPtr = NULL;
  63. }
  64. ~CSmartPtr( void ) throw( _com_error )
  65. {
  66. if ( m_tPtr != NULL )
  67. {
  68. m_tPtr->Release();
  69. m_tPtr = NULL;
  70. }
  71. }
  72. __declspec(nothrow) T ** operator&() const
  73. {
  74. return &m_tPtr;
  75. }
  76. __declspec(nothrow) T * operator->() const
  77. {
  78. return m_tPtr;
  79. }
  80. __declspec(nothrow) operator T * () const
  81. {
  82. return m_tPtr;
  83. }
  84. __declspec(nothrow) T * operator=( T * ptr )
  85. {
  86. if ( m_tPtr != NULL )
  87. {
  88. m_tPtr->Release();
  89. m_tPtr = NULL;
  90. }
  91. if ( ( m_tPtr = ptr ) != NULL )
  92. {
  93. m_tPtr->AddRef();
  94. }
  95. return m_tPtr;
  96. }
  97. __declspec(nothrow) bool operator==( T * ptr ) const
  98. {
  99. return m_tPtr == ptr;
  100. }
  101. __declspec(nothrow) bool operator!=( T * ptr ) const
  102. {
  103. return m_tPtr != ptr;
  104. }
  105. //
  106. // This is the only non-conforming operator in this class.
  107. //
  108. __declspec(nothrow) T * operator*() const
  109. {
  110. return m_tPtr;
  111. }
  112. }; //*** Class CSmartPtr
  113. #endif // __SMARTPOINTER_H__