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
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/
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
15 * The Original Code is mozilla.org Code.
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.
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.
36 * ***** END LICENSE BLOCK ***** */
38 #ifndef nsComponentManagerUtils_h__
39 #define nsComponentManagerUtils_h__
49 #include "nsIFactory.h"
54 (const nsCID
&aClass
, nsISupports
*aDelegate
, const nsIID
&aIID
,
59 (const char *aContractID
, nsISupports
*aDelegate
, const nsIID
&aIID
,
64 (const nsCID
&aClass
, const nsIID
&aIID
, void **aResult
);
68 (const char *aContractID
, const nsIID
&aIID
, void **aResult
);
71 class NS_COM_GLUE nsCreateInstanceByCID
: public nsCOMPtr_helper
74 nsCreateInstanceByCID( const nsCID
& aCID
, nsISupports
* aOuter
, nsresult
* aErrorPtr
)
79 // nothing else to do here
82 virtual nsresult NS_FASTCALL
operator()( const nsIID
&, void** ) const;
90 class NS_COM_GLUE nsCreateInstanceByContractID
: public nsCOMPtr_helper
93 nsCreateInstanceByContractID( const char* aContractID
, nsISupports
* aOuter
, nsresult
* aErrorPtr
)
94 : mContractID(aContractID
),
98 // nothing else to do here
101 virtual nsresult NS_FASTCALL
operator()( const nsIID
&, void** ) const;
104 const char* mContractID
;
109 class NS_COM_GLUE nsCreateInstanceFromFactory
: public nsCOMPtr_helper
112 nsCreateInstanceFromFactory( nsIFactory
* aFactory
, nsISupports
* aOuter
, nsresult
* aErrorPtr
)
113 : mFactory(aFactory
),
117 // nothing else to do here
120 virtual nsresult NS_FASTCALL
operator()( const nsIID
&, void** ) const;
123 nsIFactory
* mFactory
;
130 const nsCreateInstanceByCID
131 do_CreateInstance( const nsCID
& aCID
, nsresult
* error
= 0 )
133 return nsCreateInstanceByCID(aCID
, 0, error
);
137 const nsCreateInstanceByCID
138 do_CreateInstance( const nsCID
& aCID
, nsISupports
* aOuter
, nsresult
* error
= 0 )
140 return nsCreateInstanceByCID(aCID
, aOuter
, error
);
144 const nsCreateInstanceByContractID
145 do_CreateInstance( const char* aContractID
, nsresult
* error
= 0 )
147 return nsCreateInstanceByContractID(aContractID
, 0, error
);
151 const nsCreateInstanceByContractID
152 do_CreateInstance( const char* aContractID
, nsISupports
* aOuter
, nsresult
* error
= 0 )
154 return nsCreateInstanceByContractID(aContractID
, aOuter
, error
);
158 const nsCreateInstanceFromFactory
159 do_CreateInstance( nsIFactory
* aFactory
, nsresult
* error
= 0 )
161 return nsCreateInstanceFromFactory(aFactory
, 0, error
);
165 const nsCreateInstanceFromFactory
166 do_CreateInstance( nsIFactory
* aFactory
, nsISupports
* aOuter
, nsresult
* error
= 0 )
168 return nsCreateInstanceFromFactory(aFactory
, aOuter
, error
);
172 class NS_COM_GLUE nsGetClassObjectByCID
: public nsCOMPtr_helper
175 nsGetClassObjectByCID( const nsCID
& aCID
, nsresult
* aErrorPtr
)
179 // nothing else to do here
182 virtual nsresult NS_FASTCALL
operator()( const nsIID
&, void** ) const;
189 class NS_COM_GLUE nsGetClassObjectByContractID
: public nsCOMPtr_helper
192 nsGetClassObjectByContractID( const char* aContractID
, nsresult
* aErrorPtr
)
193 : mContractID(aContractID
),
196 // nothing else to do here
199 virtual nsresult NS_FASTCALL
operator()( const nsIID
&, void** ) const;
202 const char* mContractID
;
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
213 inline const nsGetClassObjectByCID
214 do_GetClassObject( const nsCID
& aCID
, nsresult
* error
= 0 )
216 return nsGetClassObjectByCID(aCID
, error
);
219 inline const nsGetClassObjectByContractID
220 do_GetClassObject( const char* aContractID
, nsresult
* error
= 0 )
222 return nsGetClassObjectByContractID(aContractID
, error
);
225 // type-safe shortcuts for calling |CreateInstance|
226 template <class DestinationType
>
229 CallCreateInstance( const nsCID
&aClass
,
230 nsISupports
*aDelegate
,
231 DestinationType
** aDestination
)
233 NS_PRECONDITION(aDestination
, "null parameter");
235 return CallCreateInstance(aClass
, aDelegate
,
236 NS_GET_TEMPLATE_IID(DestinationType
),
237 reinterpret_cast<void**>(aDestination
));
240 template <class DestinationType
>
243 CallCreateInstance( const nsCID
&aClass
,
244 DestinationType
** aDestination
)
246 NS_PRECONDITION(aDestination
, "null parameter");
248 return CallCreateInstance(aClass
, nsnull
,
249 NS_GET_TEMPLATE_IID(DestinationType
),
250 reinterpret_cast<void**>(aDestination
));
253 template <class DestinationType
>
256 CallCreateInstance( const char *aContractID
,
257 nsISupports
*aDelegate
,
258 DestinationType
** aDestination
)
260 NS_PRECONDITION(aContractID
, "null parameter");
261 NS_PRECONDITION(aDestination
, "null parameter");
263 return CallCreateInstance(aContractID
,
265 NS_GET_TEMPLATE_IID(DestinationType
),
266 reinterpret_cast<void**>(aDestination
));
269 template <class DestinationType
>
272 CallCreateInstance( const char *aContractID
,
273 DestinationType
** aDestination
)
275 NS_PRECONDITION(aContractID
, "null parameter");
276 NS_PRECONDITION(aDestination
, "null parameter");
278 return CallCreateInstance(aContractID
, nsnull
,
279 NS_GET_TEMPLATE_IID(DestinationType
),
280 reinterpret_cast<void**>(aDestination
));
283 template <class DestinationType
>
286 CallCreateInstance( nsIFactory
*aFactory
,
287 nsISupports
*aDelegate
,
288 DestinationType
** aDestination
)
290 NS_PRECONDITION(aFactory
, "null parameter");
291 NS_PRECONDITION(aDestination
, "null parameter");
293 return aFactory
->CreateInstance(aDelegate
,
294 NS_GET_TEMPLATE_IID(DestinationType
),
295 reinterpret_cast<void**>(aDestination
));
298 template <class DestinationType
>
301 CallCreateInstance( nsIFactory
*aFactory
,
302 DestinationType
** aDestination
)
304 NS_PRECONDITION(aFactory
, "null parameter");
305 NS_PRECONDITION(aDestination
, "null parameter");
307 return aFactory
->CreateInstance(nsnull
,
308 NS_GET_TEMPLATE_IID(DestinationType
),
309 reinterpret_cast<void**>(aDestination
));
312 template <class DestinationType
>
315 CallGetClassObject( const nsCID
&aClass
,
316 DestinationType
** aDestination
)
318 NS_PRECONDITION(aDestination
, "null parameter");
320 return CallGetClassObject(aClass
,
321 NS_GET_TEMPLATE_IID(DestinationType
), reinterpret_cast<void**>(aDestination
));
324 template <class DestinationType
>
327 CallGetClassObject( const char* aContractID
,
328 DestinationType
** aDestination
)
330 NS_PRECONDITION(aDestination
, "null parameter");
332 return CallGetClassObject(aContractID
,
333 NS_GET_TEMPLATE_IID(DestinationType
), reinterpret_cast<void**>(aDestination
));
336 #endif /* nsComponentManagerUtils_h__ */