michael@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsComponentManagerUtils_h__ michael@0: #define nsComponentManagerUtils_h__ michael@0: michael@0: #include "nscore.h" michael@0: #include "nsCOMPtr.h" michael@0: michael@0: #include "nsIFactory.h" michael@0: michael@0: michael@0: NS_COM_GLUE nsresult michael@0: CallCreateInstance michael@0: (const nsCID &aClass, nsISupports *aDelegate, const nsIID &aIID, michael@0: void **aResult); michael@0: michael@0: NS_COM_GLUE nsresult michael@0: CallCreateInstance michael@0: (const char *aContractID, nsISupports *aDelegate, const nsIID &aIID, michael@0: void **aResult); michael@0: michael@0: NS_COM_GLUE nsresult michael@0: CallGetClassObject michael@0: (const nsCID &aClass, const nsIID &aIID, void **aResult); michael@0: michael@0: NS_COM_GLUE nsresult michael@0: CallGetClassObject michael@0: (const char *aContractID, const nsIID &aIID, void **aResult); michael@0: michael@0: michael@0: class NS_COM_GLUE nsCreateInstanceByCID : public nsCOMPtr_helper michael@0: { michael@0: public: michael@0: nsCreateInstanceByCID( const nsCID& aCID, nsISupports* aOuter, nsresult* aErrorPtr ) michael@0: : mCID(aCID), michael@0: mOuter(aOuter), michael@0: mErrorPtr(aErrorPtr) michael@0: { michael@0: // nothing else to do here michael@0: } michael@0: michael@0: virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const; michael@0: michael@0: private: michael@0: const nsCID& mCID; michael@0: nsISupports* mOuter; michael@0: nsresult* mErrorPtr; michael@0: }; michael@0: michael@0: class NS_COM_GLUE nsCreateInstanceByContractID : public nsCOMPtr_helper michael@0: { michael@0: public: michael@0: nsCreateInstanceByContractID( const char* aContractID, nsISupports* aOuter, nsresult* aErrorPtr ) michael@0: : mContractID(aContractID), michael@0: mOuter(aOuter), michael@0: mErrorPtr(aErrorPtr) michael@0: { michael@0: // nothing else to do here michael@0: } michael@0: michael@0: virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const; michael@0: michael@0: private: michael@0: const char* mContractID; michael@0: nsISupports* mOuter; michael@0: nsresult* mErrorPtr; michael@0: }; michael@0: michael@0: class NS_COM_GLUE nsCreateInstanceFromFactory : public nsCOMPtr_helper michael@0: { michael@0: public: michael@0: nsCreateInstanceFromFactory( nsIFactory* aFactory, nsISupports* aOuter, nsresult* aErrorPtr ) michael@0: : mFactory(aFactory), michael@0: mOuter(aOuter), michael@0: mErrorPtr(aErrorPtr) michael@0: { michael@0: // nothing else to do here michael@0: } michael@0: michael@0: virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const; michael@0: michael@0: private: michael@0: nsIFactory* mFactory; michael@0: nsISupports* mOuter; michael@0: nsresult* mErrorPtr; michael@0: }; michael@0: michael@0: michael@0: inline michael@0: const nsCreateInstanceByCID michael@0: do_CreateInstance( const nsCID& aCID, nsresult* error = 0 ) michael@0: { michael@0: return nsCreateInstanceByCID(aCID, 0, error); michael@0: } michael@0: michael@0: inline michael@0: const nsCreateInstanceByCID michael@0: do_CreateInstance( const nsCID& aCID, nsISupports* aOuter, nsresult* error = 0 ) michael@0: { michael@0: return nsCreateInstanceByCID(aCID, aOuter, error); michael@0: } michael@0: michael@0: inline michael@0: const nsCreateInstanceByContractID michael@0: do_CreateInstance( const char* aContractID, nsresult* error = 0 ) michael@0: { michael@0: return nsCreateInstanceByContractID(aContractID, 0, error); michael@0: } michael@0: michael@0: inline michael@0: const nsCreateInstanceByContractID michael@0: do_CreateInstance( const char* aContractID, nsISupports* aOuter, nsresult* error = 0 ) michael@0: { michael@0: return nsCreateInstanceByContractID(aContractID, aOuter, error); michael@0: } michael@0: michael@0: inline michael@0: const nsCreateInstanceFromFactory michael@0: do_CreateInstance( nsIFactory* aFactory, nsresult* error = 0 ) michael@0: { michael@0: return nsCreateInstanceFromFactory(aFactory, 0, error); michael@0: } michael@0: michael@0: inline michael@0: const nsCreateInstanceFromFactory michael@0: do_CreateInstance( nsIFactory* aFactory, nsISupports* aOuter, nsresult* error = 0 ) michael@0: { michael@0: return nsCreateInstanceFromFactory(aFactory, aOuter, error); michael@0: } michael@0: michael@0: michael@0: class NS_COM_GLUE nsGetClassObjectByCID : public nsCOMPtr_helper michael@0: { michael@0: public: michael@0: nsGetClassObjectByCID( const nsCID& aCID, nsresult* aErrorPtr ) michael@0: : mCID(aCID), michael@0: mErrorPtr(aErrorPtr) michael@0: { michael@0: // nothing else to do here michael@0: } michael@0: michael@0: virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const; michael@0: michael@0: private: michael@0: const nsCID& mCID; michael@0: nsresult* mErrorPtr; michael@0: }; michael@0: michael@0: class NS_COM_GLUE nsGetClassObjectByContractID : public nsCOMPtr_helper michael@0: { michael@0: public: michael@0: nsGetClassObjectByContractID( const char* aContractID, nsresult* aErrorPtr ) michael@0: : mContractID(aContractID), michael@0: mErrorPtr(aErrorPtr) michael@0: { michael@0: // nothing else to do here michael@0: } michael@0: michael@0: virtual nsresult NS_FASTCALL operator()( const nsIID&, void** ) const; michael@0: michael@0: private: michael@0: const char* mContractID; michael@0: nsresult* mErrorPtr; michael@0: }; michael@0: michael@0: /** michael@0: * do_GetClassObject can be used to improve performance of callers michael@0: * that call |CreateInstance| many times. They can cache the factory michael@0: * and call do_CreateInstance or CallCreateInstance with the cached michael@0: * factory rather than having the component manager retrieve it every michael@0: * time. michael@0: */ michael@0: inline const nsGetClassObjectByCID michael@0: do_GetClassObject( const nsCID& aCID, nsresult* error = 0 ) michael@0: { michael@0: return nsGetClassObjectByCID(aCID, error); michael@0: } michael@0: michael@0: inline const nsGetClassObjectByContractID michael@0: do_GetClassObject( const char* aContractID, nsresult* error = 0 ) michael@0: { michael@0: return nsGetClassObjectByContractID(aContractID, error); michael@0: } michael@0: michael@0: // type-safe shortcuts for calling |CreateInstance| michael@0: template michael@0: inline michael@0: nsresult michael@0: CallCreateInstance( const nsCID &aClass, michael@0: nsISupports *aDelegate, michael@0: DestinationType** aDestination ) michael@0: { michael@0: NS_PRECONDITION(aDestination, "null parameter"); michael@0: michael@0: return CallCreateInstance(aClass, aDelegate, michael@0: NS_GET_TEMPLATE_IID(DestinationType), michael@0: reinterpret_cast(aDestination)); michael@0: } michael@0: michael@0: template michael@0: inline michael@0: nsresult michael@0: CallCreateInstance( const nsCID &aClass, michael@0: DestinationType** aDestination ) michael@0: { michael@0: NS_PRECONDITION(aDestination, "null parameter"); michael@0: michael@0: return CallCreateInstance(aClass, nullptr, michael@0: NS_GET_TEMPLATE_IID(DestinationType), michael@0: reinterpret_cast(aDestination)); michael@0: } michael@0: michael@0: template michael@0: inline michael@0: nsresult michael@0: CallCreateInstance( const char *aContractID, michael@0: nsISupports *aDelegate, michael@0: DestinationType** aDestination ) michael@0: { michael@0: NS_PRECONDITION(aContractID, "null parameter"); michael@0: NS_PRECONDITION(aDestination, "null parameter"); michael@0: michael@0: return CallCreateInstance(aContractID, michael@0: aDelegate, michael@0: NS_GET_TEMPLATE_IID(DestinationType), michael@0: reinterpret_cast(aDestination)); michael@0: } michael@0: michael@0: template michael@0: inline michael@0: nsresult michael@0: CallCreateInstance( const char *aContractID, michael@0: DestinationType** aDestination ) michael@0: { michael@0: NS_PRECONDITION(aContractID, "null parameter"); michael@0: NS_PRECONDITION(aDestination, "null parameter"); michael@0: michael@0: return CallCreateInstance(aContractID, nullptr, michael@0: NS_GET_TEMPLATE_IID(DestinationType), michael@0: reinterpret_cast(aDestination)); michael@0: } michael@0: michael@0: template michael@0: inline michael@0: nsresult michael@0: CallCreateInstance( nsIFactory *aFactory, michael@0: nsISupports *aDelegate, michael@0: DestinationType** aDestination ) michael@0: { michael@0: NS_PRECONDITION(aFactory, "null parameter"); michael@0: NS_PRECONDITION(aDestination, "null parameter"); michael@0: michael@0: return aFactory->CreateInstance(aDelegate, michael@0: NS_GET_TEMPLATE_IID(DestinationType), michael@0: reinterpret_cast(aDestination)); michael@0: } michael@0: michael@0: template michael@0: inline michael@0: nsresult michael@0: CallCreateInstance( nsIFactory *aFactory, michael@0: DestinationType** aDestination ) michael@0: { michael@0: NS_PRECONDITION(aFactory, "null parameter"); michael@0: NS_PRECONDITION(aDestination, "null parameter"); michael@0: michael@0: return aFactory->CreateInstance(nullptr, michael@0: NS_GET_TEMPLATE_IID(DestinationType), michael@0: reinterpret_cast(aDestination)); michael@0: } michael@0: michael@0: template michael@0: inline michael@0: nsresult michael@0: CallGetClassObject( const nsCID &aClass, michael@0: DestinationType** aDestination ) michael@0: { michael@0: NS_PRECONDITION(aDestination, "null parameter"); michael@0: michael@0: return CallGetClassObject(aClass, michael@0: NS_GET_TEMPLATE_IID(DestinationType), reinterpret_cast(aDestination)); michael@0: } michael@0: michael@0: template michael@0: inline michael@0: nsresult michael@0: CallGetClassObject( const char* aContractID, michael@0: DestinationType** aDestination ) michael@0: { michael@0: NS_PRECONDITION(aDestination, "null parameter"); michael@0: michael@0: return CallGetClassObject(aContractID, michael@0: NS_GET_TEMPLATE_IID(DestinationType), reinterpret_cast(aDestination)); michael@0: } michael@0: michael@0: #endif /* nsComponentManagerUtils_h__ */