Consolix
Loading...
Searching...
No Matches
AppComponentManager.hpp
Go to the documentation of this file.
1#pragma once
2#ifndef _CONSOLIX_APP_COMPONENT_MANAGER_HPP_INCLUDED
3#define _CONSOLIX_APP_COMPONENT_MANAGER_HPP_INCLUDED
4
8
9#include <vector>
10
11namespace consolix {
12
26 public:
27
30
33 m_components.clear(); // Ensures the container is empty.
34 }
35
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)...);
46 m_components.push_back(ptr);
47 return ptr;
48 }
49
52 void add(std::shared_ptr<IAppComponent> component) {
53 m_components.push_back(std::move(component));
54 }
55
61 bool initialize() {
62 try {
63 for (const auto& component : m_components) {
64 if (component->is_initialized()) continue;
65 component->initialize();
66 }
67 return is_initialized();
68 } catch (const std::exception& e) {
69# if CONSOLIX_USE_LOGIT == 1
70 LOGIT_PRINT_FATAL("Initialization error: ", e.what());
71# endif
72 throw;
73 }
74 }
75
78 bool is_initialized() const {
79 for (const auto& component : m_components) {
80 if (!component->is_initialized()) return false;
81 }
82 return true;
83 }
84
89 void process() {
90 try {
91 for (const auto& component : m_components) {
92 component->process();
93 }
94 } catch (const std::exception& e) {
95# if CONSOLIX_USE_LOGIT == 1
96 LOGIT_PRINT_FATAL("Processing error: ", e.what());
97# endif
98 throw;
99 }
100 }
101
115 void shutdown(int signal) {
116# if CONSOLIX_USE_LOGIT == 1
117 LOGIT_PRINT_INFO("Starting shutdown with signal: ", signal);
118# endif
119 std::vector<std::string> errors; // Собираем ошибки
120 for (size_t index = 0; index < m_components.size(); ++index) {
121 const auto& component = m_components[index];
122 try {
123 if (auto shutdownable = std::dynamic_pointer_cast<IShutdownable>(component)) {
124 shutdownable->shutdown(signal);
125 }
126 } catch (const std::exception& e) {
127# if CONSOLIX_USE_LOGIT == 1
128 LOGIT_PRINT_FATAL("Component [", index, "] shutdown error: ", e.what());
129# endif
130 errors.push_back("Component [" + std::to_string(index) + "] error: " + e.what());
131 } catch (...) {
132# if CONSOLIX_USE_LOGIT == 1
133 LOGIT_PRINT_FATAL("Component [", index, "] shutdown error: Unknown error");
134# endif
135 errors.push_back("Component [" + std::to_string(index) + "] error: Unknown error");
136 }
137 }
138
139 if (!errors.empty()) {
140 // Обрабатываем собранные ошибки
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;
146 }
147 );
148 LOGIT_PRINT_FATAL("Shutdown completed with errors. Summary: ", summary);
149# endif
150 //throw std::runtime_error("Shutdown completed with errors. See logs for details.");
151 }
152 }
153
154 private:
156 std::vector<std::shared_ptr<IAppComponent>> m_components;
157 }; // AppComponentManager
158
159}; // namespace consolix
160
161#endif // _CONSOLIX_APP_COMPONENT_MANAGER_HPP_INCLUDED
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.