nsIWeakReferenceUtils.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is mozilla.org code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Scott Collins <[email protected]> (Original Author)
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either of the GNU General Public License Version 2 or later (the "GPL"),
  27. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. #ifndef nsIWeakReferenceUtils_h__
  39. #define nsIWeakReferenceUtils_h__
  40. #ifndef nsCOMPtr_h__
  41. #include "nsCOMPtr.h"
  42. #endif
  43. typedef nsCOMPtr<nsIWeakReference> nsWeakPtr;
  44. /**
  45. *
  46. */
  47. // a type-safe shortcut for calling the |QueryReferent()| member function
  48. // T must inherit from nsIWeakReference, but the cast may be ambiguous.
  49. template <class T, class DestinationType>
  50. inline
  51. nsresult
  52. CallQueryReferent( T* aSource, DestinationType** aDestination )
  53. {
  54. NS_PRECONDITION(aSource, "null parameter");
  55. NS_PRECONDITION(aDestination, "null parameter");
  56. return aSource->QueryReferent(NS_GET_IID(DestinationType),
  57. NS_REINTERPRET_CAST(void**, aDestination));
  58. }
  59. class NS_COM_GLUE nsQueryReferent : public nsCOMPtr_helper
  60. {
  61. public:
  62. nsQueryReferent( nsIWeakReference* aWeakPtr, nsresult* error )
  63. : mWeakPtr(aWeakPtr),
  64. mErrorPtr(error)
  65. {
  66. // nothing else to do here
  67. }
  68. virtual nsresult NS_FASTCALL operator()( const nsIID& aIID, void** ) const;
  69. private:
  70. nsIWeakReference* mWeakPtr;
  71. nsresult* mErrorPtr;
  72. };
  73. inline
  74. const nsQueryReferent
  75. do_QueryReferent( nsIWeakReference* aRawPtr, nsresult* error = 0 )
  76. {
  77. return nsQueryReferent(aRawPtr, error);
  78. }
  79. /**
  80. * Deprecated, use |do_GetWeakReference| instead.
  81. */
  82. extern NS_COM_GLUE
  83. nsIWeakReference*
  84. NS_GetWeakReference( nsISupports* , nsresult* aResult=0 );
  85. /**
  86. * |do_GetWeakReference| is a convenience function that bundles up all the work needed
  87. * to get a weak reference to an arbitrary object, i.e., the |QueryInterface|, test, and
  88. * call through to |GetWeakReference|, and put it into your |nsCOMPtr|.
  89. * It is specifically designed to cooperate with |nsCOMPtr| (or |nsWeakPtr|) like so:
  90. * |nsWeakPtr myWeakPtr = do_GetWeakReference(aPtr);|.
  91. */
  92. inline
  93. already_AddRefed<nsIWeakReference>
  94. do_GetWeakReference( nsISupports* aRawPtr, nsresult* error = 0 )
  95. {
  96. return NS_GetWeakReference(aRawPtr, error);
  97. }
  98. inline
  99. void
  100. do_GetWeakReference( nsIWeakReference* aRawPtr, nsresult* error = 0 )
  101. {
  102. // This signature exists soley to _stop_ you from doing a bad thing.
  103. // Saying |do_GetWeakReference()| on a weak reference itself,
  104. // is very likely to be a programmer error.
  105. }
  106. template <class T>
  107. inline
  108. void
  109. do_GetWeakReference( already_AddRefed<T>& )
  110. {
  111. // This signature exists soley to _stop_ you from doing the bad thing.
  112. // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
  113. // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
  114. }
  115. template <class T>
  116. inline
  117. void
  118. do_GetWeakReference( already_AddRefed<T>&, nsresult* )
  119. {
  120. // This signature exists soley to _stop_ you from doing the bad thing.
  121. // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
  122. // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
  123. }
  124. #endif