2#ifndef _CONSOLIX_CONSOLE_APPLICATION_HPP_INCLUDED
3#define _CONSOLIX_CONSOLE_APPLICATION_HPP_INCLUDED
33 template <
typename Component,
typename... Args>
34 std::shared_ptr<Component>
add(Args&&... args) {
35 return m_manager.add<Component>(std::forward<Args>(args)...);
40 void add(std::shared_ptr<IAppComponent> component) {
49 std::this_thread::sleep_for(std::chrono::milliseconds(1));
54 }
catch (
const std::exception& e) {
62 template <
typename InitAction>
63 void init(InitAction init_action) {
66 std::this_thread::sleep_for(std::chrono::milliseconds(1));
75 }
catch (
const std::exception& e) {
92 template <
typename IterationAction>
93 void run(IterationAction iteration_action) {
121# if defined(_WIN32) || defined(_WIN64)
122 SetConsoleCtrlHandler(console_handler, TRUE);
133 void cleanup(
int exit_code,
bool wait_for_press =
false) {
137# if CONSOLIX_USE_LOGIT == 1
138 LOGIT_PRINT_INFO(
"Cleaning up application for exit code: ", exit_code);
141 }
catch (
const std::exception& e) {
146 }
catch (
const std::exception& e) {
158 if (wait_for_press) {
162 std::exit(exit_code);
168# if CONSOLIX_USE_LOGIT == 1
169 LOGIT_PRINT_FATAL(
"Unhandled exception: ", e.what());
177 template <
typename IterationAction>
185 }
catch (
const std::exception& e) {
197 }
catch (
const std::exception& e) {
207# if defined(_WIN32) || defined(_WIN64)
212 static BOOL WINAPI console_handler(DWORD win_event) {
215 log_event(
"CTRL_C_EVENT");
217 case CTRL_CLOSE_EVENT:
218 log_event(
"CTRL_CLOSE_EVENT");
220 case CTRL_LOGOFF_EVENT:
221 log_event(
"CTRL_LOGOFF_EVENT");
223 case CTRL_SHUTDOWN_EVENT:
224 log_event(
"CTRL_SHUTDOWN_EVENT");
227 log_event(
"UNKNOWN_EVENT");
236 static void log_event(
const char* event_name) {
237# if CONSOLIX_USE_LOGIT == 1
238 LOGIT_PRINT_INFO(
"Console event received: ", event_name);
245 static int event_to_exit_code(DWORD win_event) {
276# if CONSOLIX_USE_LOGIT == 1
277 LOGIT_PRINT_INFO(
"POSIX signal received: ", signal_name,
", exit code: ", exit_code);
#define CONSOLIX_STREAM()
Fallback for general logging.
Manages a collection of application components with lifecycle support.
std::shared_ptr< Component > add(Args &&... args)
Adds a new component to the application.
AppComponentManager m_manager
void add(std::shared_ptr< IAppComponent > component)
Adds an existing component to the application.
std::atomic< bool > m_stopping
std::atomic< bool > m_cleanup
ConsoleApplication(const ConsoleApplication &)=delete
void setup_signal_handlers()
Sets up signal handlers for graceful application termination.
ConsoleApplication & operator=(ConsoleApplication &&)=delete
std::atomic< bool > m_running
Flag indicating whether the loop is running.
ConsoleApplication()=default
void lifecycle_loop()
The main lifecycle loop.
void run()
Runs the application with the registered components.
void run(IterationAction iteration_action)
Runs the application with a custom loop action.
static ConsoleApplication & get_instance()
Retrieves the singleton instance of the application.
std::atomic< bool > m_init
static void signal_handler(int exit_code)
Handles a POSIX signal and delegates to signal handler.
void lifecycle_loop(IterationAction iteration_action)
The main lifecycle loop with a custom action.
void cleanup(int exit_code, bool wait_for_press=false)
Cleans up the application and shuts down all components.
static void handle_signal(const char *signal_name, int exit_code)
Logs and processes a POSIX signal by initiating application cleanup.
ConsoleApplication & operator=(const ConsoleApplication &)=delete
~ConsoleApplication()=default
void init(InitAction init_action)
Initializes the application with a custom action.
ConsoleApplication(ConsoleApplication &&)=delete
static void on_exit_handler()
Called upon normal program termination.
void init()
Initializes the application and its components. This method initializes all components in the manager...
void stop()
Stops the application's main loop.
void handle_fatal_exception(const std::exception &e)
Handles fatal exceptions by logging the error and terminating the application.
void shutdown(int signal)
static ServiceLocator & get_instance()
Retrieves the singleton instance of the ServiceLocator.
void clear_all()
Clears all registered resources.
#define CONSOLIX_WAIT_ON_ERROR
Enables or disables waiting for user input before exiting on a fatal error.
< Utility modules and helpers.