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 mozilla_GenericModule_h michael@0: #define mozilla_GenericModule_h michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/Module.h" michael@0: michael@0: #define NS_GENERIC_FACTORY_CONSTRUCTOR(_InstanceClass) \ michael@0: static nsresult \ michael@0: _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \ michael@0: void **aResult) \ michael@0: { \ michael@0: nsresult rv; \ michael@0: \ michael@0: _InstanceClass * inst; \ michael@0: \ michael@0: *aResult = nullptr; \ michael@0: if (nullptr != aOuter) { \ michael@0: rv = NS_ERROR_NO_AGGREGATION; \ michael@0: return rv; \ michael@0: } \ michael@0: \ michael@0: inst = new _InstanceClass(); \ michael@0: if (nullptr == inst) { \ michael@0: rv = NS_ERROR_OUT_OF_MEMORY; \ michael@0: return rv; \ michael@0: } \ michael@0: NS_ADDREF(inst); \ michael@0: rv = inst->QueryInterface(aIID, aResult); \ michael@0: NS_RELEASE(inst); \ michael@0: \ michael@0: return rv; \ michael@0: } michael@0: michael@0: #define NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(_InstanceClass, _InitMethod) \ michael@0: static nsresult \ michael@0: _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \ michael@0: void **aResult) \ michael@0: { \ michael@0: nsresult rv; \ michael@0: \ michael@0: _InstanceClass * inst; \ michael@0: \ michael@0: *aResult = nullptr; \ michael@0: if (nullptr != aOuter) { \ michael@0: rv = NS_ERROR_NO_AGGREGATION; \ michael@0: return rv; \ michael@0: } \ michael@0: \ michael@0: inst = new _InstanceClass(); \ michael@0: if (nullptr == inst) { \ michael@0: rv = NS_ERROR_OUT_OF_MEMORY; \ michael@0: return rv; \ michael@0: } \ michael@0: NS_ADDREF(inst); \ michael@0: rv = inst->_InitMethod(); \ michael@0: if(NS_SUCCEEDED(rv)) { \ michael@0: rv = inst->QueryInterface(aIID, aResult); \ michael@0: } \ michael@0: NS_RELEASE(inst); \ michael@0: \ michael@0: return rv; \ michael@0: } michael@0: michael@0: // 'Constructor' that uses an existing getter function that gets a singleton. michael@0: // NOTE: assumes that getter does an AddRef - so additional AddRef is not done. michael@0: #define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(_InstanceClass, _GetterProc) \ michael@0: static nsresult \ michael@0: _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \ michael@0: void **aResult) \ michael@0: { \ michael@0: nsresult rv; \ michael@0: \ michael@0: _InstanceClass * inst; \ michael@0: \ michael@0: *aResult = nullptr; \ michael@0: if (nullptr != aOuter) { \ michael@0: rv = NS_ERROR_NO_AGGREGATION; \ michael@0: return rv; \ michael@0: } \ michael@0: \ michael@0: inst = already_AddRefed<_InstanceClass>(_GetterProc()).take(); \ michael@0: if (nullptr == inst) { \ michael@0: rv = NS_ERROR_OUT_OF_MEMORY; \ michael@0: return rv; \ michael@0: } \ michael@0: /* NS_ADDREF(inst); */ \ michael@0: rv = inst->QueryInterface(aIID, aResult); \ michael@0: NS_RELEASE(inst); \ michael@0: \ michael@0: return rv; \ michael@0: } michael@0: michael@0: #ifndef MOZILLA_INTERNAL_API michael@0: michael@0: #include "nsIModule.h" michael@0: #include "nsISupportsUtils.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: class GenericModule MOZ_FINAL : public nsIModule michael@0: { michael@0: public: michael@0: GenericModule(const mozilla::Module* aData) michael@0: : mData(aData) michael@0: { michael@0: } michael@0: michael@0: NS_DECL_THREADSAFE_ISUPPORTS michael@0: NS_DECL_NSIMODULE michael@0: michael@0: private: michael@0: const mozilla::Module* mData; michael@0: }; michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #define NS_IMPL_MOZILLA192_NSGETMODULE(module) \ michael@0: extern "C" NS_EXPORT nsresult \ michael@0: NSGetModule(nsIComponentManager* aCompMgr, \ michael@0: nsIFile* aLocation, \ michael@0: nsIModule** aResult) \ michael@0: { \ michael@0: *aResult = new mozilla::GenericModule(module); \ michael@0: NS_ADDREF(*aResult); \ michael@0: return NS_OK; \ michael@0: } michael@0: michael@0: #endif michael@0: michael@0: #endif // mozilla_GenericModule_h