Bumping manifests a=b2g-bump
[gecko.git] / xpcom / glue / nsIWeakReferenceUtils.h
blob684a7af40cb1e37325478baba0c3f89769e43b3e
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__
10 #include "nsCOMPtr.h"
11 #include "nsIWeakReference.h"
13 typedef nsCOMPtr<nsIWeakReference> nsWeakPtr;
15 /**
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
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
35 public:
36 nsQueryReferent(nsIWeakReference* aWeakPtr, nsresult* aError)
37 : mWeakPtr(aWeakPtr)
38 , mErrorPtr(aError)
42 virtual nsresult NS_FASTCALL operator()(const nsIID& aIID, void**) const;
44 private:
45 nsIWeakReference* mWeakPtr;
46 nsresult* mErrorPtr;
49 inline const nsQueryReferent
50 do_QueryReferent(nsIWeakReference* aRawPtr, nsresult* aError = 0)
52 return nsQueryReferent(aRawPtr, aError);
56 /**
57 * Deprecated, use |do_GetWeakReference| instead.
59 extern nsIWeakReference* NS_GetWeakReference(nsISupports*,
60 nsresult* aResult = 0);
62 /**
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));
75 inline void
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.
83 template<class T>
84 inline void
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>.
92 template<class T>
93 inline void
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>.
101 #endif