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 std::cerr << "[ModuleFactory]: No module registered with name: " << name;
104 throw std::runtime_error(
105 static_cast<std::string>("[ModuleFactory]: No module registered with name: ") + name);
106}
107
114template <typename T>
115bool constexpr ModuleFactory<T>::registerModule(const std::string name,
117{
118 // If the module is not yet registered, do that and return true
119 if (auto current_module = m_registry_submodules.find(name);
120 current_module == m_registry_submodules.end())
121 {
122 m_registry_submodules.emplace(name, module);
123 // Only print this message to the log in release build
124 std::cout << "Registering submodule: " << name << "\n";
125 return true;
126 }
127 return false;
128}
129
135template <typename T>
136std::shared_ptr<GenericSubmodule<T>> ModuleFactory<T>::createSubmodule(const std::string& name,
138{
139 // std::cout << "Create submodule " + name + "\n";
140 // If the module was registered, then create it.
141 if (auto current_module = m_registry_submodules.find(name);
142 current_module != m_registry_submodules.end())
143 {
144 return current_module->second(sim);
145 }
146 std::cerr << "[ModuleFactory]: No submodule registered with name: " << name;
147 throw std::runtime_error(
148 static_cast<std::string>("[ModuleFactory]: No submodule registered with name: ") + name);
149}
150
156template <typename T>
157std::vector<std::shared_ptr<GenericManagingModule<T>>> ModuleFactory<T>::createAllManagingModules(
159{
160 std::vector<std::shared_ptr<GenericManagingModule<T>>> all_loaded_modules{};
161
162 all_loaded_modules.reserve(m_registry.size());
163 for (auto& [key, module] : m_registry)
164 {
166 }
167 return all_loaded_modules;
168}
169
175template <typename T>
176std::vector<std::shared_ptr<GenericSubmodule<T>>> ModuleFactory<T>::createAllSubmodules(
178{
179 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_loaded_submodules{};
180
181 for (auto& [key, submodule] : m_registry_submodules)
182 {
184 }
186}
187
197template <typename T>
198std::vector<std::shared_ptr<GenericManagingModule<T>>>
201 const std::vector<std::string>& list_of_mananging_modules_to_load)
202{
203 std::unordered_set<std::string> modules_to_load_set(list_of_mananging_modules_to_load.begin(),
205 std::vector<std::shared_ptr<GenericManagingModule<T>>> all_loaded_modules{};
206
207 for (auto& [key, module] : m_registry)
208 {
209 if (modules_to_load_set.contains(key))
210 {
212 }
213 }
214 return all_loaded_modules;
215}
216
227template <typename T>
228std::vector<std::shared_ptr<GenericSubmodule<T>>> ModuleFactory<T>::createAllSelectedSubmodules(
230 const std::vector<std::shared_ptr<GenericManagingModule<T>>>& loaded_managing_modules)
231{
232 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_loaded_submodules{};
233
234 std::unordered_set<std::string> unique_submodule_names_set;
235
236 for (auto const& module_pointer : loaded_managing_modules)
237 {
238 for (auto const& submodule_name : module_pointer->getSubmoduleList())
239 {
240 all_loaded_submodules.emplace_back(m_registry_submodules[submodule_name](sim_pointer));
242 }
243 }
244
245 std::vector<std::string> last_added_submodules(unique_submodule_names_set.begin(),
247
248 // Check iteratively the sub-submodules to complete the loaded submodules list.
249 while (!last_added_submodules.empty())
250 {
251 std::vector<std::string> newly_added;
253 {
254 std::string submodule_name{submodule_pointer->getNameLocal()};
256 {
257 for (auto& submodule_name : submodule_pointer->getSubmoduleList())
258 {
260 {
261 newly_added.push_back(submodule_name);
262 all_loaded_submodules.emplace_back(
263 m_registry_submodules[submodule_name](sim_pointer));
264 }
265 }
266 }
267 }
269 }
270
272}
273#endif
Concrete implementation of a matrix class representing a Compressed Sparse Rows (CSR) matrix....
Definition CsrMatrix.h:37
CsrSparseMatrix()
Constructor for an empty CsrSparseMatrix object. Leaves all storage arrays empty but sets the flag to...
Definition CsrMatrix.h:94
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:199
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:228
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:136
static std::vector< std::shared_ptr< GenericSubmodule< T > > > createAllSubmodules(SimulationClassBase< T > *sim_pointer)
Creates every submodule in the submodule registry.
Definition ModuleFactory.h:176
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:157