Bug 1932613 - temporarily disable browser_ml_end_to_end.js for permanent failures...
[gecko.git] / xpcom / components / ModuleUtils.h
blob92ec8aa173c201035345ed4b6177d000e6f60804
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; \
20 *aResult = nullptr; \
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) { \
28 nsresult rv; \
30 RefPtr<_InstanceClass> inst; \
32 *aResult = nullptr; \
34 inst = new _InstanceClass(); \
35 rv = inst->_InitMethod(); \
36 if (NS_SUCCEEDED(rv)) { \
37 rv = inst->QueryInterface(aIID, aResult); \
38 } \
40 return rv; \
43 namespace mozilla {
44 namespace detail {
46 template <typename T>
47 struct RemoveAlreadyAddRefed {
48 using Type = T;
51 template <typename T>
52 struct RemoveAlreadyAddRefed<already_AddRefed<T>> {
53 using Type = T;
56 } // namespace detail
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; \
64 *aResult = nullptr; \
66 using T = \
67 mozilla::detail::RemoveAlreadyAddRefed<decltype(_GetterProc())>::Type; \
68 static_assert( \
69 std::is_same_v<already_AddRefed<T>, decltype(_GetterProc())>, \
70 "Singleton constructor must return already_AddRefed"); \
71 static_assert( \
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; \
77 } \
78 return inst->QueryInterface(aIID, aResult); \
81 #endif // mozilla_GenericModule_h