1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nsIWeakReferenceUtils_h__
8 #define nsIWeakReferenceUtils_h__
11 #include "nsIWeakReference.h"
13 typedef nsCOMPtr
<nsIWeakReference
> nsWeakPtr
;
19 // a type-safe shortcut for calling the |QueryReferent()| member function
20 // T must inherit from nsIWeakReference, but the cast may be ambiguous.
21 template <class T
, class DestinationType
>
22 inline nsresult
CallQueryReferent(T
* aSource
, DestinationType
** aDestination
) {
23 MOZ_ASSERT(aSource
, "null parameter");
24 MOZ_ASSERT(aDestination
, "null parameter");
26 return aSource
->QueryReferent(NS_GET_TEMPLATE_IID(DestinationType
),
27 reinterpret_cast<void**>(aDestination
));
30 inline const nsQueryReferent
do_QueryReferent(nsIWeakReference
* aRawPtr
,
31 nsresult
* aError
= 0) {
32 return nsQueryReferent(aRawPtr
, aError
);
36 * Deprecated, use |do_GetWeakReference| instead.
38 extern nsIWeakReference
* NS_GetWeakReference(nsISupports
*,
39 nsresult
* aResult
= 0);
40 extern nsIWeakReference
* NS_GetWeakReference(nsISupportsWeakReference
*,
41 nsresult
* aResult
= 0);
44 * |do_GetWeakReference| is a convenience function that bundles up all the work
45 * needed to get a weak reference to an arbitrary object, i.e., the
46 * |QueryInterface|, test, and call through to |GetWeakReference|, and put it
47 * into your |nsCOMPtr|. It is specifically designed to cooperate with
48 * |nsCOMPtr| (or |nsWeakPtr|) like so: |nsWeakPtr myWeakPtr =
49 * do_GetWeakReference(aPtr);|.
51 inline already_AddRefed
<nsIWeakReference
> do_GetWeakReference(
52 nsISupports
* aRawPtr
, nsresult
* aError
= 0) {
53 return dont_AddRef(NS_GetWeakReference(aRawPtr
, aError
));
56 inline already_AddRefed
<nsIWeakReference
> do_GetWeakReference(
57 nsISupportsWeakReference
* aRawPtr
, nsresult
* aError
= 0) {
58 return dont_AddRef(NS_GetWeakReference(aRawPtr
, aError
));
61 inline void do_GetWeakReference(nsIWeakReference
* aRawPtr
,
62 nsresult
* aError
= 0) {
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.
69 inline void do_GetWeakReference(already_AddRefed
<T
>&) {
70 // This signature exists solely to _stop_ you from doing the bad thing.
71 // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
72 // someone else is an automatic leak. See
73 // <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
77 inline void do_GetWeakReference(already_AddRefed
<T
>&, nsresult
*) {
78 // This signature exists solely to _stop_ you from doing the bad thing.
79 // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
80 // someone else is an automatic leak. See
81 // <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.