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.

185 lines
3.3 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1997
  6. //
  7. // File: refcnt.h
  8. //
  9. //--------------------------------------------------------------------------
  10. //
  11. // refcnt.h: base class for reference-counted objects
  12. //
  13. #ifndef _REFCNT_H_
  14. #define _REFCNT_H_
  15. class REFCNT
  16. {
  17. public:
  18. // Bind the object again
  19. void Bind () { IncRef(1) ; }
  20. // Release the object
  21. void Unbind () { IncRef(-1) ; }
  22. // Return the reference count
  23. UINT CRef() const { return _cref; }
  24. protected:
  25. REFCNT() : _cref(0) {}
  26. // Virtual call-out when reference count goes to zero
  27. virtual void NoRef () {}
  28. private:
  29. UINT _cref; // Number of references to this
  30. protected:
  31. void IncRef ( int i = 1 )
  32. {
  33. if ((_cref += i) > 0 )
  34. return;
  35. _cref = 0;
  36. NoRef();
  37. }
  38. // Hide the assignment operator
  39. HIDE_AS(REFCNT);
  40. };
  41. ////////////////////////////////////////////////////////////////////
  42. // template REFPOBJ: Smart pointer wrapper template. Knows
  43. // to destroy the pointed object when it itself is destroyed.
  44. ////////////////////////////////////////////////////////////////////
  45. class ZSREF;
  46. template<class T>
  47. class REFPOBJ
  48. {
  49. // Friendship is required for manipulation by the symbol table
  50. friend pair<ZSREF, REFPOBJ<T> >;
  51. friend map<ZSREF, REFPOBJ<T>, less<ZSREF> >;
  52. public:
  53. ~ REFPOBJ ()
  54. { Deref(); }
  55. // Return the real object
  56. T * Pobj () const
  57. { return _pobj ; }
  58. // Allow a REFPOBJ to be used wherever a T * is required
  59. operator T * () const
  60. { return _pobj ; }
  61. // Operator == compares only pointers.
  62. bool operator == ( const REFPOBJ & pobj ) const
  63. { return _pobj == pobj._pobj; }
  64. T * MoveTo (REFPOBJ & pobj)
  65. {
  66. pobj = Pobj();
  67. _pobj = NULL;
  68. return pobj;
  69. };
  70. REFPOBJ & operator = ( T * pobj )
  71. {
  72. Deref();
  73. _pobj = pobj;
  74. return *this;
  75. }
  76. protected:
  77. REFPOBJ ()
  78. : _pobj(NULL)
  79. {}
  80. protected:
  81. T * _pobj;
  82. private:
  83. void Deref ()
  84. {
  85. delete _pobj;
  86. _pobj = NULL;
  87. }
  88. HIDE_AS(REFPOBJ);
  89. };
  90. ////////////////////////////////////////////////////////////////////
  91. // template REFCWRAP: Smart pointer wrapper template for objects
  92. // using REFCNT semantics.
  93. ////////////////////////////////////////////////////////////////////
  94. template<class T>
  95. class REFCWRAP
  96. {
  97. public:
  98. REFCWRAP (T * pobj = NULL)
  99. : _pobj(NULL)
  100. {
  101. Ref( pobj );
  102. }
  103. ~ REFCWRAP ()
  104. {
  105. Deref();
  106. }
  107. REFCWRAP ( const REFCWRAP & refp )
  108. : _pobj(NULL)
  109. {
  110. Ref( refp._pobj );
  111. }
  112. // Return true if there's a referenced object
  113. bool BRef () const
  114. { return _pobj != NULL; }
  115. // Return the real object
  116. T * Pobj () const
  117. { return _pobj ; }
  118. // Allow a REFPOBJ to be used wherever a T * is required
  119. operator T * () const
  120. { return _pobj ; }
  121. // Operator == compares only pointers.
  122. bool operator == ( const REFCWRAP & pobj ) const
  123. { return _pobj == pobj._pobj; }
  124. T * operator -> () const
  125. {
  126. assert( _pobj );
  127. return _pobj;
  128. }
  129. REFCWRAP & operator = ( T * pobj )
  130. {
  131. Ref(pobj);
  132. return *this;
  133. }
  134. REFCWRAP & operator = ( const REFCWRAP & refp )
  135. {
  136. Ref(refp._pobj);
  137. return *this;
  138. }
  139. void Deref ()
  140. {
  141. if ( _pobj )
  142. {
  143. _pobj->Unbind();
  144. _pobj = NULL;
  145. }
  146. }
  147. protected:
  148. T * _pobj;
  149. private:
  150. void Ref ( T * pobj )
  151. {
  152. Deref();
  153. if ( pobj )
  154. {
  155. pobj->Bind();
  156. _pobj = pobj;
  157. }
  158. }
  159. };
  160. #endif