Consolix
Toggle main menu visibility
Loading...
Searching...
No Matches
LoopComponent.hpp
Go to the documentation of this file.
1
#pragma once
2
#ifndef _CONSOLIX_LOOP_COMPONENT_HPP_INCLUDED
3
#define _CONSOLIX_LOOP_COMPONENT_HPP_INCLUDED
4
8
9
#include <thread>
10
#include <atomic>
11
#include <chrono>
12
#include <functional>
13
14
namespace
consolix
{
15
24
class
LoopComponent
:
25
public
IAppComponent
,
26
public
IShutdownable
{
27
public
:
28
30
LoopComponent
() =
default
;
31
36
LoopComponent
(
37
std::function<
bool
()>
on_initialize
,
38
std::function<
void
()>
on_execute
,
39
std::function<
void
(
int
)>
on_shutdown
) :
40
m_on_initialize
(std::move(
on_initialize
)),
41
m_on_execute
(std::move(
on_execute
)),
42
m_on_shutdown
(std::move(
on_shutdown
)) {
43
}
44
46
virtual
~LoopComponent
()
override
=
default
;
47
50
std::function<bool()>&
on_initialize
() {
51
return
m_on_initialize
;
52
}
53
56
std::function<void()>&
on_execute
() {
57
return
m_on_execute
;
58
}
59
62
std::function<void(
int
)>&
on_shutdown
() {
63
return
m_on_shutdown
;
64
}
65
66
protected
:
67
70
bool
initialize
()
override
{
71
try
{
72
if
(
m_on_initialize
) {
73
m_is_init
=
m_on_initialize
();
74
}
else
m_is_init
=
true
;
75
return
m_is_init
;
76
}
catch
(
const
std::exception& e) {
77
# if CONSOLIX_USE_LOGIT == 1
78
LOGIT_PRINT_ERROR(
"Unhandled exception during initialization: "
, e.what());
79
# else
80
CONSOLIX_STREAM
() <<
"Unhandled exception during initialization: "
<< e.what() << std::endl;
81
# endif
82
throw
;
83
}
84
}
85
88
bool
is_initialized
()
const override
{
89
return
m_is_init
;
90
}
91
96
void
process
()
override
{
97
try
{
98
if
(
m_on_execute
)
m_on_execute
();
99
else
std::this_thread::sleep_for(std::chrono::milliseconds(1));
100
}
catch
(
const
std::exception& e) {
101
# if CONSOLIX_USE_LOGIT == 1
102
LOGIT_PRINT_ERROR(
"Unhandled exception during execution: "
, e.what());
103
# else
104
CONSOLIX_STREAM
() <<
"Unhandled exception during execution: "
<< e.what() << std::endl;
105
# endif
106
throw
;
107
}
108
}
109
113
void
shutdown
(
int
signal)
override
{
114
try
{
115
if
(
m_on_shutdown
)
m_on_shutdown
(signal);
116
}
catch
(
const
std::exception& e) {
117
# if CONSOLIX_USE_LOGIT == 1
118
LOGIT_PRINT_ERROR(
"Unhandled exception during shutdown: "
, e.what());
119
# else
120
CONSOLIX_STREAM
() <<
"Unhandled exception during shutdown: "
<< e.what() << std::endl;
121
# endif
122
throw
;
123
}
124
}
125
126
private
:
127
std::function<bool()>
m_on_initialize
;
128
std::function<void()>
m_on_execute
;
129
std::function<void(
int
)>
m_on_shutdown
;
130
std::atomic<bool>
m_is_init
{
false
};
131
};
// LoopComponent
132
133
};
// namespace consolix
134
135
#endif
// _CONSOLIX_LOOP_COMPONENT_HPP_INCLUDED
CONSOLIX_STREAM
#define CONSOLIX_STREAM()
Fallback for general logging.
Definition
LoggerComponent.hpp:257
consolix::IAppComponent
Interface for defining application components.
Definition
IAppComponent.hpp:19
consolix::IShutdownable
Interface for components supporting shutdown logic.
Definition
IShutdownable.hpp:19
consolix::LoopComponent::m_on_execute
std::function< void()> m_on_execute
Execution function for the loop.
Definition
LoopComponent.hpp:128
consolix::LoopComponent::m_is_init
std::atomic< bool > m_is_init
Indicates whether the component is initialized.
Definition
LoopComponent.hpp:130
consolix::LoopComponent::on_shutdown
std::function< void(int)> & on_shutdown()
Access the shutdown function.
Definition
LoopComponent.hpp:62
consolix::LoopComponent::process
void process() override
Executes the main loop logic.
Definition
LoopComponent.hpp:96
consolix::LoopComponent::initialize
bool initialize() override
Initializes the component.
Definition
LoopComponent.hpp:70
consolix::LoopComponent::LoopComponent
LoopComponent()=default
Default constructor.
consolix::LoopComponent::LoopComponent
LoopComponent(std::function< bool()> on_initialize, std::function< void()> on_execute, std::function< void(int)> on_shutdown)
Constructor with customizable callbacks.
Definition
LoopComponent.hpp:36
consolix::LoopComponent::is_initialized
bool is_initialized() const override
Checks if the component is initialized.
Definition
LoopComponent.hpp:88
consolix::LoopComponent::on_initialize
std::function< bool()> & on_initialize()
Access the initialization function.
Definition
LoopComponent.hpp:50
consolix::LoopComponent::shutdown
void shutdown(int signal) override
Shuts down the component. This callback runs outside the POSIX signal handler even when a signal init...
Definition
LoopComponent.hpp:113
consolix::LoopComponent::on_execute
std::function< void()> & on_execute()
Access the execution function.
Definition
LoopComponent.hpp:56
consolix::LoopComponent::m_on_shutdown
std::function< void(int)> m_on_shutdown
Shutdown function.
Definition
LoopComponent.hpp:129
consolix::LoopComponent::~LoopComponent
virtual ~LoopComponent() override=default
Virtual destructor.
consolix::LoopComponent::m_on_initialize
std::function< bool()> m_on_initialize
Initialization function.
Definition
LoopComponent.hpp:127
consolix
< Utility modules and helpers.
Definition
BaseLoopComponent.hpp:13
include
consolix
components
LoopComponent.hpp
Generated by
1.17.0