![]() |
Consolix
|
Singleton class to manage the lifecycle of a console application. More...
#include <ConsoleApplication.hpp>
Public Member Functions | |
| template<typename Component, typename... Args> | |
| std::shared_ptr< Component > | add (Args &&... args) |
| Adds a new component to the application. | |
| void | add (std::shared_ptr< IAppComponent > component) |
| Adds an existing component to the application. | |
| void | init () |
| Initializes the application and its components. This method initializes all components in the manager and ensures readiness. | |
| template<typename InitAction> | |
| void | init (InitAction init_action) |
| Initializes the application with a custom action. | |
| void | run () |
| Runs the application with the registered components. | |
| template<typename IterationAction> | |
| void | run (IterationAction iteration_action) |
| Runs the application with a custom loop action. | |
| void | stop () |
| Stops the application's main loop. | |
| void | shutdown (int signal) |
Static Public Member Functions | |
| static ConsoleApplication & | get_instance () |
| Retrieves the singleton instance of the application. | |
Private Member Functions | |
| void | setup_signal_handlers () |
| Sets up signal handlers for graceful application termination. | |
| void | cleanup (int exit_code, bool wait_for_press=false) |
| Cleans up the application and shuts down all components. | |
| void | handle_fatal_exception (const std::exception &e) |
| Handles fatal exceptions by logging the error and terminating the application. | |
| template<typename IterationAction> | |
| void | lifecycle_loop (IterationAction iteration_action) |
| The main lifecycle loop with a custom action. | |
| void | lifecycle_loop () |
| The main lifecycle loop. | |
| ConsoleApplication ()=default | |
| ~ConsoleApplication ()=default | |
| ConsoleApplication (const ConsoleApplication &)=delete | |
| ConsoleApplication & | operator= (const ConsoleApplication &)=delete |
| ConsoleApplication (ConsoleApplication &&)=delete | |
| ConsoleApplication & | operator= (ConsoleApplication &&)=delete |
Static Private Member Functions | |
| static void | on_exit_handler () |
| Called upon normal program termination. | |
| static void | signal_handler (int exit_code) |
| Handles a POSIX signal and delegates to signal handler. | |
| static void | handle_signal (const char *signal_name, int exit_code) |
| Logs and processes a POSIX signal by initiating application cleanup. | |
Private Attributes | |
| AppComponentManager | m_manager |
| std::atomic< bool > | m_init {false} |
| std::atomic< bool > | m_running {false} |
| Flag indicating whether the loop is running. | |
| std::atomic< bool > | m_stopping {false} |
| std::atomic< bool > | m_cleanup {false} |
Singleton class to manage the lifecycle of a console application.
Provides functionality to initialize, execute, and gracefully shut down components in a structured lifecycle. Supports signal handling for both Windows and POSIX systems.
Definition at line 18 of file ConsoleApplication.hpp.
|
privatedefault |
|
privatedefault |
|
privatedelete |
|
privatedelete |
|
inline |
Adds a new component to the application.
| Component | The type of the component to create. |
| Args | The argument types for the component's constructor. |
| args | Arguments for constructing the component. |
std::shared_ptr to the newly added component. Definition at line 34 of file ConsoleApplication.hpp.
|
inline |
Adds an existing component to the application.
| component | A shared pointer to the component. |
Definition at line 40 of file ConsoleApplication.hpp.
|
inlineprivate |
Cleans up the application and shuts down all components.
| exit_code | Exit code indicating the reason for cleanup. |
| wait_for_press | Flag indicating if the program should wait for a key press before exiting. |
Definition at line 133 of file ConsoleApplication.hpp.
|
inlinestatic |
Retrieves the singleton instance of the application.
ConsoleApplication instance. Definition at line 23 of file ConsoleApplication.hpp.
|
inlineprivate |
Handles fatal exceptions by logging the error and terminating the application.
| e | The exception that caused the fatal error. |
Definition at line 167 of file ConsoleApplication.hpp.
|
inlinestaticprivate |
Logs and processes a POSIX signal by initiating application cleanup.
| signal_name | Name of the POSIX signal. |
| exit_code | POSIX signal code. |
Definition at line 275 of file ConsoleApplication.hpp.
|
inline |
Initializes the application and its components. This method initializes all components in the manager and ensures readiness.
Definition at line 46 of file ConsoleApplication.hpp.
|
inline |
Initializes the application with a custom action.
| InitAction | A callable type for the custom initialization action. |
| init_action | The custom action to perform during initialization. |
Definition at line 63 of file ConsoleApplication.hpp.
|
inlineprivate |
The main lifecycle loop.
Definition at line 191 of file ConsoleApplication.hpp.
|
inlineprivate |
The main lifecycle loop with a custom action.
| IterationAction | A callable executed within the loop. |
| iteration_action | The action to execute in each iteration. |
Definition at line 178 of file ConsoleApplication.hpp.
|
inlinestaticprivate |
Called upon normal program termination.
Definition at line 203 of file ConsoleApplication.hpp.
|
privatedelete |
|
privatedelete |
|
inline |
Runs the application with the registered components.
Definition at line 81 of file ConsoleApplication.hpp.
|
inline |
Runs the application with a custom loop action.
| IterationAction | A callable type executed within the main loop. |
| iteration_action | The custom action to perform in each loop iteration. |
Definition at line 93 of file ConsoleApplication.hpp.
|
inlineprivate |
Sets up signal handlers for graceful application termination.
Definition at line 120 of file ConsoleApplication.hpp.
|
inline |
Definition at line 108 of file ConsoleApplication.hpp.
|
inlinestaticprivate |
Handles a POSIX signal and delegates to signal handler.
| exit_code | POSIX signal code. |
Definition at line 258 of file ConsoleApplication.hpp.
|
inline |
Stops the application's main loop.
Definition at line 102 of file ConsoleApplication.hpp.
|
private |
Definition at line 117 of file ConsoleApplication.hpp.
|
private |
Definition at line 114 of file ConsoleApplication.hpp.
|
private |
Definition at line 113 of file ConsoleApplication.hpp.
|
private |
Flag indicating whether the loop is running.
Definition at line 115 of file ConsoleApplication.hpp.
|
private |
Definition at line 116 of file ConsoleApplication.hpp.