1 : /* -*- Mode: C++; 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 : *
24 : * Alternatively, the contents of this file may be used under the terms of
25 : * either of the GNU General Public License Version 2 or later (the "GPL"),
26 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 : * in which case the provisions of the GPL or the LGPL are applicable instead
28 : * of those above. If you wish to allow use of your version of this file only
29 : * under the terms of either the GPL or the LGPL, and not to allow others to
30 : * use your version of this file under the terms of the MPL, indicate your
31 : * decision by deleting the provisions above and replace them with the notice
32 : * and other provisions required by the GPL or the LGPL. If you do not delete
33 : * the provisions above, a recipient may use your version of this file under
34 : * the terms of any one of the MPL, the GPL or the LGPL.
35 : *
36 : * ***** END LICENSE BLOCK ***** */
37 :
38 : #ifndef nsComponentManagerUtils_h__
39 : #define nsComponentManagerUtils_h__
40 :
41 : #ifndef nscore_h__
42 : #include "nscore.h"
43 : #endif
44 :
45 : #ifndef nsCOMPtr_h__
46 : #include "nsCOMPtr.h"
47 : #endif
48 :
49 : #include "nsIFactory.h"
50 :
51 :
52 : NS_COM_GLUE nsresult
53 : CallCreateInstance
54 : (const nsCID &aClass, nsISupports *aDelegate, const nsIID &aIID,
55 : void **aResult);
56 :
57 : NS_COM_GLUE nsresult
58 : CallCreateInstance
59 : (const char *aContractID, nsISupports *aDelegate, const nsIID &aIID,
60 : void **aResult);
61 :
62 : NS_COM_GLUE nsresult
63 : CallGetClassObject
64 : (const nsCID &aClass, const nsIID &aIID, void **aResult);
65 :
66 : NS_COM_GLUE nsresult
67 : CallGetClassObject
68 : (const char *aContractID, const nsIID &aIID, void **aResult);
69 :
70 :
71 : class NS_COM_GLUE nsCreateInstanceByCID : public nsCOMPtr_helper
72 : {
73 : public:
74 11085 : nsCreateInstanceByCID( const nsCID& aCID, nsISupports* aOuter, nsresult* aErrorPtr )
75 : : mCID(aCID),
76 : mOuter(aOuter),
77 11085 : mErrorPtr(aErrorPtr)
78 : {
79 : // nothing else to do here
80 11085 : }
81 :
82 : virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
83 :
84 : private:
85 : const nsCID& mCID;
86 : nsISupports* mOuter;
87 : nsresult* mErrorPtr;
88 : };
89 :
90 : class NS_COM_GLUE nsCreateInstanceByContractID : public nsCOMPtr_helper
91 : {
92 : public:
93 389089 : nsCreateInstanceByContractID( const char* aContractID, nsISupports* aOuter, nsresult* aErrorPtr )
94 : : mContractID(aContractID),
95 : mOuter(aOuter),
96 389089 : mErrorPtr(aErrorPtr)
97 : {
98 : // nothing else to do here
99 389089 : }
100 :
101 : virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
102 :
103 : private:
104 : const char* mContractID;
105 : nsISupports* mOuter;
106 : nsresult* mErrorPtr;
107 : };
108 :
109 : class NS_COM_GLUE nsCreateInstanceFromFactory : public nsCOMPtr_helper
110 : {
111 : public:
112 0 : nsCreateInstanceFromFactory( nsIFactory* aFactory, nsISupports* aOuter, nsresult* aErrorPtr )
113 : : mFactory(aFactory),
114 : mOuter(aOuter),
115 0 : mErrorPtr(aErrorPtr)
116 : {
117 : // nothing else to do here
118 0 : }
119 :
120 : virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
121 :
122 : private:
123 : nsIFactory* mFactory;
124 : nsISupports* mOuter;
125 : nsresult* mErrorPtr;
126 : };
127 :
128 :
129 : inline
130 : const nsCreateInstanceByCID
131 11085 : do_CreateInstance( const nsCID& aCID, nsresult* error = 0 )
132 : {
133 11085 : return nsCreateInstanceByCID(aCID, 0, error);
134 : }
135 :
136 : inline
137 : const nsCreateInstanceByCID
138 : do_CreateInstance( const nsCID& aCID, nsISupports* aOuter, nsresult* error = 0 )
139 : {
140 : return nsCreateInstanceByCID(aCID, aOuter, error);
141 : }
142 :
143 : inline
144 : const nsCreateInstanceByContractID
145 389087 : do_CreateInstance( const char* aContractID, nsresult* error = 0 )
146 : {
147 389087 : return nsCreateInstanceByContractID(aContractID, 0, error);
148 : }
149 :
150 : inline
151 : const nsCreateInstanceByContractID
152 2 : do_CreateInstance( const char* aContractID, nsISupports* aOuter, nsresult* error = 0 )
153 : {
154 2 : return nsCreateInstanceByContractID(aContractID, aOuter, error);
155 : }
156 :
157 : inline
158 : const nsCreateInstanceFromFactory
159 0 : do_CreateInstance( nsIFactory* aFactory, nsresult* error = 0 )
160 : {
161 0 : return nsCreateInstanceFromFactory(aFactory, 0, error);
162 : }
163 :
164 : inline
165 : const nsCreateInstanceFromFactory
166 : do_CreateInstance( nsIFactory* aFactory, nsISupports* aOuter, nsresult* error = 0 )
167 : {
168 : return nsCreateInstanceFromFactory(aFactory, aOuter, error);
169 : }
170 :
171 :
172 : class NS_COM_GLUE nsGetClassObjectByCID : public nsCOMPtr_helper
173 : {
174 : public:
175 1405 : nsGetClassObjectByCID( const nsCID& aCID, nsresult* aErrorPtr )
176 : : mCID(aCID),
177 1405 : mErrorPtr(aErrorPtr)
178 : {
179 : // nothing else to do here
180 1405 : }
181 :
182 : virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
183 :
184 : private:
185 : const nsCID& mCID;
186 : nsresult* mErrorPtr;
187 : };
188 :
189 : class NS_COM_GLUE nsGetClassObjectByContractID : public nsCOMPtr_helper
190 : {
191 : public:
192 14623 : nsGetClassObjectByContractID( const char* aContractID, nsresult* aErrorPtr )
193 : : mContractID(aContractID),
194 14623 : mErrorPtr(aErrorPtr)
195 : {
196 : // nothing else to do here
197 14623 : }
198 :
199 : virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
200 :
201 : private:
202 : const char* mContractID;
203 : nsresult* mErrorPtr;
204 : };
205 :
206 : /**
207 : * do_GetClassObject can be used to improve performance of callers
208 : * that call |CreateInstance| many times. They can cache the factory
209 : * and call do_CreateInstance or CallCreateInstance with the cached
210 : * factory rather than having the component manager retrieve it every
211 : * time.
212 : */
213 : inline const nsGetClassObjectByCID
214 1405 : do_GetClassObject( const nsCID& aCID, nsresult* error = 0 )
215 : {
216 1405 : return nsGetClassObjectByCID(aCID, error);
217 : }
218 :
219 : inline const nsGetClassObjectByContractID
220 14623 : do_GetClassObject( const char* aContractID, nsresult* error = 0 )
221 : {
222 14623 : return nsGetClassObjectByContractID(aContractID, error);
223 : }
224 :
225 : // type-safe shortcuts for calling |CreateInstance|
226 : template <class DestinationType>
227 : inline
228 : nsresult
229 : CallCreateInstance( const nsCID &aClass,
230 : nsISupports *aDelegate,
231 : DestinationType** aDestination )
232 : {
233 : NS_PRECONDITION(aDestination, "null parameter");
234 :
235 : return CallCreateInstance(aClass, aDelegate,
236 : NS_GET_TEMPLATE_IID(DestinationType),
237 : reinterpret_cast<void**>(aDestination));
238 : }
239 :
240 : template <class DestinationType>
241 : inline
242 : nsresult
243 19 : CallCreateInstance( const nsCID &aClass,
244 : DestinationType** aDestination )
245 : {
246 19 : NS_PRECONDITION(aDestination, "null parameter");
247 :
248 : return CallCreateInstance(aClass, nsnull,
249 : NS_GET_TEMPLATE_IID(DestinationType),
250 19 : reinterpret_cast<void**>(aDestination));
251 : }
252 :
253 : template <class DestinationType>
254 : inline
255 : nsresult
256 : CallCreateInstance( const char *aContractID,
257 : nsISupports *aDelegate,
258 : DestinationType** aDestination )
259 : {
260 : NS_PRECONDITION(aContractID, "null parameter");
261 : NS_PRECONDITION(aDestination, "null parameter");
262 :
263 : return CallCreateInstance(aContractID,
264 : aDelegate,
265 : NS_GET_TEMPLATE_IID(DestinationType),
266 : reinterpret_cast<void**>(aDestination));
267 : }
268 :
269 : template <class DestinationType>
270 : inline
271 : nsresult
272 5563 : CallCreateInstance( const char *aContractID,
273 : DestinationType** aDestination )
274 : {
275 5563 : NS_PRECONDITION(aContractID, "null parameter");
276 5563 : NS_PRECONDITION(aDestination, "null parameter");
277 :
278 : return CallCreateInstance(aContractID, nsnull,
279 : NS_GET_TEMPLATE_IID(DestinationType),
280 5563 : reinterpret_cast<void**>(aDestination));
281 : }
282 :
283 : template <class DestinationType>
284 : inline
285 : nsresult
286 : CallCreateInstance( nsIFactory *aFactory,
287 : nsISupports *aDelegate,
288 : DestinationType** aDestination )
289 : {
290 : NS_PRECONDITION(aFactory, "null parameter");
291 : NS_PRECONDITION(aDestination, "null parameter");
292 :
293 : return aFactory->CreateInstance(aDelegate,
294 : NS_GET_TEMPLATE_IID(DestinationType),
295 : reinterpret_cast<void**>(aDestination));
296 : }
297 :
298 : template <class DestinationType>
299 : inline
300 : nsresult
301 : CallCreateInstance( nsIFactory *aFactory,
302 : DestinationType** aDestination )
303 : {
304 : NS_PRECONDITION(aFactory, "null parameter");
305 : NS_PRECONDITION(aDestination, "null parameter");
306 :
307 : return aFactory->CreateInstance(nsnull,
308 : NS_GET_TEMPLATE_IID(DestinationType),
309 : reinterpret_cast<void**>(aDestination));
310 : }
311 :
312 : template <class DestinationType>
313 : inline
314 : nsresult
315 : CallGetClassObject( const nsCID &aClass,
316 : DestinationType** aDestination )
317 : {
318 : NS_PRECONDITION(aDestination, "null parameter");
319 :
320 : return CallGetClassObject(aClass,
321 : NS_GET_TEMPLATE_IID(DestinationType), reinterpret_cast<void**>(aDestination));
322 : }
323 :
324 : template <class DestinationType>
325 : inline
326 : nsresult
327 0 : CallGetClassObject( const char* aContractID,
328 : DestinationType** aDestination )
329 : {
330 0 : NS_PRECONDITION(aDestination, "null parameter");
331 :
332 : return CallGetClassObject(aContractID,
333 0 : NS_GET_TEMPLATE_IID(DestinationType), reinterpret_cast<void**>(aDestination));
334 : }
335 :
336 : #endif /* nsComponentManagerUtils_h__ */
|