forked from microsoft/react-native-windows
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathModuleRegistration.h
More file actions
193 lines (156 loc) · 11.4 KB
/
ModuleRegistration.h
File metadata and controls
193 lines (156 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// IMPORTANT: Before updating this file
// please read react-native-windows repo:
// vnext/Microsoft.ReactNative.Cxx/README.md
#pragma once
#include "winrt/Microsoft.ReactNative.h"
// We implement optional parameter macros based on the StackOverflow discussion:
// https://stackoverflow.com/questions/3046889/optional-parameters-with-c-macros
// Please refer to it if you want to understand it works.
#define INTERNAL_REACT_GET_ARG_4(arg1, arg2, arg3, arg4, ...) arg4
#define INTERNAL_REACT_RECOMPOSER_4(argsWithParentheses) INTERNAL_REACT_GET_ARG_4 argsWithParentheses
//
// The macros below are internal implementation details for macro defined in nativeModules.h
//
// ADL-based fallback: returns false for any type not tagged by the macros below.
// Using ADL instead of explicit template specialization allows the macros to be
// used inside a namespace (explicit specializations of a global template must
// occur at global scope, which the macros cannot guarantee).
template <typename T>
constexpr bool ReactIsReactTurboModuleImpl(T *) noexcept {
return false;
}
template <typename T>
struct IsReactTurboModule : std::bool_constant<ReactIsReactTurboModuleImpl(static_cast<T *>(nullptr))> {};
#define INTERNAL_REACT_MODULE_REGISTRATION_AND_PROVIDER( \
moduleStruct, moduleName, eventEmitterName, isReactTurboModule) \
struct moduleStruct; \
\
constexpr bool ReactIsReactTurboModuleImpl(moduleStruct *) noexcept { return isReactTurboModule; } \
\
template <class TDummy> \
struct moduleStruct##_ModuleRegistration final : winrt::Microsoft::ReactNative::ModuleRegistration { \
moduleStruct##_ModuleRegistration() noexcept : winrt::Microsoft::ReactNative::ModuleRegistration{moduleName} {} \
\
winrt::Microsoft::ReactNative::ReactModuleProvider MakeModuleProvider() const noexcept override { \
return winrt::Microsoft::ReactNative::MakeModuleProvider<moduleStruct>(); \
} \
\
bool ShouldRegisterAsTurboModule() const noexcept override { return isReactTurboModule; } \
\
static const moduleStruct##_ModuleRegistration Registration; \
}; \
\
template <class TDummy> \
const moduleStruct##_ModuleRegistration<TDummy> moduleStruct##_ModuleRegistration<TDummy>::Registration; \
template struct moduleStruct##_ModuleRegistration<int>; \
\
template <class TRegistry> \
constexpr void GetReactModuleInfo(moduleStruct *, TRegistry ®istry) noexcept { \
registry.RegisterModule( \
moduleName, eventEmitterName, winrt::Microsoft::ReactNative::ReactAttributeId<__COUNTER__>{}); \
}
#define INTERNAL_REACT_MODULE_3_ARGS(moduleStruct, moduleName, eventEmitterName) \
INTERNAL_REACT_MODULE_REGISTRATION_AND_PROVIDER(moduleStruct, moduleName, eventEmitterName, false)
#define INTERNAL_REACT_MODULE_2_ARGS(moduleStruct, moduleName) \
INTERNAL_REACT_MODULE_3_ARGS(moduleStruct, moduleName, L"")
#define INTERNAL_REACT_MODULE_1_ARG(moduleStruct) INTERNAL_REACT_MODULE_2_ARGS(moduleStruct, L## #moduleStruct)
#define INTERNAL_REACT_MODULE(...) \
INTERNAL_REACT_RECOMPOSER_4( \
(__VA_ARGS__, INTERNAL_REACT_MODULE_3_ARGS, INTERNAL_REACT_MODULE_2_ARGS, INTERNAL_REACT_MODULE_1_ARG, ))
#define INTERNAL_REACT_MODULE_NO_REGISTRATION_AND_PROVIDER( \
moduleStruct, moduleName, eventEmitterName, isReactTurboModule) \
struct moduleStruct; \
\
[[maybe_unused]] constexpr bool ReactIsReactTurboModuleImpl(moduleStruct *) noexcept { return isReactTurboModule; } \
\
template <class TRegistry> \
constexpr void GetReactModuleInfo(moduleStruct *, TRegistry ®istry) noexcept { \
registry.RegisterModule( \
moduleName, eventEmitterName, winrt::Microsoft::ReactNative::ReactAttributeId<__COUNTER__>{}); \
}
// Another version of REACT_MODULE but does not do auto registration
#define INTERNAL_REACT_MODULE_NOREG_3_ARGS(moduleStruct, moduleName, eventEmitterName) \
INTERNAL_REACT_MODULE_NO_REGISTRATION_AND_PROVIDER(moduleStruct, moduleName, eventEmitterName, false)
#define INTERNAL_REACT_MODULE_NOREG_2_ARGS(moduleStruct, moduleName) \
INTERNAL_REACT_MODULE_NOREG_3_ARGS(moduleStruct, moduleName, L"")
#define INTERNAL_REACT_MODULE_NOREG_1_ARG(moduleStruct) \
INTERNAL_REACT_MODULE_NOREG_2_ARGS(moduleStruct, L## #moduleStruct)
#define INTERNAL_REACT_MODULE_NOREG(...) \
INTERNAL_REACT_RECOMPOSER_4( \
(__VA_ARGS__, \
INTERNAL_REACT_MODULE_NOREG_3_ARGS, \
INTERNAL_REACT_MODULE_NOREG_2_ARGS, \
INTERNAL_REACT_MODULE_NOREG_1_ARG, ))
#define INTERNAL_REACT_GET_ARG_3(arg1, arg2, arg3, ...) arg3
#define INTERNAL_REACT_RECOMPOSER_3(argsWithParentheses) INTERNAL_REACT_GET_ARG_3 argsWithParentheses
// Register struct as a ReactNative module.
#define INTERNAL_REACT_TURBO_MODULE_2_ARGS(moduleStruct, moduleName) \
INTERNAL_REACT_MODULE_REGISTRATION_AND_PROVIDER(moduleStruct, moduleName, L"", true)
#define INTERNAL_REACT_TURBO_MODULE_1_ARG(moduleStruct) \
INTERNAL_REACT_TURBO_MODULE_2_ARGS(moduleStruct, L## #moduleStruct)
#define INTERNAL_REACT_TURBO_MODULE(...) \
INTERNAL_REACT_RECOMPOSER_3((__VA_ARGS__, INTERNAL_REACT_TURBO_MODULE_2_ARGS, INTERNAL_REACT_TURBO_MODULE_1_ARG, ))
// Another version of REACT_MODULE but does not do auto registration
#define INTERNAL_REACT_TURBO_MODULE_NOREG_2_ARGS(moduleStruct, moduleName) \
INTERNAL_REACT_MODULE_NO_REGISTRATION_AND_PROVIDER(moduleStruct, moduleName, L"", true)
#define INTERNAL_REACT_TURBO_MODULE_NOREG_1_ARG(moduleStruct) \
INTERNAL_REACT_TURBO_MODULE_NOREG_2_ARGS(moduleStruct, L## #moduleStruct)
#define INTERNAL_REACT_TURBO_MODULE_NOREG(...) \
INTERNAL_REACT_RECOMPOSER_3( \
(__VA_ARGS__, INTERNAL_REACT_TURBO_MODULE_NOREG_2_ARGS, INTERNAL_REACT_TURBO_MODULE_NOREG_1_ARG, ))
// Provide meta data information about struct member.
// For each member with a 'custom attribute' macro we create a static method to provide meta data.
// The member Id is generated as a ReactMemberId<__COUNTER__> type.
// To enumerate the static methods, we can increment ReactMemberId while static member exists.
#define INTERNAL_REACT_MEMBER_4_ARGS(memberKind, member, jsMemberName, jsModuleName) \
template <class TStruct, class TVisitor> \
constexpr static void GetReactMemberAttribute( \
TVisitor &visitor, winrt::Microsoft::ReactNative::ReactAttributeId<__COUNTER__> attributeId) noexcept { \
visitor.Visit( \
&TStruct::member, \
attributeId, \
winrt::Microsoft::ReactNative::React##memberKind##Attribute(jsMemberName, jsModuleName)); \
}
#define INTERNAL_REACT_MEMBER_3_ARGS(memberKind, member, jsMemberName) \
INTERNAL_REACT_MEMBER_4_ARGS(memberKind, member, jsMemberName, L"")
#define INTERNAL_REACT_MEMBER_2_ARGS(memberKind, member) INTERNAL_REACT_MEMBER_3_ARGS(memberKind, member, L## #member)
#define INTERNAL_REACT_MEMBER(...) \
INTERNAL_REACT_RECOMPOSER_4( \
(__VA_ARGS__, INTERNAL_REACT_MEMBER_4_ARGS, INTERNAL_REACT_MEMBER_3_ARGS, INTERNAL_REACT_MEMBER_2_ARGS, ))
namespace winrt::Microsoft::ReactNative {
struct ModuleRegistration {
ModuleRegistration(wchar_t const *moduleName) noexcept;
virtual ReactModuleProvider MakeModuleProvider() const noexcept = 0;
virtual bool ShouldRegisterAsTurboModule() const noexcept = 0;
static ModuleRegistration const *Head() noexcept {
return s_head;
}
ModuleRegistration const *Next() const noexcept {
return m_next;
}
wchar_t const *ModuleName() const noexcept {
return m_moduleName;
}
private:
wchar_t const *m_moduleName{nullptr};
ModuleRegistration const *m_next{nullptr};
static const ModuleRegistration *s_head;
};
// AddAttributedModules(IReactPackageBuilder const &packageBuilder, bool useTurboModules = false)
// Arguments:
// - packageBuilder (required).
// - useTurboModules (optional) - If set to true, all modules will be registered as TurboModules.
// Consider moving modules to use REACT_TURBO_MODULE instead.
//
// AddAttributedModules will add all modules attributed with REACT_MODULE or REACT_TURBO_MODULE into packageBuilder.
// if useTurboModules is specified, then all modules will be registered using packageBuilder.AddTurboModules
// Otherwise modules attributed with REACT_MODULE will use packageBuilder.AddModule, and modules attributed with
// REACT_TURBO_MODULE will use packageBuilder.AddTurboModule.
void AddAttributedModules(IReactPackageBuilder const &packageBuilder, bool useTurboModules = false) noexcept;
bool TryAddAttributedModule(
IReactPackageBuilder const &packageBuilder,
std::wstring_view moduleName,
bool useTurboModules = false) noexcept;
} // namespace winrt::Microsoft::ReactNative