PageRenderTime 36ms CodeModel.GetById 10ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/gecko_api/include/nsComponentManagerUtils.h

http://firefox-mac-pdf.googlecode.com/
C++ Header | 336 lines | 235 code | 52 blank | 49 comment | 0 complexity | 6f07257655724e0769153e09f675c214 MD5 | raw file
  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
 52NS_COM_GLUE nsresult
 53CallCreateInstance
 54  (const nsCID &aClass, nsISupports *aDelegate, const nsIID &aIID,
 55   void **aResult);
 56
 57NS_COM_GLUE nsresult
 58CallCreateInstance
 59  (const char *aContractID, nsISupports *aDelegate, const nsIID &aIID,
 60   void **aResult);
 61
 62NS_COM_GLUE nsresult
 63CallGetClassObject
 64  (const nsCID &aClass, const nsIID &aIID, void **aResult);
 65
 66NS_COM_GLUE nsresult
 67CallGetClassObject
 68  (const char *aContractID, const nsIID &aIID, void **aResult);
 69
 70
 71class NS_COM_GLUE nsCreateInstanceByCID : public nsCOMPtr_helper
 72{
 73public:
 74    nsCreateInstanceByCID( const nsCID& aCID, nsISupports* aOuter, nsresult* aErrorPtr )
 75        : mCID(aCID),
 76          mOuter(aOuter),
 77          mErrorPtr(aErrorPtr)
 78    {
 79        // nothing else to do here
 80    }
 81    
 82    virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
 83    
 84private:
 85    const nsCID&    mCID;
 86    nsISupports*    mOuter;
 87    nsresult*       mErrorPtr;
 88};
 89
 90class NS_COM_GLUE nsCreateInstanceByContractID : public nsCOMPtr_helper
 91{
 92public:
 93    nsCreateInstanceByContractID( const char* aContractID, nsISupports* aOuter, nsresult* aErrorPtr )
 94        : mContractID(aContractID),
 95          mOuter(aOuter),
 96          mErrorPtr(aErrorPtr)
 97    {
 98        // nothing else to do here
 99    }
100    
101    virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
102    
103private:
104    const char*   mContractID;
105    nsISupports*  mOuter;
106    nsresult*     mErrorPtr;
107};
108
109class NS_COM_GLUE nsCreateInstanceFromFactory : public nsCOMPtr_helper
110{
111public:
112    nsCreateInstanceFromFactory( nsIFactory* aFactory, nsISupports* aOuter, nsresult* aErrorPtr )
113        : mFactory(aFactory),
114          mOuter(aOuter),
115          mErrorPtr(aErrorPtr)
116    {
117        // nothing else to do here
118    }
119    
120    virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
121    
122private:
123    nsIFactory*   mFactory;
124    nsISupports*  mOuter;
125    nsresult*     mErrorPtr;
126};
127
128
129inline
130const nsCreateInstanceByCID
131do_CreateInstance( const nsCID& aCID, nsresult* error = 0 )
132{
133    return nsCreateInstanceByCID(aCID, 0, error);
134}
135
136inline
137const nsCreateInstanceByCID
138do_CreateInstance( const nsCID& aCID, nsISupports* aOuter, nsresult* error = 0 )
139{
140    return nsCreateInstanceByCID(aCID, aOuter, error);
141}
142
143inline
144const nsCreateInstanceByContractID
145do_CreateInstance( const char* aContractID, nsresult* error = 0 )
146{
147    return nsCreateInstanceByContractID(aContractID, 0, error);
148}
149
150inline
151const nsCreateInstanceByContractID
152do_CreateInstance( const char* aContractID, nsISupports* aOuter, nsresult* error = 0 )
153{
154    return nsCreateInstanceByContractID(aContractID, aOuter, error);
155}
156
157inline
158const nsCreateInstanceFromFactory
159do_CreateInstance( nsIFactory* aFactory, nsresult* error = 0 )
160{
161    return nsCreateInstanceFromFactory(aFactory, 0, error);
162}
163
164inline
165const nsCreateInstanceFromFactory
166do_CreateInstance( nsIFactory* aFactory, nsISupports* aOuter, nsresult* error = 0 )
167{
168    return nsCreateInstanceFromFactory(aFactory, aOuter, error);
169}
170
171
172class NS_COM_GLUE nsGetClassObjectByCID : public nsCOMPtr_helper
173{
174public:
175    nsGetClassObjectByCID( const nsCID& aCID, nsresult* aErrorPtr )
176        : mCID(aCID),
177          mErrorPtr(aErrorPtr)
178    {
179        // nothing else to do here
180    }
181    
182    virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
183    
184private:
185    const nsCID&    mCID;
186    nsresult*       mErrorPtr;
187};
188
189class NS_COM_GLUE nsGetClassObjectByContractID : public nsCOMPtr_helper
190{
191public:
192    nsGetClassObjectByContractID( const char* aContractID, nsresult* aErrorPtr )
193        : mContractID(aContractID),
194          mErrorPtr(aErrorPtr)
195    {
196        // nothing else to do here
197    }
198    
199    virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
200    
201private:
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 */
213inline const nsGetClassObjectByCID
214do_GetClassObject( const nsCID& aCID, nsresult* error = 0 )
215{
216    return nsGetClassObjectByCID(aCID, error);
217}
218
219inline const nsGetClassObjectByContractID
220do_GetClassObject( const char* aContractID, nsresult* error = 0 )
221{
222    return nsGetClassObjectByContractID(aContractID, error);
223}
224
225// type-safe shortcuts for calling |CreateInstance|
226template <class DestinationType>
227inline
228nsresult
229CallCreateInstance( 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
240template <class DestinationType>
241inline
242nsresult
243CallCreateInstance( const nsCID &aClass,
244                    DestinationType** aDestination )
245{
246    NS_PRECONDITION(aDestination, "null parameter");
247    
248    return CallCreateInstance(aClass, nsnull,
249                              NS_GET_TEMPLATE_IID(DestinationType),
250                              reinterpret_cast<void**>(aDestination));
251}
252
253template <class DestinationType>
254inline
255nsresult
256CallCreateInstance( 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
269template <class DestinationType>
270inline
271nsresult
272CallCreateInstance( const char *aContractID,
273                    DestinationType** aDestination )
274{
275    NS_PRECONDITION(aContractID, "null parameter");
276    NS_PRECONDITION(aDestination, "null parameter");
277    
278    return CallCreateInstance(aContractID, nsnull,
279                              NS_GET_TEMPLATE_IID(DestinationType),
280                              reinterpret_cast<void**>(aDestination));
281}
282
283template <class DestinationType>
284inline
285nsresult
286CallCreateInstance( 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
298template <class DestinationType>
299inline
300nsresult
301CallCreateInstance( 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
312template <class DestinationType>
313inline
314nsresult
315CallGetClassObject( 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
324template <class DestinationType>
325inline
326nsresult
327CallGetClassObject( const char* aContractID,
328                    DestinationType** aDestination )
329{
330    NS_PRECONDITION(aDestination, "null parameter");
331    
332    return CallGetClassObject(aContractID,
333        NS_GET_TEMPLATE_IID(DestinationType), reinterpret_cast<void**>(aDestination));
334}
335
336#endif /* nsComponentManagerUtils_h__ */