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::vector<std::shared_ptr<GenericManagingModule<T>>>
45 const std::vector<std::string>& list_of_mananging_modules_to_load);
46 static std::vector<std::shared_ptr<GenericSubmodule<T>>> createAllSelectedSubmodules(
48 const std::vector<std::shared_ptr<GenericManagingModule<T>>>& loaded_managing_modules);
49 static std::shared_ptr<GenericSubmodule<T>> createSubmodule(const std::string& name,
51
52 private:
53 static std::unordered_map<std::string, creation_method_submodules> m_registry_submodules;
54 static std::unordered_map<std::string, creation_method> m_registry;
55};
56
57// Declaration of registry variables needed here, because they are static
58template <typename T>
59std::unordered_map<std::string,
60 std::shared_ptr<GenericManagingModule<T>> (*)(SimulationClassBase<T>* sim)>
62
63template <typename T>
64std::unordered_map<std::string,
65 std::shared_ptr<GenericSubmodule<T>> (*)(SimulationClassBase<T>* sim)>
67
74template <typename T>
75bool constexpr ModuleFactory<T>::registerModule(const std::string name,
76 creation_method module) noexcept
77{
78 // If the module is not yet registered, do that and return true
79 if (auto current_module = m_registry.find(name); current_module == m_registry.end())
80 {
81 m_registry.emplace(name, module);
82 // Only print this message to the log in release build
83 std::cout << "Registering: " << name << "\n";
84 return true;
85 }
86 return false;
87}
88
94template <typename T>
95std::shared_ptr<GenericManagingModule<T>> ModuleFactory<T>::create(const std::string& name,
97{
98 // If the module was registered, then create it.
99 if (auto current_module = m_registry.find(name); current_module != m_registry.end())
100 {
101 return current_module->second(sim);
102 }
103 throw std::runtime_error("ModuleFactory: No module registered with name: " + name);
104}
105
112template <typename T>
113bool constexpr ModuleFactory<T>::registerModule(const std::string name,
115{
116 // If the module is not yet registered, do that and return true
117 if (auto current_module = m_registry_submodules.find(name);
118 current_module == m_registry_submodules.end())
119 {
120 m_registry_submodules.emplace(name, module);
121 // Only print this message to the log in release build
122 std::cout << "Registering submodule: " << name << "\n";
123 return true;
124 }
125 return false;
126}
127
133template <typename T>
134std::shared_ptr<GenericSubmodule<T>> ModuleFactory<T>::createSubmodule(const std::string& name,
136{
137 // std::cout << "Create submodule " + name + "\n";
138 // If the module was registered, then create it.
139 if (auto current_module = m_registry_submodules.find(name);
140 current_module != m_registry_submodules.end())
141 {
142 return current_module->second(sim);
143 }
144 throw std::runtime_error("ModuleFactory: No module registered with name: " + name);
145}
146
152template <typename T>
153std::vector<std::shared_ptr<GenericManagingModule<T>>> ModuleFactory<T>::createAllManagingModules(
155{
156 std::vector<std::shared_ptr<GenericManagingModule<T>>> all_loaded_modules{};
157
158 all_loaded_modules.reserve(m_registry.size());
159 for (auto& [key, module] : m_registry)
160 {
162 }
163 return all_loaded_modules;
164}
165
171template <typename T>
172std::vector<std::shared_ptr<GenericSubmodule<T>>> ModuleFactory<T>::createAllSubmodules(
174{
175 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_loaded_submodules{};
176
177 for (auto& [key, submodule] : m_registry_submodules)
178 {
180 }
182}
183
193template <typename T>
194std::vector<std::shared_ptr<GenericManagingModule<T>>>
197 const std::vector<std::string>& list_of_mananging_modules_to_load)
198{
199 std::unordered_set<std::string> modules_to_load_set(list_of_mananging_modules_to_load.begin(),
201 std::vector<std::shared_ptr<GenericManagingModule<T>>> all_loaded_modules{};
202
203 for (auto& [key, module] : m_registry)
204 {
205 if (modules_to_load_set.contains(key))
206 {
208 std::cout << "Loaded module: " << key << "\n";
209 }
210 }
211 return all_loaded_modules;
212}
213
224template <typename T>
225std::vector<std::shared_ptr<GenericSubmodule<T>>> ModuleFactory<T>::createAllSelectedSubmodules(
227 const std::vector<std::shared_ptr<GenericManagingModule<T>>>& loaded_managing_modules)
228{
229 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_loaded_submodules{};
230
231 std::unordered_set<std::string> unique_submodule_names_set;
232
233 for (auto const& module_pointer : loaded_managing_modules)
234 {
235 for (auto const& submodule_name : module_pointer->getSubmoduleList())
236 {
237 all_loaded_submodules.emplace_back(m_registry_submodules[submodule_name](sim_pointer));
239 std::cout << "Loaded submodule: " << submodule_name << "\n";
240 }
241 }
242
243 std::vector<std::string> last_added_submodules(unique_submodule_names_set.begin(),
245
246 // Check iteratively the sub-submodules to complete the loaded submodules list.
247 while (!last_added_submodules.empty())
248 {
249 std::vector<std::string> newly_added;
251 {
252 std::string submodule_name{submodule_pointer->getNameLocal()};
254 {
255 for (auto& submodule_name : submodule_pointer->getSubmoduleList())
256 {
258 {
259 newly_added.push_back(submodule_name);
260 all_loaded_submodules.emplace_back(
261 m_registry_submodules[submodule_name](sim_pointer));
262 std::cout << "Loaded submodule: " << submodule_name << "\n";
263 }
264 }
265 }
266 }
268 }
269
271}
272#endif
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:35
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:92
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 std::vector< std::shared_ptr< GenericManagingModule< T > > > createAllSelectedManagingModules(SimulationClassBase< T > *sim_pointer, const std::vector< std::string > &list_of_mananging_modules_to_load)
Only creates instances of managing modules that have been specified by the user. (This allows only ha...
Definition ModuleFactory.h:195
static constexpr bool registerModule(std::string name, creation_method module) noexcept
Function that adds a module to the module registry map.
Definition ModuleFactory.h:75
static std::vector< std::shared_ptr< GenericSubmodule< T > > > createAllSelectedSubmodules(SimulationClassBase< T > *sim_pointer, const std::vector< std::shared_ptr< GenericManagingModule< T > > > &loaded_managing_modules)
Only creates submodules that are used within the simulation. Iteratively requests the submodule list ...
Definition ModuleFactory.h:225
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:134
static std::vector< std::shared_ptr< GenericSubmodule< T > > > createAllSubmodules(SimulationClassBase< T > *sim_pointer)
Creates every submodule in the submodule registry.
Definition ModuleFactory.h:172
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:95
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:153