MoCSI API Reference
Loading...
Searching...
No Matches
ModuleFactory.h
Go to the documentation of this file.
1#ifndef MODULE_FACTORY_H
2#define MODULE_FACTORY_H
3
4#include <iostream>
5#include <map>
6#include <memory>
7#include <string>
8#include <unordered_map>
9
11#include "GenericSubmodule.h"
12#include "SimulationClassBase.h"
13
20template <typename T>
22{
23 public:
25 std::shared_ptr<GenericManagingModule<T>> (*)(SimulationClassBase<T>* sim);
27 std::shared_ptr<GenericSubmodule<T>> (*)(SimulationClassBase<T>* sim);
28
29 // No constructor, as only static functions/variables will be used
30 ModuleFactory() = delete;
31
32 static constexpr bool registerModule(std::string name, creation_method module) noexcept;
33 static constexpr bool registerModule(std::string name,
35
36 static std::shared_ptr<GenericManagingModule<T>> create(const std::string& name,
38 static std::vector<std::shared_ptr<GenericManagingModule<T>>> createAllManagingModules(
40 static std::vector<std::shared_ptr<GenericSubmodule<T>>> createAllSubmodules(
42 static std::shared_ptr<GenericSubmodule<T>> createSubmodule(const std::string& name,
44
45 private:
46 static std::unordered_map<std::string, creation_method_submodules> m_registry_submodules;
47 static std::unordered_map<std::string, creation_method> m_registry;
48};
49
50// Declaration of registry variables needed here, because they are static
51template <typename T>
52std::unordered_map<std::string,
53 std::shared_ptr<GenericManagingModule<T>> (*)(SimulationClassBase<T>* sim)>
55
56template <typename T>
57std::unordered_map<std::string,
58 std::shared_ptr<GenericSubmodule<T>> (*)(SimulationClassBase<T>* sim)>
60
67template <typename T>
68bool constexpr ModuleFactory<T>::registerModule(const std::string name,
69 creation_method module) noexcept
70{
71 // If the module is not yet registered, do that and return true
72 if (auto current_module = m_registry.find(name); current_module == m_registry.end())
73 {
74 m_registry.emplace(name, module);
75 // Only print this message to the log in release build
76 std::cout << "Registering: " << name << "\n";
77 return true;
78 }
79 return false;
80}
81
87template <typename T>
88std::shared_ptr<GenericManagingModule<T>> ModuleFactory<T>::create(const std::string& name,
90{
91 // If the module was registered, then create it.
92 if (auto current_module = m_registry.find(name); current_module != m_registry.end())
93 {
94 return current_module->second(sim);
95 }
96 throw std::runtime_error("ModuleFactory: No module registered with name: " + name);
97}
98
105template <typename T>
106bool constexpr ModuleFactory<T>::registerModule(const std::string name,
108{
109 // If the module is not yet registered, do that and return true
110 if (auto current_module = m_registry_submodules.find(name);
111 current_module == m_registry_submodules.end())
112 {
113 m_registry_submodules.emplace(name, module);
114 // Only print this message to the log in release build
115 std::cout << "Registering submodule: " << name << "\n";
116 return true;
117 }
118 return false;
119}
120
126template <typename T>
127std::shared_ptr<GenericSubmodule<T>> ModuleFactory<T>::createSubmodule(const std::string& name,
129{
130 // std::cout << "Create submodule " + name + "\n";
131 // If the module was registered, then create it.
132 if (auto current_module = m_registry_submodules.find(name);
133 current_module != m_registry_submodules.end())
134 {
135 return current_module->second(sim);
136 }
137 throw std::runtime_error("ModuleFactory: No module registered with name: " + name);
138}
139
145template <typename T>
146std::vector<std::shared_ptr<GenericManagingModule<T>>> ModuleFactory<T>::createAllManagingModules(
148{
149 std::vector<std::shared_ptr<GenericManagingModule<T>>> all_loaded_modules{};
150
151 all_loaded_modules.reserve(m_registry.size());
152 for (auto& [key, module] : m_registry)
153 {
155 }
156 return all_loaded_modules;
157}
158
164template <typename T>
165std::vector<std::shared_ptr<GenericSubmodule<T>>> ModuleFactory<T>::createAllSubmodules(
167{
168 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_loaded_submodules{};
169
170 for (auto& [key, submodule] : m_registry_submodules)
171 {
173 }
175}
176
177#endif
Error class to throw when a module chain has circular or missing dependencies. Inherits from std::exc...
Definition ChainManager.h:28
Class to register and generate every module compiled into the executable. Has static auto-registry fu...
Definition ModuleFactory.h:22
std::shared_ptr< GenericManagingModule< T > >(*)(SimulationClassBase< T > *sim) creation_method
Definition ModuleFactory.h:25
static constexpr bool registerModule(std::string name, creation_method module) noexcept
Function that adds a module to the module registry map.
Definition ModuleFactory.h:68
static std::shared_ptr< GenericSubmodule< T > > createSubmodule(const std::string &name, SimulationClassBase< T > *sim)
Creates a submodule, if a submodule with the corresponding name has been registered.
Definition ModuleFactory.h:127
static std::vector< std::shared_ptr< GenericSubmodule< T > > > createAllSubmodules(SimulationClassBase< T > *sim_pointer)
Creates every submodule in the submodule registry.
Definition ModuleFactory.h:165
static std::shared_ptr< GenericManagingModule< T > > create(const std::string &name, SimulationClassBase< T > *sim)
Creates a module, if a module with the corresponding name has been registered.
Definition ModuleFactory.h:88
ModuleFactory()=delete
std::shared_ptr< GenericSubmodule< T > >(*)(SimulationClassBase< T > *sim) creation_method_submodules
Definition ModuleFactory.h:27
static std::vector< std::shared_ptr< GenericManagingModule< T > > > createAllManagingModules(SimulationClassBase< T > *sim_pointer)
Creates every module in the module registry.
Definition ModuleFactory.h:146