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.

226 lines
5.4 KiB

  1. #ifndef _REFPTR_H_
  2. #define _REFPTR_H_
  3. /////////////////////////////////////////////////////////////////////////////
  4. //
  5. // TRefPtr
  6. //
  7. // This ref pointer template is useful for any objects that are referenced
  8. // multiple times.
  9. //
  10. // The ref pointer depends on AddRef and Release being defined in the class
  11. // type T. ( AddRef should increment a reference counter, release should
  12. // decrement it and delete itself if the count is 0). AddRef is called
  13. // upon construction and Release is called upon destruction. Much care should
  14. // go into defining AddRef and Release if the smart pointer is used across
  15. // thread boundaries, since smart pointer don't force thread-safety. In
  16. // particular, an object could get deleted twice if smart pointers in
  17. // seperate threads release it at the same time.
  18. //
  19. /////////////////////////////////////////////////////////////////////////////
  20. template< class T >
  21. class TRefPtr
  22. {
  23. public:
  24. TRefPtr();
  25. TRefPtr( T* pT );
  26. TRefPtr( const TRefPtr<T>& sp );
  27. ~TRefPtr();
  28. T& operator*();
  29. const T& operator*() const;
  30. T* operator->();
  31. const T* operator->() const;
  32. TRefPtr<T>& operator=(const TRefPtr<T>&);
  33. bool IsValid();
  34. T* Get(){ return m_pT; }
  35. const T* Get() const { return m_pT; }
  36. void Set( T* );
  37. bool operator==( const TRefPtr<T>& sp ) const;
  38. bool operator!=( const TRefPtr<T>& sp ) const;
  39. bool operator<( const TRefPtr<T>& sp ) const;
  40. bool operator>( const TRefPtr<T>& sp ) const;
  41. // template<class newType>
  42. // operator TRefPtr<newType>()
  43. // {
  44. // return TRefPtr<newType>(m_pT);
  45. // }
  46. protected:
  47. T* m_pT;
  48. };
  49. template< class T >
  50. TRefPtr<T>::TRefPtr<T>()
  51. : m_pT( NULL )
  52. {
  53. }
  54. template< class T >
  55. TRefPtr<T>::TRefPtr<T>(
  56. T* pT )
  57. : m_pT( pT )
  58. {
  59. if ( m_pT )
  60. {
  61. m_pT->AddRef();
  62. }
  63. }
  64. template< class T >
  65. TRefPtr<T>::TRefPtr<T>(
  66. const TRefPtr<T>& sp )
  67. : m_pT( sp.m_pT )
  68. {
  69. if ( m_pT )
  70. {
  71. m_pT->AddRef();
  72. }
  73. }
  74. template< class T >
  75. TRefPtr<T>::~TRefPtr<T>()
  76. {
  77. if ( m_pT )
  78. {
  79. m_pT->Release();
  80. }
  81. }
  82. template< class T >
  83. void
  84. TRefPtr<T>::Set(
  85. T* pT )
  86. {
  87. if ( m_pT )
  88. {
  89. m_pT->Release();
  90. }
  91. m_pT = pT;
  92. if ( m_pT )
  93. {
  94. m_pT ->AddRef();
  95. }
  96. }
  97. template< class T >
  98. T&
  99. TRefPtr<T>::operator*()
  100. {
  101. return *m_pT;
  102. }
  103. template< class T >
  104. const T&
  105. TRefPtr<T>::operator*() const
  106. {
  107. return *m_pT;
  108. }
  109. template< class T >
  110. T*
  111. TRefPtr<T>::operator->()
  112. {
  113. return m_pT;
  114. }
  115. template< class T >
  116. const T*
  117. TRefPtr<T>::operator->() const
  118. {
  119. return m_pT;
  120. }
  121. template< class T >
  122. bool
  123. TRefPtr<T>::operator==(
  124. const TRefPtr<T>& sp ) const
  125. {
  126. return ( m_pT == sp.m_pT );
  127. }
  128. template< class T >
  129. bool
  130. TRefPtr<T>::operator!=(
  131. const TRefPtr<T>& sp ) const
  132. {
  133. return ( m_pT != sp.m_pT );
  134. }
  135. template< class T >
  136. bool
  137. TRefPtr<T>::operator<(
  138. const TRefPtr<T>& sp ) const
  139. {
  140. return ( (long)m_pT < (long)sp.m_pT );
  141. }
  142. template< class T >
  143. bool
  144. TRefPtr<T>::operator>(
  145. const TRefPtr<T>& sp ) const
  146. {
  147. return ( (long)m_pT > (long)sp.m_pT );
  148. }
  149. template< class T >
  150. TRefPtr<T>&
  151. TRefPtr<T>::operator=(const TRefPtr<T>& rhs)
  152. {
  153. if ( m_pT )
  154. {
  155. m_pT->Release();
  156. }
  157. m_pT = rhs.m_pT;
  158. if ( m_pT )
  159. {
  160. m_pT->AddRef();
  161. }
  162. return *this;
  163. }
  164. template< class T >
  165. bool
  166. TRefPtr<T>::IsValid()
  167. {
  168. return ( m_pT != NULL );
  169. }
  170. // This macro helps solve the up-casting problems associated with smart pointers
  171. // If you have class B inheriting from class A. Then you can do the following
  172. // typedef TRefPtr<A> APtr;
  173. // DECLARE_REFPTR( B, A )
  174. // Now you have can safe cast a BPtr to an APtr (BPtr is derived from APtr)
  175. #define DECLARE_REFPTR( iclass, bclass ) \
  176. class iclass##Ptr : public bclass##Ptr \
  177. { \
  178. public: \
  179. iclass##Ptr() \
  180. : bclass##Ptr(){}; \
  181. iclass##Ptr( iclass * pT ) \
  182. : bclass##Ptr(pT){}; \
  183. iclass##Ptr( const iclass##Ptr & sp ) \
  184. : bclass##Ptr(sp){}; \
  185. \
  186. iclass & operator*() \
  187. { return *((iclass *)m_pT); }; \
  188. const iclass & operator*() const \
  189. { return *((const iclass *)m_pT); }; \
  190. iclass * operator->() \
  191. { return (iclass *)m_pT; }; \
  192. const iclass * operator->() const \
  193. { return (const iclass *)m_pT; }; \
  194. iclass * Get() \
  195. { return (iclass *)m_pT; }; \
  196. };
  197. #endif // !_SMARTPTR_H_