1 | /* -*- Mode: IDL; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
---|
2 | /* ***** BEGIN LICENSE BLOCK *****
|
---|
3 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
---|
4 | *
|
---|
5 | * The contents of this file are subject to the Mozilla Public License Version
|
---|
6 | * 1.1 (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | * http://www.mozilla.org/MPL/
|
---|
9 | *
|
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis,
|
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
---|
12 | * for the specific language governing rights and limitations under the
|
---|
13 | * License.
|
---|
14 | *
|
---|
15 | * The Original Code is mozilla.org code.
|
---|
16 | *
|
---|
17 | * The Initial Developer of the Original Code is
|
---|
18 | * Netscape Communications Corporation.
|
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1998
|
---|
20 | * the Initial Developer. All Rights Reserved.
|
---|
21 | *
|
---|
22 | * Contributor(s):
|
---|
23 | * Scott Collins <[email protected]> (Original Author)
|
---|
24 | *
|
---|
25 | * Alternatively, the contents of this file may be used under the terms of
|
---|
26 | * either of the GNU General Public License Version 2 or later (the "GPL"),
|
---|
27 | * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
---|
28 | * in which case the provisions of the GPL or the LGPL are applicable instead
|
---|
29 | * of those above. If you wish to allow use of your version of this file only
|
---|
30 | * under the terms of either the GPL or the LGPL, and not to allow others to
|
---|
31 | * use your version of this file under the terms of the MPL, indicate your
|
---|
32 | * decision by deleting the provisions above and replace them with the notice
|
---|
33 | * and other provisions required by the GPL or the LGPL. If you do not delete
|
---|
34 | * the provisions above, a recipient may use your version of this file under
|
---|
35 | * the terms of any one of the MPL, the GPL or the LGPL.
|
---|
36 | *
|
---|
37 | * ***** END LICENSE BLOCK ***** */
|
---|
38 |
|
---|
39 | #ifndef nsIWeakReferenceUtils_h__
|
---|
40 | #define nsIWeakReferenceUtils_h__
|
---|
41 |
|
---|
42 | #ifndef nsCOMPtr_h__
|
---|
43 | #include "nsCOMPtr.h"
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | typedef nsCOMPtr<nsIWeakReference> nsWeakPtr;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | *
|
---|
50 | */
|
---|
51 |
|
---|
52 | // a type-safe shortcut for calling the |QueryReferent()| member function
|
---|
53 | // T must inherit from nsIWeakReference, but the cast may be ambiguous.
|
---|
54 | template <class T, class DestinationType>
|
---|
55 | inline
|
---|
56 | nsresult
|
---|
57 | CallQueryReferent( T* aSource, DestinationType** aDestination )
|
---|
58 | {
|
---|
59 | NS_PRECONDITION(aSource, "null parameter");
|
---|
60 | NS_PRECONDITION(aDestination, "null parameter");
|
---|
61 |
|
---|
62 | return aSource->QueryReferent(NS_GET_IID(DestinationType),
|
---|
63 | NS_REINTERPRET_CAST(void**, aDestination));
|
---|
64 | }
|
---|
65 |
|
---|
66 |
|
---|
67 | class NS_COM nsQueryReferent : public nsCOMPtr_helper
|
---|
68 | {
|
---|
69 | public:
|
---|
70 | nsQueryReferent( nsIWeakReference* aWeakPtr, nsresult* error )
|
---|
71 | : mWeakPtr(aWeakPtr),
|
---|
72 | mErrorPtr(error)
|
---|
73 | {
|
---|
74 | // nothing else to do here
|
---|
75 | }
|
---|
76 |
|
---|
77 | virtual nsresult NS_FASTCALL operator()( const nsIID& aIID, void** ) const;
|
---|
78 |
|
---|
79 | private:
|
---|
80 | nsIWeakReference* mWeakPtr;
|
---|
81 | nsresult* mErrorPtr;
|
---|
82 | };
|
---|
83 |
|
---|
84 | inline
|
---|
85 | const nsQueryReferent
|
---|
86 | do_QueryReferent( nsIWeakReference* aRawPtr, nsresult* error = 0 )
|
---|
87 | {
|
---|
88 | return nsQueryReferent(aRawPtr, error);
|
---|
89 | }
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 | class NS_COM nsGetWeakReference : public nsCOMPtr_helper
|
---|
94 | {
|
---|
95 | public:
|
---|
96 | nsGetWeakReference( nsISupports* aRawPtr, nsresult* error )
|
---|
97 | : mRawPtr(aRawPtr),
|
---|
98 | mErrorPtr(error)
|
---|
99 | {
|
---|
100 | // nothing else to do here
|
---|
101 | }
|
---|
102 |
|
---|
103 | virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
|
---|
104 |
|
---|
105 | private:
|
---|
106 | nsISupports* mRawPtr;
|
---|
107 | nsresult* mErrorPtr;
|
---|
108 | };
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * |do_GetWeakReference| is a convenience function that bundles up all the work needed
|
---|
112 | * to get a weak reference to an arbitrary object, i.e., the |QueryInterface|, test, and
|
---|
113 | * call through to |GetWeakReference|, and put it into your |nsCOMPtr|.
|
---|
114 | * It is specifically designed to cooperate with |nsCOMPtr| (or |nsWeakPtr|) like so:
|
---|
115 | * |nsWeakPtr myWeakPtr = do_GetWeakReference(aPtr);|.
|
---|
116 | */
|
---|
117 | inline
|
---|
118 | const nsGetWeakReference
|
---|
119 | do_GetWeakReference( nsISupports* aRawPtr, nsresult* error = 0 )
|
---|
120 | {
|
---|
121 | return nsGetWeakReference(aRawPtr, error);
|
---|
122 | }
|
---|
123 |
|
---|
124 | inline
|
---|
125 | void
|
---|
126 | do_GetWeakReference( nsIWeakReference* aRawPtr, nsresult* error = 0 )
|
---|
127 | {
|
---|
128 | // This signature exists soley to _stop_ you from doing a bad thing.
|
---|
129 | // Saying |do_GetWeakReference()| on a weak reference itself,
|
---|
130 | // is very likely to be a programmer error.
|
---|
131 | }
|
---|
132 |
|
---|
133 | template <class T>
|
---|
134 | inline
|
---|
135 | void
|
---|
136 | do_GetWeakReference( already_AddRefed<T>& )
|
---|
137 | {
|
---|
138 | // This signature exists soley to _stop_ you from doing the bad thing.
|
---|
139 | // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
|
---|
140 | // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
|
---|
141 | }
|
---|
142 |
|
---|
143 | template <class T>
|
---|
144 | inline
|
---|
145 | void
|
---|
146 | do_GetWeakReference( already_AddRefed<T>&, nsresult* )
|
---|
147 | {
|
---|
148 | // This signature exists soley to _stop_ you from doing the bad thing.
|
---|
149 | // Saying |do_GetWeakReference()| on a pointer that is not otherwise owned by
|
---|
150 | // someone else is an automatic leak. See <http://bugzilla.mozilla.org/show_bug.cgi?id=8221>.
|
---|
151 | }
|
---|
152 |
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Deprecated, use |do_GetWeakReference| instead.
|
---|
157 | */
|
---|
158 | extern NS_COM
|
---|
159 | nsIWeakReference*
|
---|
160 | NS_GetWeakReference( nsISupports* , nsresult* aResult=0 );
|
---|
161 |
|
---|
162 | #endif
|
---|