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