![]() |
Consolix
|
Singleton class to manage the lifecycle of a console application. More...
#include <ConsoleApplication.hpp>
Classes | |
| class | PosixTerminationSignalMaskGuard |
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. | |
| bool | stop_requested () |
| Checks whether a POSIX signal requested shutdown and synchronizes the runtime state. | |
| int | resolve_stop_exit_code (int fallback_exit_code) const |
| Resolves the exit code for the active stop request. | |
| void | cleanup_if_stopping () |
| Executes cleanup immediately if a stop request is already pending. | |
| 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 by recording a deferred shutdown request. | |
| static void | reset_signal_state () |
| Resets POSIX signal state before installing handlers. | |
| static volatile std::sig_atomic_t & | pending_signal_code () |
| Storage for the last requested POSIX shutdown signal. | |
| static volatile std::sig_atomic_t & | signal_stop_requested () |
| Storage for the POSIX stop-request flag. | |
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. On POSIX systems, signal handlers only request shutdown, and the actual component cleanup runs later in the normal execution path.
Definition at line 25 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. |
Definition at line 41 of file ConsoleApplication.hpp.
|
inline |
Adds an existing component to the application.
| component | A shared pointer to the component. |
Definition at line 47 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 144 of file ConsoleApplication.hpp.
|
inlineprivate |
Executes cleanup immediately if a stop request is already pending.
Definition at line 328 of file ConsoleApplication.hpp.
|
inlinestatic |
Retrieves the singleton instance of the application.
Definition at line 30 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 184 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 53 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 68 of file ConsoleApplication.hpp.
|
inlineprivate |
The main lifecycle loop.
Definition at line 208 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 195 of file ConsoleApplication.hpp.
|
inlinestaticprivate |
Called upon normal program termination.
Definition at line 220 of file ConsoleApplication.hpp.
|
privatedelete |
|
privatedelete |
|
inlinestaticprivate |
Storage for the last requested POSIX shutdown signal.
Definition at line 335 of file ConsoleApplication.hpp.
|
inlinestaticprivate |
Resets POSIX signal state before installing handlers.
Definition at line 305 of file ConsoleApplication.hpp.
|
inlineprivate |
Resolves the exit code for the active stop request.
Definition at line 319 of file ConsoleApplication.hpp.
|
inline |
Runs the application with the registered components.
Definition at line 82 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 94 of file ConsoleApplication.hpp.
|
inlineprivate |
Sets up signal handlers for graceful application termination.
Definition at line 122 of file ConsoleApplication.hpp.
|
inline |
Definition at line 109 of file ConsoleApplication.hpp.
|
inlinestaticprivate |
Handles a POSIX signal by recording a deferred shutdown request.
| exit_code | POSIX signal code. |
Definition at line 299 of file ConsoleApplication.hpp.
|
inlinestaticprivate |
Storage for the POSIX stop-request flag.
Definition at line 341 of file ConsoleApplication.hpp.
|
inline |
Stops the application's main loop.
Definition at line 103 of file ConsoleApplication.hpp.
|
inlineprivate |
Checks whether a POSIX signal requested shutdown and synchronizes the runtime state.
Definition at line 311 of file ConsoleApplication.hpp.
|
private |
Definition at line 119 of file ConsoleApplication.hpp.
|
private |
Definition at line 116 of file ConsoleApplication.hpp.
|
private |
Definition at line 115 of file ConsoleApplication.hpp.
|
private |
Flag indicating whether the loop is running.
Definition at line 117 of file ConsoleApplication.hpp.
|
private |
Definition at line 118 of file ConsoleApplication.hpp.