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 <algorithm>
5#include <iostream>
6#include <map>
7#include <memory>
8#include <string>
9#include <unordered_map>
10
12#include "GenericSubmodule.h"
13#include "SimulationClassBase.h"
14
21template <typename T>
23{
24 public:
26 std::shared_ptr<GenericManagingModule<T>> (*)(SimulationClassBase<T>* sim);
28 std::shared_ptr<GenericSubmodule<T>> (*)(SimulationClassBase<T>* sim);
29
30 // No constructor, as only static functions/variables will be used
31 ModuleFactory() = delete;
32
33 static constexpr bool registerModule(std::string name, creation_method module) noexcept;
34 static constexpr bool registerModule(std::string name,
36
37 static std::shared_ptr<GenericManagingModule<T>> create(const std::string& name,
39 static std::vector<std::shared_ptr<GenericManagingModule<T>>> createAllManagingModules(
41 static std::vector<std::shared_ptr<GenericSubmodule<T>>> createAllSubmodules(
43 static std::vector<std::shared_ptr<GenericManagingModule<T>>>
46 const std::vector<std::string>& list_of_mananging_modules_to_load);
47 static std::vector<std::shared_ptr<GenericSubmodule<T>>> createAllSelectedSubmodules(
49 const std::vector<std::shared_ptr<GenericManagingModule<T>>>& loaded_managing_modules);
50 static std::shared_ptr<GenericSubmodule<T>> createSubmodule(const std::string& name,
52
53 private:
54 static std::unordered_map<std::string, creation_method_submodules> m_registry_submodules;
55 static std::unordered_map<std::string, creation_method> m_registry;
56};
57
58// Declaration of registry variables needed here, because they are static
59template <typename T>
60std::unordered_map<std::string,
61 std::shared_ptr<GenericManagingModule<T>> (*)(SimulationClassBase<T>* sim)>
63
64template <typename T>
65std::unordered_map<std::string,
66 std::shared_ptr<GenericSubmodule<T>> (*)(SimulationClassBase<T>* sim)>
68
75template <typename T>
76bool constexpr ModuleFactory<T>::registerModule(const std::string name,
77 creation_method module) noexcept
78{
79 // If the module is not yet registered, do that and return true
80 if (auto current_module = m_registry.find(name); current_module == m_registry.end())
81 {
82 m_registry.emplace(name, module);
83 // Only print this message to the log in release build
84 std::cout << "Registering: " << name << "\n";
85 return true;
86 }
87 return false;
88}
89
95template <typename T>
96std::shared_ptr<GenericManagingModule<T>> ModuleFactory<T>::create(const std::string& name,
98{
99 // If the module was registered, then create it.
100 if (auto current_module = m_registry.find(name); current_module != m_registry.end())
101 {
102 return current_module->second(sim);
103 }
104 std::cerr << "[ModuleFactory]: No module registered with name: " << name;
105 throw std::runtime_error(
106 static_cast<std::string>("[ModuleFactory]: No module registered with name: ") + name);
107}
108
115template <typename T>
116bool constexpr ModuleFactory<T>::registerModule(const std::string name,
118{
119 // If the module is not yet registered, do that and return true
120 if (auto current_module = m_registry_submodules.find(name);
121 current_module == m_registry_submodules.end())
122 {
123 m_registry_submodules.emplace(name, module);
124 // Only print this message to the log in release build
125 std::cout << "Registering submodule: " << name << "\n";
126 return true;
127 }
128 return false;
129}
130
136template <typename T>
137std::shared_ptr<GenericSubmodule<T>> ModuleFactory<T>::createSubmodule(const std::string& name,
139{
140 // std::cout << "Create submodule " + name + "\n";
141 // If the module was registered, then create it.
142 if (auto current_module = m_registry_submodules.find(name);
143 current_module != m_registry_submodules.end())
144 {
145 return current_module->second(sim);
146 }
147 std::cerr << "[ModuleFactory]: No submodule registered with name: " << name;
148 throw std::runtime_error(
149 static_cast<std::string>("[ModuleFactory]: No submodule registered with name: ") + name);
150}
151
157template <typename T>
158std::vector<std::shared_ptr<GenericManagingModule<T>>> ModuleFactory<T>::createAllManagingModules(
160{
161 std::vector<std::shared_ptr<GenericManagingModule<T>>> all_loaded_modules{};
162
163 all_loaded_modules.reserve(m_registry.size());
164 for (auto& [key, module] : m_registry)
165 {
167 }
168 return all_loaded_modules;
169}
170
176template <typename T>
177std::vector<std::shared_ptr<GenericSubmodule<T>>> ModuleFactory<T>::createAllSubmodules(
179{
180 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_loaded_submodules{};
181
182 for (auto& [key, submodule] : m_registry_submodules)
183 {
185 }
187}
188
198template <typename T>
199std::vector<std::shared_ptr<GenericManagingModule<T>>>
202 const std::vector<std::string>& list_of_mananging_modules_to_load)
203{
204 std::unordered_set<std::string> modules_to_load_set(list_of_mananging_modules_to_load.begin(),
206 std::vector<std::shared_ptr<GenericManagingModule<T>>> all_loaded_modules{};
207
208 for (auto& [key, module] : m_registry)
209 {
210 if (modules_to_load_set.contains(key))
211 {
213 }
214 }
215 return all_loaded_modules;
216}
217
228template <typename T>
229std::vector<std::shared_ptr<GenericSubmodule<T>>> ModuleFactory<T>::createAllSelectedSubmodules(
231 const std::vector<std::shared_ptr<GenericManagingModule<T>>>& loaded_managing_modules)
232{
233 std::vector<std::shared_ptr<GenericSubmodule<T>>> all_loaded_submodules{};
234
235 std::unordered_set<std::string> unique_submodule_names_set;
236
237 for (auto const& module_pointer : loaded_managing_modules)
238 {
239 for (auto const& submodule_name : module_pointer->getSubmoduleList())
240 {
241 if (m_registry_submodules.find(submodule_name) == m_registry_submodules.end())
242 {
243 std::cerr << "[ModuleFactory]: No submodule " << submodule_name
244 << " loaded in registry.\n[ModuleFactory]: This submodule was requested "
245 "in managing module "
246 << module_pointer->getNameLocal() << "\n";
247 throw std::runtime_error("[ModuleFactory]: No submodule "
248 + static_cast<std::string>(submodule_name)
249 + " loaded in registry.\n[ModuleFactory]: This submodule was "
250 "requested in managing module "
251 + static_cast<std::string>(module_pointer->getNameLocal()) + "\n");
252 }
253 all_loaded_submodules.emplace_back(m_registry_submodules[submodule_name](sim_pointer));
255 }
256 }
257
258 std::vector<std::string> last_added_submodules(unique_submodule_names_set.begin(),
260
261 // Check iteratively the sub-submodules to complete the loaded submodules list.
262 while (!last_added_submodules.empty())
263 {
264 std::vector<std::string> newly_added;
265 // Iterate through all submodules
267 {
268 // Iterate through all requested sub-submodules if present.
269 for (auto& submodule_name : submodule_pointer->getSubmoduleList())
270 {
271 // If the requested sub-submodule from one of the loaded ones is not yet in the
272 // unique list (so already created) check if it is in the registry.
274 {
275 // If it is not in the registry, throw an error here. Otherwise add it to
276 // the list of submodules to be created (newly_added).
277 if (m_registry_submodules.find(submodule_name) == m_registry_submodules.end())
278 {
279 std::cerr << "[ModuleFactory]: No submodule " << submodule_name
280 << " loaded in registry.\n[ModuleFactory]: This submodule "
281 "was requested in submodule "
282 << submodule_pointer->getNameLocal() << "\n";
283 throw std::runtime_error(
284 "[ModuleFactory]: No submodule "
285 + static_cast<std::string>(submodule_name)
286 + " loaded in registry.\n[ModuleFactory]: This submodule "
287 "was requested in submodule "
288 + static_cast<std::string>(submodule_pointer->getNameLocal())
289 + "\n");
290 }
291 newly_added.push_back(submodule_name);
292 }
293 }
294 }
295 // Create all the newly added (so the requested) submodules.
296 for (auto& submodules_to_add : newly_added)
297 {
298 all_loaded_submodules.emplace_back(
299 m_registry_submodules[submodules_to_add](sim_pointer));
300 }
301 // Swap the newly added into the last added, thus signaling that there are more submodules
302 // that need to be checked for submodule. Start new iteration.
304 }
306}
307#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:23
std::shared_ptr< GenericManagingModule< T > >(*)(SimulationClassBase< T > *sim) creation_method
Definition ModuleFactory.h:26
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:200
static constexpr bool registerModule(std::string name, creation_method module) noexcept
Function that adds a module to the module registry map.
Definition ModuleFactory.h:76
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:229
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:137
static std::vector< std::shared_ptr< GenericSubmodule< T > > > createAllSubmodules(SimulationClassBase< T > *sim_pointer)
Creates every submodule in the submodule registry.
Definition ModuleFactory.h:177
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:96
ModuleFactory()=delete
std::shared_ptr< GenericSubmodule< T > >(*)(SimulationClassBase< T > *sim) creation_method_submodules
Definition ModuleFactory.h:28
static std::vector< std::shared_ptr< GenericManagingModule< T > > > createAllManagingModules(SimulationClassBase< T > *sim_pointer)
Creates every module in the module registry.
Definition ModuleFactory.h:158