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
>
23 CallQueryReferent(T
* aSource
, DestinationType
** aDestination
)
25 NS_PRECONDITION(aSource
, "null parameter");
26 NS_PRECONDITION(aDestination
, "null parameter");
28 return aSource
->QueryReferent(NS_GET_TEMPLATE_IID(DestinationType
),
29 reinterpret_cast<void**>(aDestination
));
33 class nsQueryReferent
: public nsCOMPtr_helper
36 nsQueryReferent(nsIWeakReference
* aWeakPtr
, nsresult
* aError
)
42 virtual nsresult NS_FASTCALL
operator()(const nsIID
& aIID
, void**) const;
45 nsIWeakReference
* mWeakPtr
;
49 inline const nsQueryReferent
50 do_QueryReferent(nsIWeakReference
* aRawPtr
, nsresult
* aError
= 0)
52 return nsQueryReferent(aRawPtr
, aError
);
57 * Deprecated, use |do_GetWeakReference| instead.
59 extern nsIWeakReference
* NS_GetWeakReference(nsISupports
*,
60 nsresult
* aResult
= 0);
63 * |do_GetWeakReference| is a convenience function that bundles up all the work needed
64 * to get a weak reference to an arbitrary object, i.e., the |QueryInterface|, test, and
65 * call through to |GetWeakReference|, and put it into your |nsCOMPtr|.
66 * It is specifically designed to cooperate with |nsCOMPtr| (or |nsWeakPtr|) like so:
67 * |nsWeakPtr myWeakPtr = do_GetWeakReference(aPtr);|.
69 inline already_AddRefed
<nsIWeakReference
>
70 do_GetWeakReference(nsISupports
* aRawPtr
, nsresult
* aError
= 0)
72 return dont_AddRef(NS_GetWeakReference(aRawPtr
, aError
));
76 do_GetWeakReference(nsIWeakReference
* aRawPtr
, nsresult
* aError
= 0)
78 // This signature exists solely to _stop_ you from doing a bad thing.
79 // Saying |do_GetWeakReference()| on a weak reference itself,
80 // is very likely to be a programmer error.
85 do_GetWeakReference(already_AddRefed
<T
>&)
87 // This signature exists solely to _stop_ you from doing the bad thing.
88 // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
89 // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
94 do_GetWeakReference(already_AddRefed
<T
>&, nsresult
*)
96 // This signature exists solely to _stop_ you from doing the bad thing.
97 // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
98 // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.