1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_GenericModule_h
8 #define mozilla_GenericModule_h
10 #include <type_traits>
12 #include "mozilla/AlreadyAddRefed.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/Module.h"
16 #define NS_GENERIC_FACTORY_CONSTRUCTOR(_InstanceClass) \
17 static nsresult _InstanceClass##Constructor(REFNSIID aIID, void** aResult) { \
18 RefPtr<_InstanceClass> inst; \
22 inst = new _InstanceClass(); \
23 return inst->QueryInterface(aIID, aResult); \
26 #define NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(_InstanceClass, _InitMethod) \
27 static nsresult _InstanceClass##Constructor(REFNSIID aIID, void** aResult) { \
30 RefPtr<_InstanceClass> inst; \
34 inst = new _InstanceClass(); \
35 rv = inst->_InitMethod(); \
36 if (NS_SUCCEEDED(rv)) { \
37 rv = inst->QueryInterface(aIID, aResult); \
47 struct RemoveAlreadyAddRefed
{
52 struct RemoveAlreadyAddRefed
<already_AddRefed
<T
>> {
57 } // namespace mozilla
59 // 'Constructor' that uses an existing getter function that gets a singleton.
60 #define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(_InstanceClass, _GetterProc) \
61 static nsresult _InstanceClass##Constructor(REFNSIID aIID, void** aResult) { \
62 RefPtr<_InstanceClass> inst; \
67 mozilla::detail::RemoveAlreadyAddRefed<decltype(_GetterProc())>::Type; \
69 std::is_same_v<already_AddRefed<T>, decltype(_GetterProc())>, \
70 "Singleton constructor must return already_AddRefed"); \
72 std::is_base_of<_InstanceClass, T>::value, \
73 "Singleton constructor must return correct already_AddRefed"); \
74 inst = _GetterProc(); \
75 if (nullptr == inst) { \
76 return NS_ERROR_OUT_OF_MEMORY; \
78 return inst->QueryInterface(aIID, aResult); \
81 #endif // mozilla_GenericModule_h