-
-
Notifications
You must be signed in to change notification settings - Fork 164
Expand file tree
/
Copy pathcomponent.hpp
More file actions
267 lines (227 loc) · 8.05 KB
/
component.hpp
File metadata and controls
267 lines (227 loc) · 8.05 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/.
*
* The original code is copyright (c) 2022, open.mp team and contributors.
*/
#pragma once
#include "types.hpp"
#ifndef BUILD_NUMBER
#define BUILD_NUMBER 0
#endif
#define OMP_VERSION_SUPPORTED 1
/// Should always be used in classes inheriting IExtension
#define PROVIDE_EXT_UID(uuid) \
static constexpr UID ExtensionIID = uuid; \
UID getExtensionID() override { return ExtensionIID; }
/// A class which should be inherited by extensions returned by IExtensible
struct IExtension
{
/// Get the extension's UID
virtual UID getExtensionID() = 0;
/// Delete the extension.
/// If the extension is added dynamically with addExtension and the autoDeleteExt flag was set,
/// this will be called on destruction of the IExtensible interface or on removeExtension.
virtual void freeExtension() { }
virtual void reset() = 0;
};
/// A class which should be inherited by classes which want to be extensible without breaking the ABI
struct IExtensible
{
/// Try to get an extension by its UID
/// @return A pointer to the extension or nullptr if it's not supported
virtual IExtension* getExtension(UID id) { return nullptr; }
/// Query an extension by its type
/// Don't call directly, use global queryExtension() instead
/// @typeparam ExtensionT The extension type, must derive from IExtension
template <class ExtensionT>
ExtensionT* _queryExtension()
{
static_assert(std::is_base_of<IExtension, ExtensionT>::value, "queryExtension parameter must inherit from IExtension");
auto it = miscExtensions.find(ExtensionT::ExtensionIID);
if (it != miscExtensions.end())
{
return static_cast<ExtensionT*>(it->second.first);
}
IExtension* ext = getExtension(ExtensionT::ExtensionIID);
if (ext)
{
return static_cast<ExtensionT*>(ext);
}
return nullptr;
}
/// Add an extension dynamically
/// @param ext The extension to add
/// @param autoDeleteExt Whether the extension should be automatically deleted (its freeExtension method called) on the extensible's destruction
/// @return True if added successfully, false otherwise (the ID is already inserted)
virtual bool addExtension(IExtension* ext, bool autoDeleteExt)
{
return miscExtensions.emplace(robin_hood::pair<UID, Pair<IExtension*, bool>>(ext->getExtensionID(), std::make_pair(ext, autoDeleteExt))).second;
}
/// Remove a dynamically added extension
/// If the extension was added with the autoDeleteExt flag on this also calls its freeExtension method
/// @param ext The extension to remove
/// @return True if removed successfully, false otherwise (ID not found)
virtual bool removeExtension(IExtension* ext)
{
auto it = miscExtensions.find(ext->getExtensionID());
if (it == miscExtensions.end())
{
return false;
}
if (it->second.second)
{
it->second.first->freeExtension();
}
miscExtensions.erase(it);
return true;
}
/// Remove a dynamically added extension
/// If the extension was added with the autoDeleteExt flag on this also calls its freeExtension method
/// @param ext The extension to remove
/// @return True if removed successfully, false otherwise (ID not found)
virtual bool removeExtension(UID id)
{
auto it = miscExtensions.find(id);
if (it == miscExtensions.end())
{
return false;
}
if (it->second.second)
{
it->second.first->freeExtension();
}
miscExtensions.erase(it);
return true;
}
virtual ~IExtensible()
{
freeExtensions();
}
protected:
FlatHashMap<UID, Pair<IExtension*, bool>> miscExtensions;
void freeExtensions()
{
for (auto it = miscExtensions.begin(); it != miscExtensions.end(); ++it)
{
if (it->second.second)
{
it->second.first->freeExtension();
}
}
}
void resetExtensions()
{
for (auto it = miscExtensions.begin(); it != miscExtensions.end(); ++it)
{
if (it->second.second)
{
it->second.first->reset();
}
}
}
};
/// Query an extension by its type
/// @typeparam ExtensionT The extension type, must derive from IExtension
template <class ExtensionT>
ExtensionT* queryExtension(IExtensible* extensible)
{
return extensible->_queryExtension<ExtensionT>();
}
/// Query an extension by its type
/// @typeparam ExtensionT The extension type, must derive from IExtension
template <class ExtensionT>
ExtensionT* queryExtension(const IExtensible* extensible)
{
return extensible->_queryExtension<ExtensionT>();
}
/// Query an extension by its type
/// @typeparam ExtensionT The extension type, must derive from IExtension
template <class ExtensionT>
ExtensionT* queryExtension(IExtensible& extensible)
{
return extensible._queryExtension<ExtensionT>();
}
/// Query an extension by its type
/// @typeparam ExtensionT The extension type, must derive from IExtension
template <class ExtensionT>
ExtensionT* queryExtension(const IExtensible& extensible)
{
return extensible._queryExtension<ExtensionT>();
}
/// Should always be used in classes inheriting IUIDProvider
#define PROVIDE_UID(uuid) \
static constexpr UID IID = uuid; \
UID getUID() override { return uuid; }
/// An interface providing UIDs
struct IUIDProvider
{
virtual UID getUID() = 0;
};
enum ComponentType
{
Other,
Network,
Pool,
};
struct ICore;
struct IComponentList;
struct ILogger;
struct IEarlyConfig;
/// A component interface
struct IComponent : public IExtensible, public IUIDProvider
{
/// The idea is for the SDK to be totally forward compatible, so code built at any time will
/// always work, thanks to ABI compatibility. This method is an emergency trap door, just in
/// case that's ever not the problem. Check which major version this component was built for,
/// if it isn't the current major version, fail to load it. Always just returns a constant,
/// recompiling will often be enough to upgrade. `virtual` and `final` to be the vtable, but it
/// can't be overridden because it is a constant.
virtual int supportedVersion() const final
{
return OMP_VERSION_SUPPORTED;
}
/// Get the component's name
virtual StringView componentName() const = 0;
/// Get the component's type
virtual ComponentType componentType() const { return ComponentType::Other; }
/// Get the component's version
virtual SemanticVersion componentVersion() const = 0;
/// Called for every component to load it
/// Should be used for caching the core interface, registering player/core event handlers
virtual void onLoad(ICore* c) = 0;
/// Called when all components have been loaded
/// Should be used for querying other components, registering their event handlers
/// Should NOT be used for interacting with other components or firing events
/// @param components Tcomponentgins list to query
virtual void onInit(IComponentList* components) { }
/// Called when all components have been initialised
/// Should be used for interacting with other components, firing events, or any more complex logic
virtual void onReady() { }
/// Called before the components are about to be freed
/// Should be used for disconnecting
virtual void onFree(IComponent* component) { }
/// Fill a configuration object with custom configuration
/// @param defaults Whether to only provide default values. True when generating the config file
virtual void provideConfiguration(ILogger& logger, IEarlyConfig& config, bool defaults) { }
/// Frees the component data
virtual void free() = 0;
/// Reset the component data (on GMX)
virtual void reset() = 0;
};
struct IComponentList : public IExtensible
{
/// Query a component by its ID
/// @param id The UID of the component
/// @return A pointer to the component or nullptr if not available
virtual IComponent* queryComponent(UID id) = 0;
/// Query a component by its type
/// @typeparam ComponentT The component type, must derive from IComponent
template <class ComponentT>
ComponentT* queryComponent()
{
static_assert(std::is_base_of<IComponent, ComponentT>::value, "queryComponent parameter must inherit from IComponent");
return static_cast<ComponentT*>(queryComponent(ComponentT::IID));
}
};