P-E-R-R-Y
Utilitaires, contrats et vendors du moteur, en une seule doc
Loading...
Searching...
No Matches
ModuleManager.hpp
Go to the documentation of this file.
1
7
8#ifndef MODULE_MANAGER_HPP
9#define MODULE_MANAGER_HPP
10
11#include <concepts>
12#include <map>
13#include <memory>
14#include <optional>
15#include <string>
16#include <unordered_map>
17#include <vector>
18
19#include "IModule.hpp"
20#include "IModuleRegistry.hpp"
21#include "SharedLibrary.hpp"
22#include "Stride.hpp"
23
24
50template <std::derived_from<IModule>... Ts>
51class ModuleManager : public IModuleRegistry,
52 private Stride<std::unique_ptr<SharedLibrary>, Ts *...> {
53 using Base = Stride<std::unique_ptr<SharedLibrary>, Ts *...>;
54
55 public:
56 /* Inheritance is PRIVATE : Stride is the substrate, not the API.
57 * Public it would let a caller add() a row with no dll, remove() one
58 * without clearing its columns, or set() a cell by hand - breaking
59 * the invariant Load/Unload exist to hold. Re-export anything that
60 * turns out to be genuinely needed, one `using` at a time. */
61 using typename Base::Entity;
62
72 bool Load(const std::string &path, const std::string &key) {
73 if (_byKey.count(key))
74 return false;
75 auto lib = std::make_unique<SharedLibrary>(path);
76 SharedLibrary &ref = *lib;
77 Entity e = this->add(std::move(lib));
78 (Discover<Ts>(ref, e), ...);
79 _byKey.emplace(key, e);
80 return true;
81 }
82
89 void Unload(const std::string &key) {
90 auto it = _byKey.find(key);
91
92 if (it == _byKey.end())
93 return;
94 this->remove(it->second); /* clears columns AND identity -> closes the dll */
95 _byKey.erase(it);
96 }
97
104 std::optional<Entity> Find(const std::string &key) const {
105 auto it = _byKey.find(key);
106
107 return it == _byKey.end() ? std::nullopt : std::optional<Entity>(it->second);
108 }
109
120 template <typename T>
121 T *Get(const std::string &key) {
122 auto e = Find(key);
123 return e ? Get<T>(*e) : nullptr;
124 }
125
133 template <typename T>
134 T *Get(Entity e) {
135 auto &slot = this->template at<T *>(e);
136 return slot ? *slot : nullptr;
137 }
138
145 SharedLibrary *Get(Entity e) {
146 auto &id = this->identity(e);
147 return id ? id->get() : nullptr;
148 }
149
156 template <typename T>
157 std::vector<T *> GetAll() {
158 std::vector<T *> out;
159
160 for (auto &slot : this->template column<T *>())
161 if (slot)
162 out.push_back(*slot);
163 return out;
164 }
165
176 std::vector<IModule *> GetAllByKey(const std::string &key) override {
177 std::vector<IModule *> out;
178 auto e = Find(key);
179
180 if (!e)
181 return out;
182 this->row(*e, [&](auto &slot) {
183 if (slot)
184 out.push_back(static_cast<IModule *>(*slot));
185 });
186 return out;
187 }
188
198 std::vector<IModule *> GetAll() override {
199 std::vector<IModule *> out;
200
201 for (const auto &[key, e] : _byKey)
202 this->row(e, [&](auto &slot) {
203 if (slot)
204 out.push_back(static_cast<IModule *>(*slot));
205 });
206 return out;
207 }
208
224 IModule *Get(const std::string &type, const std::string &key) override {
225 for (IModule *module : GetAllByKey(key))
226 if (type == module->type())
227 return module;
228 return nullptr;
229 }
230
232 IModule *Current(const std::string &type) override {
233 const auto found = _current.find(type);
234
235 return found == _current.end() ? nullptr : found->second;
236 }
237
239 void Select(const std::string &type, IModule *module) override {
240 if (module)
241 _current[type] = module;
242 else
243 _current.erase(type);
244 }
245
252 std::vector<IModule *> GetAllByType(const std::string &type) override {
253 std::vector<IModule *> found;
254
255 for (IModule *module : GetAll())
256 if (type == module->type())
257 found.push_back(module);
258 return found;
259 }
260
261 private:
263 template <typename T>
264 void Discover(SharedLibrary &lib, Entity e) {
265 auto entry = lib.symbol<T *(*)()>(T::entry);
266
267 if (!entry)
268 return;
269
270 T *module = entry();
271
272 // le registre avant la mise en table, comme IModuleManager
273 module->bind(*this);
274 this->template set<T *>(e, module);
275 }
276
278 std::unordered_map<std::string, Entity> _byKey;
279
281 std::map<std::string, IModule *> _current;
282};
283
285
286#endif /* !MODULE_MANAGER_HPP */
Ce qu'un module dit de lui-meme, et l'etat de sa bibliotheque.
Ce que tout module chargeable sait dire de lui-meme.
Definition IModule.hpp:24
A Stride<SharedLibrary, Ts...> : a loaded dll IS a row, a contract (Ts...) IS a column,...
Definition ModuleManager.hpp:52
void Select(const std::string &type, IModule *module) override
L'hote declare son choix. Un seul par contrat.
Definition ModuleManager.hpp:239
IModule * Current(const std::string &type) override
Le module en service pour ce contrat, ou nullptr.
Definition ModuleManager.hpp:232
std::vector< IModule * > GetAllByKey(const std::string &key) override
Every module the library named by this key provides.
Definition ModuleManager.hpp:176
T * Get(const std::string &key)
The T module of the library named by this key, or nullptr.
Definition ModuleManager.hpp:121
std::vector< IModule * > GetAllByType(const std::string &type) override
La ligne, par chaine. Impose par IModuleRegistry.
Definition ModuleManager.hpp:252
T * Get(Entity e)
The T module of this exact row, or nullptr.
Definition ModuleManager.hpp:134
bool Load(const std::string &path, const std::string &key)
Opens the dll, adds its row, fills the columns it provides.
Definition ModuleManager.hpp:72
std::vector< IModule * > GetAll() override
Every module of every loaded library.
Definition ModuleManager.hpp:198
std::vector< T * > GetAll()
Every library providing T.
Definition ModuleManager.hpp:157
IModule * Get(const std::string &type, const std::string &key) override
La case, par deux chaines. Impose par IModuleRegistry.
Definition ModuleManager.hpp:224
void Unload(const std::string &key)
Clears this row's columns AND its identity, which closes the dll. One call, nothing left to tidy up.
Definition ModuleManager.hpp:89
SharedLibrary * Get(Entity e)
The SharedLibrary of this exact row, or nullptr.
Definition ModuleManager.hpp:145
std::optional< Entity > Find(const std::string &key) const
The row named by this key, or nothing.
Definition ModuleManager.hpp:104
Encapsule le handle d'une bibliotheque dynamique. Rien de plus.
Definition SharedLibrary.hpp:25
T symbol(const char *name) const
Definition SharedLibrary.hpp:65
A two-axis table : rows are a dense index (0,1,2... reused, like ecs Entity), columns are Columns....
Definition Stride.hpp:41
void set(Entity e, T value)
Definition Stride.hpp:104
Entity add(std::unique_ptr< SharedLibrary > identity)
Definition Stride.hpp:63
void row(Entity e, F &&f)
Definition Stride.hpp:136
void remove(Entity e)
Definition Stride.hpp:82
std::optional< T > & at(Entity e)
Definition Stride.hpp:115
std::vector< std::optional< T > > & column()
Definition Stride.hpp:125
std::optional< std::unique_ptr< SharedLibrary > > & identity(Entity e)
Definition Stride.hpp:93