Consolix
Toggle main menu visibility
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
11
namespace
consolix
{
12
25
class
AppComponentManager
{
26
public
:
27
29
AppComponentManager
() =
default
;
30
32
~AppComponentManager
() {
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
116
void
shutdown
(
int
signal) {
117
# if CONSOLIX_USE_LOGIT == 1
118
LOGIT_PRINT_INFO(
"Starting shutdown with signal: "
, signal);
119
# endif
120
std::vector<std::string> errors;
// Собираем ошибки
121
for
(
size_t
remaining =
m_components
.size(); remaining > 0; --remaining) {
122
const
size_t
index = remaining - 1;
123
const
auto
& component =
m_components
[index];
124
try
{
125
if
(
auto
shutdownable = std::dynamic_pointer_cast<IShutdownable>(component)) {
126
shutdownable->shutdown(signal);
127
}
128
}
catch
(
const
std::exception& e) {
129
# if CONSOLIX_USE_LOGIT == 1
130
LOGIT_PRINT_FATAL(
"Component ["
, index,
"] shutdown error: "
, e.what());
131
# endif
132
errors.push_back(
"Component ["
+ std::to_string(index) +
"] error: "
+ e.what());
133
}
catch
(...) {
134
# if CONSOLIX_USE_LOGIT == 1
135
LOGIT_PRINT_FATAL(
"Component ["
, index,
"] shutdown error: Unknown error"
);
136
# endif
137
errors.push_back(
"Component ["
+ std::to_string(index) +
"] error: Unknown error"
);
138
}
139
}
140
141
if
(!errors.empty()) {
142
// Обрабатываем собранные ошибки
143
# if CONSOLIX_USE_LOGIT == 1
144
std::string summary = std::accumulate(
145
errors.begin(), errors.end(), std::string(),
146
[](
const
std::string& acc,
const
std::string& error) {
147
return acc.empty() ? error : acc +
"; "
+ error;
148
}
149
);
150
LOGIT_PRINT_FATAL(
"Shutdown completed with errors. Summary: "
, summary);
151
# endif
152
//throw std::runtime_error("Shutdown completed with errors. See logs for details.");
153
}
154
}
155
156
private
:
158
std::vector<std::shared_ptr<IAppComponent>>
m_components
;
159
};
// AppComponentManager
160
161
};
// namespace consolix
162
163
#endif
// _CONSOLIX_APP_COMPONENT_MANAGER_HPP_INCLUDED
consolix::AppComponentManager::m_components
std::vector< std::shared_ptr< IAppComponent > > m_components
List of managed application components.
Definition
AppComponentManager.hpp:158
consolix::AppComponentManager::shutdown
void shutdown(int signal)
Shuts down all components with "soft shutdown" support.
Definition
AppComponentManager.hpp:116
consolix::AppComponentManager::initialize
bool initialize()
Initializes all registered components.
Definition
AppComponentManager.hpp:61
consolix::AppComponentManager::is_initialized
bool is_initialized() const
Checks if all components are initialized.
Definition
AppComponentManager.hpp:78
consolix::AppComponentManager::add
std::shared_ptr< Component > add(Args &&... args)
Adds a new component to the manager.
Definition
AppComponentManager.hpp:44
consolix::AppComponentManager::process
void process()
Executes the main functionality of all components.
Definition
AppComponentManager.hpp:89
consolix::AppComponentManager::AppComponentManager
AppComponentManager()=default
Constructs an empty component manager.
consolix::AppComponentManager::~AppComponentManager
~AppComponentManager()
Destroys the component manager and clears all components.
Definition
AppComponentManager.hpp:32
consolix::AppComponentManager::add
void add(std::shared_ptr< IAppComponent > component)
Adds an existing component to the manager.
Definition
AppComponentManager.hpp:52
consolix
< Utility modules and helpers.
Definition
BaseLoopComponent.hpp:13
include
consolix
core
AppComponentManager.hpp
Generated by
1.17.0