8#ifndef IMODULE_MANAGER_HPP
9#define IMODULE_MANAGER_HPP
14#include <unordered_map>
18#include "IModuleRegistry.hpp"
84 static constexpr const char *
entry =
"getModules";
102 bool Load(
const std::string &path,
const std::string &key) {
107 if (_columnOf.count(key))
110 auto library = std::make_unique<SharedLibrary>(path);
118 if (!modules || !*modules)
121 const size_t column = _cells.size();
123 _cells.emplace_back(_rows.size(),
nullptr);
124 for (
IModule **it = modules; *it; it++) {
128 const size_t row = rowOf((*it)->type());
139 _cells[column][row] = *it;
142 _columnOf.emplace(key, column);
143 _columnNames.push_back(key);
144 _columns.push_back(std::move(library));
145 _condemned.push_back(
false);
156 IModule *
Get(
const std::string &type,
const std::string &key)
override {
157 const auto row = _rowOf.find(type);
158 const auto column = _columnOf.find(key);
160 if (row == _rowOf.end() || column == _columnOf.end())
162 return _cells[column->second][row->second];
176 std::vector<IModule *>
GetAllByType(
const std::string &type)
override {
177 std::vector<IModule *> found;
178 const auto row = _rowOf.find(type);
180 if (row == _rowOf.end())
182 for (
const std::vector<IModule *> &column : _cells)
183 if (
IModule *module = column[row->second])
184 found.push_back(module);
198 std::vector<IModule *>
GetAllByKey(
const std::string &key)
override {
199 std::vector<IModule *> found;
200 const auto column = _columnOf.find(key);
202 if (column == _columnOf.end())
204 for (
IModule *module : _cells[column->second])
206 found.push_back(module);
215 std::vector<IModule *>
GetAll()
override {
216 std::vector<IModule *> found;
218 for (
const std::vector<IModule *> &column : _cells)
221 found.push_back(module);
239 const auto column = _columnOf.find(key);
241 if (column == _columnOf.end())
244 _condemned[column->second] =
true;
245 for (
IModule *module : _cells[column->second])
263 for (
size_t column = 0; column < _cells.size(); ) {
264 if (!_condemned[column] || !isFree(column)) {
273 for (
auto it = _current.begin(); it != _current.end(); )
274 it = contains(column, it->second) ? _current.erase(it) : std::next(it);
276 _cells.erase(_cells.begin() + column);
277 _columnNames.erase(_columnNames.begin() + column);
278 _condemned.erase(_condemned.begin() + column);
279 _columns.erase(_columns.begin() + column);
289 const auto found = _current.find(type);
291 return found == _current.end() ? nullptr : found->second;
297 _current[type] =
module;
299 _current.erase(type);
324 template <
typename T>
325 T *
Get(
const std::string &key) {
326 for (
const char *
const *type = T::accepts; *type; type++)
328 return static_cast<T *
>(module);
333 template <
typename T>
335 std::vector<T *> found;
337 for (
const char *
const *type = T::accepts; *type; type++)
339 found.push_back(
static_cast<T *
>(module));
344 template <
typename T>
346 return static_cast<T *
>(
Current(T::contract));
350 template <
typename T>
352 Select(T::contract, module);
356 const std::vector<std::string> &
GetTypes()
const {
return _rows; }
359 const std::vector<std::string> &
GetKeys()
const {
return _columnNames; }
363 bool contains(
size_t column,
IModule *module)
const {
364 for (
IModule *cell : _cells[column])
365 if (cell && cell == module)
371 bool isFree(
size_t column)
const {
372 for (IModule *module : _cells[column])
381 for (
size_t column = 0; column < _columnNames.size(); column++)
382 _columnOf.emplace(_columnNames[column], column);
392 size_t rowOf(
const std::string &type) {
393 const auto found = _rowOf.find(type);
395 if (found != _rowOf.end())
396 return found->second;
398 const size_t row = _rows.size();
400 _rowOf.emplace(type, row);
401 _rows.push_back(type);
402 for (std::vector<IModule *> &column : _cells)
403 column.resize(row + 1,
nullptr);
409 std::vector<std::vector<IModule *>> _cells;
410 std::vector<std::unique_ptr<SharedLibrary>> _columns;
411 std::vector<bool> _condemned;
412 std::map<std::string, IModule *> _current;
414 std::vector<std::string> _rows;
415 std::vector<std::string> _columnNames;
416 std::unordered_map<std::string, size_t> _rowOf;
417 std::unordered_map<std::string, size_t> _columnOf;
Ce qu'un module dit de lui-meme, et l'etat de sa bibliotheque.
La meme table que ModuleManager, mais dont les DEUX axes sont des chaines connues seulement au charge...
Definition IModuleManager.hpp:58
void Unload(const std::string &key)
Condamne une colonne. NE FERME RIEN.
Definition IModuleManager.hpp:238
IModule * Current(const std::string &type) override
Le module en service pour ce contrat, ou nullptr.
Definition IModuleManager.hpp:288
size_t Reconcile()
Ferme ce qui est condamne et que plus personne ne tient.
Definition IModuleManager.hpp:260
bool Load(const std::string &path, const std::string &key)
Ouvre la dll, appelle getModules(), ajoute une colonne.
Definition IModuleManager.hpp:102
const std::vector< std::string > & GetTypes() const
Lignes, dans l'ordre de decouverte.
Definition IModuleManager.hpp:356
std::vector< IModule * > GetAllByKey(const std::string &key) override
La COLONNE : tout ce que ce fournisseur apporte.
Definition IModuleManager.hpp:198
void Select(T *module)
L'hote declare son choix pour le contrat de T.
Definition IModuleManager.hpp:351
const std::vector< std::string > & GetKeys() const
Les colonnes, dans l'ordre de chargement.
Definition IModuleManager.hpp:359
std::vector< T * > GetAllByType()
Tous ceux qui remplissent ce contrat, ou mieux.
Definition IModuleManager.hpp:334
~IModuleManager() override
Definition IModuleManager.hpp:86
std::vector< IModule * > GetAll() override
Toute la table, colonne par colonne.
Definition IModuleManager.hpp:215
IModule * Get(const std::string &type, const std::string &key) override
La case : ce type, chez ce fournisseur. O(1).
Definition IModuleManager.hpp:156
std::vector< IModule * > GetAllByType(const std::string &type) override
La LIGNE : tous ceux qui declarent ce type.
Definition IModuleManager.hpp:176
static constexpr const char * entry
Le seul symbole que la dll doit exporter.
Definition IModuleManager.hpp:84
T * Get(const std::string &key)
Ce contrat chez ce fournisseur, ou tout ce qui le satisfait.
Definition IModuleManager.hpp:325
void Select(const std::string &type, IModule *module) override
L'hote declare son choix. Un seul par type.
Definition IModuleManager.hpp:295
T * Current()
Le module en service pour ce contrat, vu comme un T.
Definition IModuleManager.hpp:345
Ce que tout module chargeable sait dire de lui-meme.
Definition IModule.hpp:24
bool isClosed() const
Plus aucun detenteur.
Definition IModule.hpp:77