8#ifndef MODULE_MANAGER_HPP
9#define MODULE_MANAGER_HPP
16#include <unordered_map>
20#include "IModuleRegistry.hpp"
50template <std::derived_from<IModule>... Ts>
52 private Stride<std::unique_ptr<SharedLibrary>, Ts *...> {
61 using typename Base::Entity;
72 bool Load(
const std::string &path,
const std::string &key) {
73 if (_byKey.count(key))
75 auto lib = std::make_unique<SharedLibrary>(path);
77 Entity e = this->
add(std::move(lib));
78 (Discover<Ts>(ref, e), ...);
79 _byKey.emplace(key, e);
89 void Unload(
const std::string &key) {
90 auto it = _byKey.find(key);
92 if (it == _byKey.end())
104 std::optional<Entity>
Find(
const std::string &key)
const {
105 auto it = _byKey.find(key);
107 return it == _byKey.end() ? std::nullopt : std::optional<Entity>(it->second);
120 template <
typename T>
121 T *
Get(
const std::string &key) {
123 return e ?
Get<T>(*e) :
nullptr;
133 template <
typename T>
135 auto &slot = this->
template at<T *>(e);
136 return slot ? *slot :
nullptr;
147 return id ?
id->get() :
nullptr;
156 template <
typename T>
158 std::vector<T *> out;
162 out.push_back(*slot);
176 std::vector<IModule *>
GetAllByKey(
const std::string &key)
override {
177 std::vector<IModule *> out;
182 this->
row(*e, [&](
auto &slot) {
184 out.push_back(
static_cast<IModule *
>(*slot));
198 std::vector<IModule *>
GetAll()
override {
199 std::vector<IModule *> out;
201 for (
const auto &[key, e] : _byKey)
202 this->
row(e, [&](
auto &slot) {
204 out.push_back(
static_cast<IModule *
>(*slot));
224 IModule *
Get(
const std::string &type,
const std::string &key)
override {
226 if (type == module->type())
233 const auto found = _current.find(type);
235 return found == _current.end() ? nullptr : found->second;
241 _current[type] =
module;
243 _current.erase(type);
252 std::vector<IModule *>
GetAllByType(
const std::string &type)
override {
253 std::vector<IModule *> found;
256 if (type == module->type())
257 found.push_back(module);
263 template <
typename T>
265 auto entry = lib.
symbol<T *(*)()>(T::entry);
278 std::unordered_map<std::string, Entity> _byKey;
281 std::map<std::string, IModule *> _current;
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