Line data Source code
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/. */
6 :
7 : #ifndef nsIWeakReferenceUtils_h__
8 : #define nsIWeakReferenceUtils_h__
9 :
10 : #include "nsCOMPtr.h"
11 : #include "nsIWeakReference.h"
12 :
13 : typedef nsCOMPtr<nsIWeakReference> nsWeakPtr;
14 :
15 : /**
16 : *
17 : */
18 :
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 10121 : CallQueryReferent(T* aSource, DestinationType** aDestination)
24 : {
25 10121 : NS_PRECONDITION(aSource, "null parameter");
26 10121 : NS_PRECONDITION(aDestination, "null parameter");
27 :
28 : return aSource->QueryReferent(NS_GET_TEMPLATE_IID(DestinationType),
29 10121 : reinterpret_cast<void**>(aDestination));
30 : }
31 :
32 :
33 : class MOZ_STACK_CLASS nsQueryReferent final : public nsCOMPtr_helper
34 : {
35 : public:
36 6443 : nsQueryReferent(nsIWeakReference* aWeakPtr, nsresult* aError)
37 6443 : : mWeakPtr(aWeakPtr)
38 6443 : , mErrorPtr(aError)
39 : {
40 6443 : }
41 :
42 : virtual nsresult NS_FASTCALL operator()(const nsIID& aIID, void**) const
43 : override;
44 :
45 : private:
46 : nsIWeakReference* MOZ_NON_OWNING_REF mWeakPtr;
47 : nsresult* mErrorPtr;
48 : };
49 :
50 : inline const nsQueryReferent
51 6443 : do_QueryReferent(nsIWeakReference* aRawPtr, nsresult* aError = 0)
52 : {
53 6443 : return nsQueryReferent(aRawPtr, aError);
54 : }
55 :
56 :
57 : /**
58 : * Deprecated, use |do_GetWeakReference| instead.
59 : */
60 : extern nsIWeakReference* NS_GetWeakReference(nsISupports*,
61 : nsresult* aResult = 0);
62 :
63 : /**
64 : * |do_GetWeakReference| is a convenience function that bundles up all the work needed
65 : * to get a weak reference to an arbitrary object, i.e., the |QueryInterface|, test, and
66 : * call through to |GetWeakReference|, and put it into your |nsCOMPtr|.
67 : * It is specifically designed to cooperate with |nsCOMPtr| (or |nsWeakPtr|) like so:
68 : * |nsWeakPtr myWeakPtr = do_GetWeakReference(aPtr);|.
69 : */
70 : inline already_AddRefed<nsIWeakReference>
71 2230 : do_GetWeakReference(nsISupports* aRawPtr, nsresult* aError = 0)
72 : {
73 2230 : return dont_AddRef(NS_GetWeakReference(aRawPtr, aError));
74 : }
75 :
76 : inline void
77 : do_GetWeakReference(nsIWeakReference* aRawPtr, nsresult* aError = 0)
78 : {
79 : // This signature exists solely to _stop_ you from doing a bad thing.
80 : // Saying |do_GetWeakReference()| on a weak reference itself,
81 : // is very likely to be a programmer error.
82 : }
83 :
84 : template<class T>
85 : inline void
86 : do_GetWeakReference(already_AddRefed<T>&)
87 : {
88 : // This signature exists solely to _stop_ you from doing the bad thing.
89 : // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
90 : // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
91 : }
92 :
93 : template<class T>
94 : inline void
95 : do_GetWeakReference(already_AddRefed<T>&, nsresult*)
96 : {
97 : // This signature exists solely to _stop_ you from doing the bad thing.
98 : // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
99 : // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
100 : }
101 :
102 : #endif
|