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 stop();
83 throw;
84 }
85 }
86
89 bool is_initialized() override const {
90 return m_is_init;
91 }
92
97 void execute() override {
98 try {
100 else std::this_thread::sleep_for(std::chrono::milliseconds(1));
101 } catch (const std::exception& e) {
102# if CONSOLIX_USE_LOGIT == 1
103 LOGIT_PRINT_ERROR("Unhandled exception during execution: , e.what());
104# else
105 CONSOLIX_STREAM() << "Unhandled exception during execution: << e.what() << std::endl;
106# endif
107 stop();
108 throw;
109 }
110 }
111
114 void shutdown(int signal) override {
115 try {
116 if (m_on_shutdown) m_on_shutdown(signal);
117 stop();
118 } catch (const std::exception& e) {
119# if CONSOLIX_USE_LOGIT == 1
120 LOGIT_PRINT_ERROR("Unhandled exception during shutdown: ", e.what());
121# else
122 CONSOLIX_STREAM() << "Unhandled exception during shutdown: " << e.what() << std::endl;
123# endif
124 throw;
125 }
126 }
127
128 private:
129 std::function<bool()> m_on_initialize;
130 std::function<void()> m_on_execute;
131 std::function<void(int)> m_on_shutdown;
132 std::atomic<bool> m_is_init{false};
133 }; // LoopComponent
134
135}; // namespace consolix
136
137#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.
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() override const
Checks if the component is initialized.
std::function< bool()> & on_initialize()
Access the initialization function.
void shutdown(int signal) override
Shuts down the component.
std::function< void()> & on_execute()
Access the execution function.
std::function< void(int)> m_on_shutdown
Shutdown function.
void execute() override
Executes the main loop logic.
virtual ~LoopComponent() override=default
Virtual destructor.
std::function< bool()> m_on_initialize
Initialization function.
< Utility modules and helpers.
void stop()
Stops the application. Stops the application's main loop and begins the shutdown process.