nsIWeakReferenceUtils.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsIWeakReferenceUtils_h__
  6. #define nsIWeakReferenceUtils_h__
  7. #include "nsCOMPtr.h"
  8. #include "nsIWeakReference.h"
  9. typedef nsCOMPtr<nsIWeakReference> nsWeakPtr;
  10. /**
  11. *
  12. */
  13. // a type-safe shortcut for calling the |QueryReferent()| member function
  14. // T must inherit from nsIWeakReference, but the cast may be ambiguous.
  15. template<class T, class DestinationType>
  16. inline nsresult
  17. CallQueryReferent(T* aSource, DestinationType** aDestination)
  18. {
  19. NS_PRECONDITION(aSource, "null parameter");
  20. NS_PRECONDITION(aDestination, "null parameter");
  21. return aSource->QueryReferent(NS_GET_TEMPLATE_IID(DestinationType),
  22. reinterpret_cast<void**>(aDestination));
  23. }
  24. class MOZ_STACK_CLASS nsQueryReferent final : public nsCOMPtr_helper
  25. {
  26. public:
  27. nsQueryReferent(nsIWeakReference* aWeakPtr, nsresult* aError)
  28. : mWeakPtr(aWeakPtr)
  29. , mErrorPtr(aError)
  30. {
  31. }
  32. virtual nsresult NS_FASTCALL operator()(const nsIID& aIID, void**) const
  33. override;
  34. private:
  35. nsIWeakReference* MOZ_NON_OWNING_REF mWeakPtr;
  36. nsresult* mErrorPtr;
  37. };
  38. inline const nsQueryReferent
  39. do_QueryReferent(nsIWeakReference* aRawPtr, nsresult* aError = 0)
  40. {
  41. return nsQueryReferent(aRawPtr, aError);
  42. }
  43. /**
  44. * Deprecated, use |do_GetWeakReference| instead.
  45. */
  46. extern nsIWeakReference* NS_GetWeakReference(nsISupports*,
  47. nsresult* aResult = 0);
  48. /**
  49. * |do_GetWeakReference| is a convenience function that bundles up all the work needed
  50. * to get a weak reference to an arbitrary object, i.e., the |QueryInterface|, test, and
  51. * call through to |GetWeakReference|, and put it into your |nsCOMPtr|.
  52. * It is specifically designed to cooperate with |nsCOMPtr| (or |nsWeakPtr|) like so:
  53. * |nsWeakPtr myWeakPtr = do_GetWeakReference(aPtr);|.
  54. */
  55. inline already_AddRefed<nsIWeakReference>
  56. do_GetWeakReference(nsISupports* aRawPtr, nsresult* aError = 0)
  57. {
  58. return dont_AddRef(NS_GetWeakReference(aRawPtr, aError));
  59. }
  60. inline void
  61. do_GetWeakReference(nsIWeakReference* aRawPtr, nsresult* aError = 0)
  62. {
  63. // This signature exists solely to _stop_ you from doing a bad thing.
  64. // Saying |do_GetWeakReference()| on a weak reference itself,
  65. // is very likely to be a programmer error.
  66. }
  67. template<class T>
  68. inline void
  69. do_GetWeakReference(already_AddRefed<T>&)
  70. {
  71. // This signature exists solely to _stop_ you from doing the bad thing.
  72. // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
  73. // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
  74. }
  75. template<class T>
  76. inline void
  77. do_GetWeakReference(already_AddRefed<T>&, nsresult*)
  78. {
  79. // This signature exists solely to _stop_ you from doing the bad thing.
  80. // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
  81. // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
  82. }
  83. #endif