11#include <unordered_set>
18template <
typename ModuleType>
20 requires requires(
const ModuleType& module) {
21 module.getNameLocal();
24 } -> std::constructible_from<std::string>;
30 std::string m_error_string;
41 const std::vector<std::shared_ptr<ModuleType>> unresolved_dependency_modules)
43 std::ostringstream oss;
44 oss <<
"Failed to resolve dependencies for the following modules:\n";
45 for (
auto& module : unresolved_dependency_modules)
47 oss <<
module->getNameLocal() << "\n";
49 oss <<
"Please check the dependencies and resolve the circular or missing "
51 m_error_string = oss.str();
57 [[nodiscard]]
const char*
what() const noexcept
override {
return m_error_string.c_str(); }
68template <
typename ModuleType>
72 using ModuleList = std::vector<std::shared_ptr<ModuleType>>;
73 std::map<std::string_view, ModuleList> m_chain_map{};
75 bool checkDependencyCurrentModule(std::shared_ptr<ModuleType> module,
76 ModuleList& sorted_chain_module_vector);
82 void insertModule(std::string_view key,
const std::shared_ptr<ModuleType>& module);
91 const std::vector<std::string>& modules_to_load);
104template <
typename ModuleType>
114template <
typename ModuleType>
117 if (chain_type ==
"global")
119 m_chain_map.insert({
"InitChain", std::vector<std::shared_ptr<ModuleType>>{}});
120 m_chain_map.insert({
"PreTimeStepChain", std::vector<std::shared_ptr<ModuleType>>{}});
121 m_chain_map.insert({
"PostNonLinIterChain", std::vector<std::shared_ptr<ModuleType>>{}});
122 m_chain_map.insert({
"PostTimeStepChain", std::vector<std::shared_ptr<ModuleType>>{}});
123 m_chain_map.insert({
"OutputChain", std::vector<std::shared_ptr<ModuleType>>{}});
133template <
typename ModuleType>
135 const std::shared_ptr<ModuleType>& module)
137 m_chain_map[key].emplace_back(module);
145template <
typename ModuleType>
149 for (
const auto& module_pointer :
153 module_pointer->exec(key);
164template <
typename ModuleType>
166 std::string_view module_key)
168 for (
const auto& module_pointer :
169 m_chain_map[chain_key])
172 if (module_pointer->getNameLocal() == module_key)
174 module_pointer->exec(chain_key);
186template <
typename ModuleType>
189 for (
auto& module : all_modules)
191 for (
auto& key :
module->getChainInsertion())
193 insertModule(key, module);
196 resolveDependencies();
207template <
typename ModuleType>
209 const std::vector<std::string>& modules_to_load)
211 std::unordered_set<std::string> modules_to_load_set(modules_to_load.begin(),
212 modules_to_load.end());
214 for (
auto& module : all_modules)
216 std::string mod_name{
module->getNameLocal()};
217 if (modules_to_load_set.contains(mod_name))
219 for (
auto& key :
module->getChainInsertion())
221 insertModule(key, module);
225 resolveDependencies();
233template <
typename ModuleType>
237 for (
auto& [key, chain_module_vector] : m_chain_map)
239 std::vector<std::shared_ptr<ModuleType>> sorted_chain_module_vector{};
240 while (!chain_module_vector.empty())
242 bool progress =
false;
246 auto current_module = std::remove_if(
247 chain_module_vector.begin(), chain_module_vector.end(),
248 [&](
const auto& module)
251 if (checkDependencyCurrentModule(module, sorted_chain_module_vector))
253 sorted_chain_module_vector.push_back(module);
262 chain_module_vector.erase(current_module, chain_module_vector.end());
282template <
typename ModuleType>
302 { return resolved_names.contains(dependency); });
308template <
typename ModuleType>
313 for (
const auto& [
key,
chain] : m_chain_map)
326template <
typename ModuleType>
Template generalized ChainManager. It stores pointers to the modules and runs their exec-function on ...
Definition ChainManager.h:70
void runChain(std::string_view key)
Runs the exec-functions of each module within the specified chain (if existing).
Definition ChainManager.h:146
void autoLoader(const ModuleList &all_modules, const std::vector< std::string > &modules_to_load)
Inserts only user specified modules derived from a module type into the chain in the load-order....
Definition ChainManager.h:208
void autoLoader(const ModuleList &all_modules)
Inserts all modules derived from a module type into the chain in the load-order. Only used for module...
Definition ChainManager.h:187
std::unordered_set< std::string > getManagingModuleNames() const
Function that returns the names of the requested/loaded managing modules.
Definition ChainManager.h:327
void insertModule(std::string_view key, const std::shared_ptr< ModuleType > &module)
Insert modules into chains. Generates a new chain, if the chain key does not yet exist.
Definition ChainManager.h:134
std::vector< std::string > getChainNames() const
Getter function for the chain names.
Definition ChainManager.h:309
void resolveDependencies()
Guarantees that the module order is correct to ensure all dependencies are met. Throws a DependencyEr...
Definition ChainManager.h:234
void runSingleModuleInChain(std::string_view chain_key, std::string_view module_key)
Runs the exec-function of a singular specified module within a specified chain (if existing).
Definition ChainManager.h:165
ChainManager(std::string_view chain_type)
Constructor of the chain manager with possibility to specify chain type. Option(s): "global"....
Definition ChainManager.h:115
ChainManager()
Constructor of the chain manager without arguments. Does not initialize chains, so the have to be add...
Definition ChainManager.h:105
Error class to throw when a module chain has circular or missing dependencies. Inherits from std::exc...
Definition ChainManager.h:28
DependencyError(const std::vector< std::shared_ptr< ModuleType > > unresolved_dependency_modules)
Definition ChainManager.h:40
~DependencyError() override=default
const char * what() const noexcept override
Supplies the error message as a c-style string/char array.
Definition ChainManager.h:57