Back to index

lightning-sunbird  0.9+nobinonly
nsComponentManagerUtils.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org Code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsComponentManagerUtils_h__
00039 #define nsComponentManagerUtils_h__
00040 
00041 #ifndef nscore_h__
00042 #include "nscore.h"
00043 #endif
00044 
00045 #ifndef nsCOMPtr_h__
00046 #include "nsCOMPtr.h"
00047 #endif
00048 
00049 #include "nsIFactory.h"
00050 
00051 
00052 NS_COM_GLUE nsresult
00053 CallCreateInstance
00054   (const nsCID &aClass, nsISupports *aDelegate, const nsIID &aIID,
00055    void **aResult);
00056 
00057 NS_COM_GLUE nsresult
00058 CallCreateInstance
00059   (const char *aContractID, nsISupports *aDelegate, const nsIID &aIID,
00060    void **aResult);
00061 
00062 NS_COM_GLUE nsresult
00063 CallGetClassObject
00064   (const nsCID &aClass, const nsIID &aIID, void **aResult);
00065 
00066 NS_COM_GLUE nsresult
00067 CallGetClassObject
00068   (const char *aContractID, const nsIID &aIID, void **aResult);
00069 
00070 
00071 class NS_COM_GLUE nsCreateInstanceByCID : public nsCOMPtr_helper
00072 {
00073 public:
00074     nsCreateInstanceByCID( const nsCID& aCID, nsISupports* aOuter, nsresult* aErrorPtr )
00075         : mCID(aCID),
00076           mOuter(aOuter),
00077           mErrorPtr(aErrorPtr)
00078     {
00079         // nothing else to do here
00080     }
00081     
00082     virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
00083     
00084 private:
00085     const nsCID&    mCID;
00086     nsISupports*    mOuter;
00087     nsresult*       mErrorPtr;
00088 };
00089 
00090 class NS_COM_GLUE nsCreateInstanceByContractID : public nsCOMPtr_helper
00091 {
00092 public:
00093     nsCreateInstanceByContractID( const char* aContractID, nsISupports* aOuter, nsresult* aErrorPtr )
00094         : mContractID(aContractID),
00095           mOuter(aOuter),
00096           mErrorPtr(aErrorPtr)
00097     {
00098         // nothing else to do here
00099     }
00100     
00101     virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
00102     
00103 private:
00104     const char*   mContractID;
00105     nsISupports*  mOuter;
00106     nsresult*     mErrorPtr;
00107 };
00108 
00109 class NS_COM_GLUE nsCreateInstanceFromFactory : public nsCOMPtr_helper
00110 {
00111 public:
00112     nsCreateInstanceFromFactory( nsIFactory* aFactory, nsISupports* aOuter, nsresult* aErrorPtr )
00113         : mFactory(aFactory),
00114           mOuter(aOuter),
00115           mErrorPtr(aErrorPtr)
00116     {
00117         // nothing else to do here
00118     }
00119     
00120     virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
00121     
00122 private:
00123     nsIFactory*   mFactory;
00124     nsISupports*  mOuter;
00125     nsresult*     mErrorPtr;
00126 };
00127 
00128 
00129 inline
00130 const nsCreateInstanceByCID
00131 do_CreateInstance( const nsCID& aCID, nsresult* error = 0 )
00132 {
00133     return nsCreateInstanceByCID(aCID, 0, error);
00134 }
00135 
00136 inline
00137 const nsCreateInstanceByCID
00138 do_CreateInstance( const nsCID& aCID, nsISupports* aOuter, nsresult* error = 0 )
00139 {
00140     return nsCreateInstanceByCID(aCID, aOuter, error);
00141 }
00142 
00143 inline
00144 const nsCreateInstanceByContractID
00145 do_CreateInstance( const char* aContractID, nsresult* error = 0 )
00146 {
00147     return nsCreateInstanceByContractID(aContractID, 0, error);
00148 }
00149 
00150 inline
00151 const nsCreateInstanceByContractID
00152 do_CreateInstance( const char* aContractID, nsISupports* aOuter, nsresult* error = 0 )
00153 {
00154     return nsCreateInstanceByContractID(aContractID, aOuter, error);
00155 }
00156 
00157 inline
00158 const nsCreateInstanceFromFactory
00159 do_CreateInstance( nsIFactory* aFactory, nsresult* error = 0 )
00160 {
00161     return nsCreateInstanceFromFactory(aFactory, 0, error);
00162 }
00163 
00164 inline
00165 const nsCreateInstanceFromFactory
00166 do_CreateInstance( nsIFactory* aFactory, nsISupports* aOuter, nsresult* error = 0 )
00167 {
00168     return nsCreateInstanceFromFactory(aFactory, aOuter, error);
00169 }
00170 
00171 
00172 class NS_COM_GLUE nsGetClassObjectByCID : public nsCOMPtr_helper
00173 {
00174 public:
00175     nsGetClassObjectByCID( const nsCID& aCID, nsresult* aErrorPtr )
00176         : mCID(aCID),
00177           mErrorPtr(aErrorPtr)
00178     {
00179         // nothing else to do here
00180     }
00181     
00182     virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
00183     
00184 private:
00185     const nsCID&    mCID;
00186     nsresult*       mErrorPtr;
00187 };
00188 
00189 class NS_COM_GLUE nsGetClassObjectByContractID : public nsCOMPtr_helper
00190 {
00191 public:
00192     nsGetClassObjectByContractID( const char* aContractID, nsresult* aErrorPtr )
00193         : mContractID(aContractID),
00194           mErrorPtr(aErrorPtr)
00195     {
00196         // nothing else to do here
00197     }
00198     
00199     virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const;
00200     
00201 private:
00202     const char*   mContractID;
00203     nsresult*     mErrorPtr;
00204 };
00205 
00213 inline const nsGetClassObjectByCID
00214 do_GetClassObject( const nsCID& aCID, nsresult* error = 0 )
00215 {
00216     return nsGetClassObjectByCID(aCID, error);
00217 }
00218 
00219 inline const nsGetClassObjectByContractID
00220 do_GetClassObject( const char* aContractID, nsresult* error = 0 )
00221 {
00222     return nsGetClassObjectByContractID(aContractID, error);
00223 }
00224 
00225 // type-safe shortcuts for calling |CreateInstance|
00226 template <class DestinationType>
00227 inline
00228 nsresult
00229 CallCreateInstance( const nsCID &aClass,
00230                     nsISupports *aDelegate,
00231                     DestinationType** aDestination )
00232 {
00233     NS_PRECONDITION(aDestination, "null parameter");
00234     
00235     return CallCreateInstance(aClass, aDelegate,
00236                               NS_GET_IID(DestinationType),
00237                               NS_REINTERPRET_CAST(void**, aDestination));
00238 }
00239 
00240 template <class DestinationType>
00241 inline
00242 nsresult
00243 CallCreateInstance( const nsCID &aClass,
00244                     DestinationType** aDestination )
00245 {
00246     NS_PRECONDITION(aDestination, "null parameter");
00247     
00248     return CallCreateInstance(aClass, nsnull,
00249                               NS_GET_IID(DestinationType),
00250                               NS_REINTERPRET_CAST(void**, aDestination));
00251 }
00252 
00253 template <class DestinationType>
00254 inline
00255 nsresult
00256 CallCreateInstance( const char *aContractID,
00257                     nsISupports *aDelegate,
00258                     DestinationType** aDestination )
00259 {
00260     NS_PRECONDITION(aContractID, "null parameter");
00261     NS_PRECONDITION(aDestination, "null parameter");
00262     
00263     return CallCreateInstance(aContractID, 
00264                               aDelegate,
00265                               NS_GET_IID(DestinationType),
00266                               NS_REINTERPRET_CAST(void**, aDestination));
00267 }
00268 
00269 template <class DestinationType>
00270 inline
00271 nsresult
00272 CallCreateInstance( const char *aContractID,
00273                     DestinationType** aDestination )
00274 {
00275     NS_PRECONDITION(aContractID, "null parameter");
00276     NS_PRECONDITION(aDestination, "null parameter");
00277     
00278     return CallCreateInstance(aContractID, nsnull,
00279                               NS_GET_IID(DestinationType),
00280                               NS_REINTERPRET_CAST(void**, aDestination));
00281 }
00282 
00283 template <class DestinationType>
00284 inline
00285 nsresult
00286 CallCreateInstance( nsIFactory *aFactory,
00287                     nsISupports *aDelegate,
00288                     DestinationType** aDestination )
00289 {
00290     NS_PRECONDITION(aFactory, "null parameter");
00291     NS_PRECONDITION(aDestination, "null parameter");
00292     
00293     return aFactory->CreateInstance(aDelegate,
00294                                     NS_GET_IID(DestinationType),
00295                                     NS_REINTERPRET_CAST(void**, aDestination));
00296 }
00297 
00298 template <class DestinationType>
00299 inline
00300 nsresult
00301 CallCreateInstance( nsIFactory *aFactory,
00302                     DestinationType** aDestination )
00303 {
00304     NS_PRECONDITION(aFactory, "null parameter");
00305     NS_PRECONDITION(aDestination, "null parameter");
00306     
00307     return aFactory->CreateInstance(nsnull,
00308                                     NS_GET_IID(DestinationType),
00309                                     NS_REINTERPRET_CAST(void**, aDestination));
00310 }
00311 
00312 template <class DestinationType>
00313 inline
00314 nsresult
00315 CallGetClassObject( const nsCID &aClass,
00316                     DestinationType** aDestination )
00317 {
00318     NS_PRECONDITION(aDestination, "null parameter");
00319     
00320     return CallGetClassObject(aClass,
00321         NS_GET_IID(DestinationType), NS_REINTERPRET_CAST(void**, aDestination));
00322 }
00323 
00324 template <class DestinationType>
00325 inline
00326 nsresult
00327 CallGetClassObject( const char* aContractID,
00328                     DestinationType** aDestination )
00329 {
00330     NS_PRECONDITION(aDestination, "null parameter");
00331     
00332     return CallGetClassObject(aContractID,
00333         NS_GET_IID(DestinationType), NS_REINTERPRET_CAST(void**, aDestination));
00334 }
00335 
00336 #endif /* nsComponentManagerUtils_h__ */