nsWeakReference.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. *
  3. * ***** BEGIN LICENSE BLOCK *****
  4. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5. *
  6. * The contents of this file are subject to the Mozilla Public License Version
  7. * 1.1 (the "License"); you may not use this file except in compliance with
  8. * the License. You may obtain a copy of the License at
  9. * http://www.mozilla.org/MPL/
  10. *
  11. * Software distributed under the License is distributed on an "AS IS" basis,
  12. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13. * for the specific language governing rights and limitations under the
  14. * License.
  15. *
  16. * The Original Code is the Mozilla browser.
  17. *
  18. * The Initial Developer of the Original Code is
  19. * Netscape Communications, Inc.
  20. * Portions created by the Initial Developer are Copyright (C) 1999
  21. * the Initial Developer. All Rights Reserved.
  22. *
  23. * Contributor(s):
  24. * Scott Collins <[email protected]>
  25. *
  26. * Alternatively, the contents of this file may be used under the terms of
  27. * either of the GNU General Public License Version 2 or later (the "GPL"),
  28. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29. * in which case the provisions of the GPL or the LGPL are applicable instead
  30. * of those above. If you wish to allow use of your version of this file only
  31. * under the terms of either the GPL or the LGPL, and not to allow others to
  32. * use your version of this file under the terms of the MPL, indicate your
  33. * decision by deleting the provisions above and replace them with the notice
  34. * and other provisions required by the GPL or the LGPL. If you do not delete
  35. * the provisions above, a recipient may use your version of this file under
  36. * the terms of any one of the MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK ***** */
  39. #ifndef nsWeakReference_h__
  40. #define nsWeakReference_h__
  41. // nsWeakReference.h
  42. #include "nsIWeakReference.h"
  43. #include "nsIWeakReferenceUtils.h"
  44. class nsWeakReference;
  45. #undef IMETHOD_VISIBILITY
  46. #define IMETHOD_VISIBILITY NS_VISIBILITY_DEFAULT
  47. class NS_COM_GLUE nsSupportsWeakReference : public nsISupportsWeakReference
  48. {
  49. public:
  50. nsSupportsWeakReference()
  51. : mProxy(0)
  52. {
  53. // nothing else to do here
  54. }
  55. NS_DECL_NSISUPPORTSWEAKREFERENCE
  56. protected:
  57. inline ~nsSupportsWeakReference();
  58. private:
  59. friend class nsWeakReference;
  60. void
  61. NoticeProxyDestruction()
  62. // ...called (only) by an |nsWeakReference| from _its_ dtor.
  63. {
  64. mProxy = 0;
  65. }
  66. nsWeakReference* mProxy;
  67. protected:
  68. inline void ClearWeakReferences();
  69. PRBool HasWeakReferences() const {return mProxy != 0;}
  70. };
  71. #undef IMETHOD_VISIBILITY
  72. #define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
  73. class NS_COM_GLUE nsWeakReference : public nsIWeakReference
  74. {
  75. public:
  76. // nsISupports...
  77. NS_DECL_ISUPPORTS
  78. // nsIWeakReference...
  79. NS_DECL_NSIWEAKREFERENCE
  80. private:
  81. friend class nsSupportsWeakReference;
  82. nsWeakReference( nsSupportsWeakReference* referent )
  83. : mReferent(referent)
  84. // ...I can only be constructed by an |nsSupportsWeakReference|
  85. {
  86. // nothing else to do here
  87. }
  88. ~nsWeakReference()
  89. // ...I will only be destroyed by calling |delete| myself.
  90. {
  91. if ( mReferent )
  92. mReferent->NoticeProxyDestruction();
  93. }
  94. void
  95. NoticeReferentDestruction()
  96. // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
  97. {
  98. mReferent = 0;
  99. }
  100. nsSupportsWeakReference* mReferent;
  101. };
  102. inline
  103. void
  104. nsSupportsWeakReference::ClearWeakReferences()
  105. /*
  106. Usually being called from |nsSupportsWeakReference::~nsSupportsWeakReference|
  107. will be good enough, but you may have a case where you need to call disconnect
  108. your weak references in an outer destructor (to prevent some client holding a
  109. weak reference from re-entering your destructor).
  110. */
  111. {
  112. if ( mProxy )
  113. {
  114. mProxy->NoticeReferentDestruction();
  115. mProxy = 0;
  116. }
  117. }
  118. inline
  119. nsSupportsWeakReference::~nsSupportsWeakReference()
  120. {
  121. ClearWeakReferences();
  122. }
  123. #endif