2#ifndef _CONSOLIX_APP_COMPONENT_MANAGER_HPP_INCLUDED
3#define _CONSOLIX_APP_COMPONENT_MANAGER_HPP_INCLUDED
43 template <
typename Component,
typename... Args>
44 std::shared_ptr<Component>
add(Args&&... args) {
45 auto ptr = std::make_shared<Component>(std::forward<Args>(args)...);
52 void add(std::shared_ptr<IAppComponent> component) {
64 if (component->is_initialized())
continue;
65 component->initialize();
68 }
catch (
const std::exception& e) {
69# if CONSOLIX_USE_LOGIT == 1
70 LOGIT_PRINT_FATAL(
"Initialization error: ", e.what());
80 if (!component->is_initialized())
return false;
94 }
catch (
const std::exception& e) {
95# if CONSOLIX_USE_LOGIT == 1
96 LOGIT_PRINT_FATAL(
"Processing error: ", e.what());
116# if CONSOLIX_USE_LOGIT == 1
117 LOGIT_PRINT_INFO(
"Starting shutdown with signal: ", signal);
119 std::vector<std::string> errors;
120 for (
size_t index = 0; index <
m_components.size(); ++index) {
123 if (
auto shutdownable = std::dynamic_pointer_cast<IShutdownable>(component)) {
124 shutdownable->shutdown(signal);
126 }
catch (
const std::exception& e) {
127# if CONSOLIX_USE_LOGIT == 1
128 LOGIT_PRINT_FATAL(
"Component [", index,
"] shutdown error: ", e.what());
130 errors.push_back(
"Component [" + std::to_string(index) +
"] error: " + e.what());
132# if CONSOLIX_USE_LOGIT == 1
133 LOGIT_PRINT_FATAL(
"Component [", index,
"] shutdown error: Unknown error");
135 errors.push_back(
"Component [" + std::to_string(index) +
"] error: Unknown error");
139 if (!errors.empty()) {
141# if CONSOLIX_USE_LOGIT == 1
142 std::string summary = std::accumulate(
143 errors.begin(), errors.end(), std::string(),
144 [](
const std::string& acc,
const std::string& error) {
145 return acc.empty() ? error : acc +
"; " + error;
148 LOGIT_PRINT_FATAL(
"Shutdown completed with errors. Summary: ", summary);
std::vector< std::shared_ptr< IAppComponent > > m_components
List of managed application components.
void shutdown(int signal)
Shuts down all components with "soft shutdown" support.
bool initialize()
Initializes all registered components.
bool is_initialized() const
Checks if all components are initialized.
std::shared_ptr< Component > add(Args &&... args)
Adds a new component to the manager.
void process()
Executes the main functionality of all components.
AppComponentManager()=default
Constructs an empty component manager.
~AppComponentManager()
Destroys the component manager and clears all components.
void add(std::shared_ptr< IAppComponent > component)
Adds an existing component to the manager.
< Utility modules and helpers.