Consolix
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
14namespace consolix {
15
25 public IAppComponent,
26 public IShutdownable {
27 public:
28
30 LoopComponent() = default;
31
37 std::function<bool()> on_initialize,
38 std::function<void()> on_execute,
39 std::function<void(int)> on_shutdown) :
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) {
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 {
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
#define CONSOLIX_STREAM()
Fallback for general logging.
Interface for defining application components.
Interface for components supporting shutdown logic.
std::function< void()> m_on_execute
Execution function for the loop.
std::atomic< bool > m_is_init
Indicates whether the component is initialized.
std::function< void(int)> & on_shutdown()
Access the shutdown function.
void process() override
Executes the main loop logic.
bool initialize() override
Initializes the component.
LoopComponent()=default
Default constructor.
LoopComponent(std::function< bool()> on_initialize, std::function< void()> on_execute, std::function< void(int)> on_shutdown)
Constructor with customizable callbacks.
bool is_initialized() const override
Checks if the component is initialized.
std::function< bool()> & on_initialize()
Access the initialization function.
void shutdown(int signal) override
Shuts down the component. This callback runs outside the POSIX signal handler even when a signal init...
std::function< void()> & on_execute()
Access the execution function.
std::function< void(int)> m_on_shutdown
Shutdown function.
virtual ~LoopComponent() override=default
Virtual destructor.
std::function< bool()> m_on_initialize
Initialization function.
< Utility modules and helpers.