This is a central class of library module ALib App.
For information about general use and features of this class consult the ALib App User Manual.
#include <app.hpp>
Inner Type Index: | |
| struct | ConfigFileDescriptor |
| struct | StateMachine |
Public Type Index: | |
| enum class | Exceptions { ControlledEarlyExit } |
Protected Field Index: | |
| String | appInfo =nullptr |
| String | appName =nullptr |
| String | appVersion =nullptr |
| Paragraphs * | cErr =nullptr |
Same as cOut, but used for stream std::err. | |
| CommandLine | cli |
| The command-line parser. | |
| bool | cliStop =false |
| Flag to stop the CLI processing loop. | |
| Paragraphs * | cOut =nullptr |
| bool | dryrun =false |
| Flags | flags =Flags::NONE |
| Various boolean flags used to configure the application. | |
| StateMachine | machine |
| The state-machine singleton. | |
| lox::textlogger::TextLogger * | releaseLogger =nullptr |
| NString | releaseLoggerName ="RLOGGER" |
| lox::Lox * | releaseLox =nullptr |
| NString | releaseLoxName ="RLOX" |
| StopWatch | stopWatch |
Protected Method Index: | |
| App () | |
| virtual | ~App () |
| Virtual destructor. | |
|
strongprotected |
|
strong |
Generic Exceptions handled by class App. This enumeration contains only one single entry.
| Enumerator | |
|---|---|
| ControlledEarlyExit | This exception is used to support the exception/error/exit code paradigm introduced by class App.
|
|
strongprotected |
Built-in exit-codes of the ALib application. The codes are mostly generated in the method exceptionToExitCode, where exceptions thrown by the CLI implementation are translated to these codes.
In the case that an application desires to use different exit codes, the latter method has to be overridden.
This enumeration gets the according ALib Enum Records assigned as well as the resources describing the exit error.
|
strongprotected |
Configuration flags used with class App.
| Enumerator | |
|---|---|
| NONE | No flags set. |
| CreateReleaseLox | If set, method virtual void onBsSetupALox() will create a release Lox instance available via field virtual lox::Lox * GetRLox() , as well as an attached logger available via field virtual lox::textlogger::TextLogger * GetRLogger() . |
| UseReleaseLoggerForDebugLogging | If set, method virtual void onBsSetupALox() will create and attach the release logger to the debug |
| ALoxVerbosityExportAllAndWriteBackRelLogger | If set, method virtual void onSdExportConfig() adds 'ExportAll' and 'writeback' to the verbosity variable of the lox::textlogger::TextLogger * releaseLogger in the case that this variable had not been read from the INI file with virtual void onBsImportConfig() . |
| ALoxVerbosityExportAllAndWriteBackDbgLogger | If set, method virtual void onSdExportConfig() adds 'ExportAll' and 'writeback' to the verbosity variable of the debug-logger in the case that this variable had not been read from the INI file with virtual void onBsImportConfig() . |
|
strongprotected |
Built-in options used with class App.
| Enumerator | |
|---|---|
| Help | Alternative to command 'help' as many people are used to pass the help command as an option. |
| Version | Alternative to command 'version' as many people are used to pass the version command as an option. |
| DryRun | Sets the flag dryrun. |
| Config | Change the configured config file path(s). |
| Verbose | Reserved option for increasing verbosity. |
|
strongprotected |
|
strongprotected |
The predefined states of the simple linear state-machine implemented by outer class App. For each state, a corresponding virtual function exists. The pairs of states and the corresponding virtual functions are collected in the field StateMachine::Program.
Custom applications (derived from class App) define their own enum type in case they want to insert additional pairs of state/methods into the program. This is possible because the collection of states in the field StateMachine::Program is implemented as type Enum, which may hold enumeration elements of an arbitrary type.
Note that the numbering of the enum elements is not of importance. The numbers found here were chosen to be quickly identifiable while debugging. The simple scheme is:
Also, the numbers are not defining the order of execution, which is solely defined by the order in the vector StateMachine::Program.
The custom enumeration type used for potential custom states may but does not need to align. It even may have the same integral values as given here. This flexibility is gained by the little magic of class Enum introduced by module ALib Boxing.
| Enumerator | |
|---|---|
| NOT_STARTED | Initial state before starting the App. The starting state not associated with a method. |
| SetCamps | Invokes the virtual method onBsSetCamps. |
| PrepareResources | Invokes the virtual method onBsPrepareResources. |
| SetNameVersionAndInfo | Invokes the virtual method onBsSetNameVersionAndInfo. |
| PrepareConfig | Invokes the virtual method onBsPrepareConfig. |
| PreloadVariables | Invokes the virtual method onBsPreloadVariables. |
| CLIDefine | Invokes the virtual method onBsCLIDefine. |
| CLIReadOptions | Invokes the virtual method onBsCLIReadOptions. |
| ImportConfig | Invokes the virtual method onBsImportConfig. |
| ConfigureCLI | Invokes the virtual method onBsConfigureCLI. |
| FinalizeBootstrap | Invokes the virtual method onBsFinalizeBootstrap. |
| ReadDryRunOption | Invokes the virtual method onBsReadDryRunOption. |
| SetupALox | Invokes the virtual method onBsSetupALox. |
| RunStart | Invokes the virtual method onRunStart. |
| Run | Invokes the virtual method onRun. |
| RunEnd | Invokes the virtual method onRunEnd. |
| AnnounceShutdown | Invokes the virtual method onSdAnnounceShutdown. |
| CleanALox | Invokes the virtual method onSdCleanALox. |
| ExportConfig | Invokes the virtual method onSdExportConfig. |
| Output | Invokes the virtual method onSdOutput. |
| FinalizeShutdown | Invokes the virtual method onSdFinalizeShutdown. |
|
protected |
Some short information and probably copyright message of the application. If not set from the descendant, it is tried to be loaded from resource "AppInfo" by the default implementation of onBsSetNameVersionAndInfo.
|
protected |
The name of the application. If not set from the descendant, it is tried to be loaded from resource "AppName" by the default implementation of onBsSetNameVersionAndInfo.
|
protected |
A version string of the application. If not set from the descendant, it is tried to be loaded from resource "AppVersion" by the default implementation of onBsSetNameVersionAndInfo.
|
protected |
|
protected |
|
protected |
|
protected |
Used to assemble and collect the output for standard output character stream std::cout during the run of this application.
The collected output is scheduled to be written by the method onSdOutput. This method, may also be "manually" invoked at any time to flush the current buffers to the output streams.
|
protected |
Set with built-in option "--dryrun", which is read with States::ReadDryRunOption. Defaults to false.
|
protected |
|
protected |
|
protected |
|
protected |
This will be the name of the release logger attached to the release Lox. The name defaults to "RLOGGER" and may be changed prior to the call to the method onBsSetupALox.
|
protected |
|
protected |
This will be the name of the release Lox. The name defaults to "RLOX" and may be changed prior to the call to the method onBsSetupALox.
|
protected |
|
protected |
Constructor. Initializes this app. Mainly by filling the vector StateMachine::Program with all states found in the enumeration States along with the corresponding virtual methods.
|
protectedvirtual |
|
virtual |
Writes exception information to std::cerr
.
| exception | The exception that was thrown. |
| target | The target string to write into. An option is to pass cErr->Buffer here. Of course a custom buffer might be passed, i.e. if the text is to be logged with ALox. |
Definition at line 158 of file app.cpp.
|
virtual |
Translates exceptions to exit-codes of the application. Implementations should call this method first and, if necessary, perform their own conversions then. If they do so, they should check whether this default version returned ErrUnknown, perform their own translation in that case, and probably debug-assert on failure.
| exception | The exception that was thrown. |
BuiltInExitCodes::ErrUnknown. Definition at line 131 of file app.cpp.
|
virtual |
Used when errors are detected. May be overwritten, for example, to suppress help output and/or otherwise change the standard behavior. This default implementation
| exitCode | The exit code to set in field StateMachine machine. Also used to write an error message by retrieving const String & FormatString() . |
| helpTopic | An optional parameter that defines a topic for the help text written to Paragraphs * cOut. |
| formatParam1 | An optional parameter to be used with the format string. |
| formatParam2 | A second optional parameter to be used with the format string. |
Definition at line 636 of file app.cpp.
|
virtual |
This default implementation first calls getConfigFilePathsFromCLIParam and, if this is unsuccessful, the method getConfigFilePathsFromResources. Then, method getConfigFilePathsMakeAbsolutePaths is called.
CLI arguments may affect which configuration files are considered. Descendants can override getConfigFilePathsFromCLIParam to implement their own CLI-based selection strategy before resource-defined defaults are completed.
| files | A pointer to an object of type StdVectorMA<ConfigFileDescriptor> that is to be filled by this method. |
Definition at line 308 of file app.cpp.
|
virtual |
Adds configuration file paths influenced by a dedicated CLI parameter. The default implementation interprets option "--config" and updates or appends corresponding ConfigFileDescriptor entries in files. Descendants may override this to parse different CLI options or search paths.
| files | Output vector to append file descriptors to. |
Definition at line 286 of file app.cpp.
|
virtual |
If the given vector files is empty, this method tries resources named "CFGF_NAME_1", "CFGF_NAME_2", "CFGF_NAME_3" and so forth until a first name is not found. For all names found, an entry in vector files is created.
Then, for each entry in the vector (regardless if they were preexisting or created with this method), comment information is loaded from resources. The resource name is composed from "CFGF_CMT_" and the number of the entry. If found, the resource is assigned to the field String Comment.
Then for each entry resources named "CFGF_EXP_" and the amount of the entry are searched. If found, the field String Exports is set. If more resources (with higher numbers) are existing, such additional exports are appended to the list of the last config file entry.
| files | The vector of file paths that is to be filled or modified by this method. |
Definition at line 209 of file app.cpp.
|
virtual |
|
inline |
Returns the version of this application. This default implementation returns the field appVersion.
|
inline |
|
inlinevirtual |
Has to return the Logger instance used with release-logging.
|
inlinevirtual |
Has to return the Lox instance used with release-logging.
|
inline |
Returns the version of this application. This default implementation returns the field appVersion.
|
virtual |
Stores cli-parameters and executes this app.
| argc | The number of command-line arguments. |
| argv | List of command-line arguments, given as single byte character strings. |
| argvw | The CLI arguments on platforms that use wide command-line strings. Defaults to nullptr. If given, parameter argv has to be nulled. |
main(). Definition at line 45 of file app.cpp.
|
virtual |
Define CLI commands, options, parameters, and exit-codes. This default implementation initializes field cli with APP and registers the built-in declarations defined by the enum types Commands, Options, Parameters and ExitCodes.
Definition at line 355 of file app.cpp.
|
virtual |
Implements CLIReadOptions: calls virtual void ReadOptions() on member cli. This is done quite early in the boot sequence to allow option '–config' to change the configuration files used.
Definition at line 363 of file app.cpp.
|
virtual |
Finalizes CLI after configuration import. The default implementation forwards ignored option arguments to the CLIVariablesPlugin and installs the default shortcut categories ALIB and ALOX.
Definition at line 365 of file app.cpp.
|
virtual |
Called on transition to state FinalizeBootstrap.
This default implementation just calls the function alib::Bootstrap passing BootstrapPhases::Final as the parameter.
Definition at line 401 of file app.cpp.
|
virtual |
Called on transition to state ImportConfig.
This default implementation creates the vector of file descriptors using getConfigFilePaths and then uses type IniFileFeeder to import the variables from the INI-files.
Definition at line 377 of file app.cpp.
|
virtual |
Called on transition to state PreloadVariables.
Preloading variables allows later fetching and exporting those variables into a configuration file (if freshly created), even if they have not been read/used in the first run of an application.
This default implementation calls void PreloadVariables() on the variables found with the enumeration Variables.
Definition at line 353 of file app.cpp.
|
virtual |
Called on transition to state PrepareConfig.
This default implementation just calls the function alib::Bootstrap passing BootstrapPhases::PrepareConfig as the parameter.
Definition at line 351 of file app.cpp.
|
virtual |
Called on transition to state PrepareResources.
This default implementation
std::cout. Definition at line 320 of file app.cpp.
|
virtual |
Implements ReadDryRunOption. Reads the option "--dryrun[=yes|no]" from the CLI and sets the flag dryrun accordingly. The optional argument is parsed as a lang::Bool and hence may be specified with various words, defined with the according resources.
Definition at line 403 of file app.cpp.
|
virtual |
Called on transition to state SetCamps.
This default implementation just calls the alib::BootstrapAddDefaultCamps.
Definition at line 318 of file app.cpp.
|
virtual |
Called on transition to state SetNameVersionAndInfo.
This default implementation tries resources "AppName", "AppVersion", and "AppInfo" to set fields appName, appVersion, and appInfo. The latter is supported with three placeholders: the name, the version, and the current year and is created using the method Paragraphs & AddMarked(const BoxedObjects& ...) .
A string that is set before this method is called, is not replaced.
Definition at line 335 of file app.cpp.
|
virtual |
Called on transition to state SetupALox.
If this method returns an error, bootstrapping is aborted. This default implementation interprets the flags CreateReleaseLox and UseReleaseLoggerForDebugLogging, and furthermore uses the fields releaseLoxName and releaseLoggerName into account and potentially creates releaseLox and releaseLogger.
Definition at line 416 of file app.cpp.
|
virtual |
This is the main execution method of the application. This default implementation processes CLI commands until cliStop is set or all commands are consumed and dispatches each recognized command to processCLICmd.
Definition at line 594 of file app.cpp.
|
virtual |
|
virtual |
This method is preceding the main execution method onRun. When this is called, all bootstrapping is done. Final preparations might be performed here to unclutter onRun.
This default implementation
"AppStart". If found its content is printed to the console using method AddMarked together with the results of the methods GetName and GetVersion. Hence the resourced application info has to have two placeholders for these values.DryRunStart and prints it to the console. Again the result of the method GetName is passed as a placeholder parameter."Help" was given. If so, calls GetHelp and stops processing."Version" was given. If so, prints the result of GetVersion and stops processing. Definition at line 561 of file app.cpp.
|
virtual |
Called on transition to state AnnounceShutdown.
This default implementation just calls the function alib::Shutdown passing ShutdownPhases::Announce as the parameter.
Definition at line 439 of file app.cpp.
|
virtual |
Called on transition to state CleanALox.
This default implementation checks the flags ALoxVerbosityExportAllAndWriteBackRelLogger, and ALoxVerbosityExportAllAndWriteBackDbgLogger and calls SetVerbosityExport if set.
Then, if set, the release logger and Lox are deleted.
Definition at line 442 of file app.cpp.
|
virtual |
Called on transition to state ExportConfig.
This default implementation creates the vector of file descriptors using getConfigFilePaths and then uses type IniFileFeeder to export variables that are either not existing yet, or which have a writeback flag set, into the INI-files.
This is done for all variables (and variable trees) determined by the field String Exports.
Definition at line 461 of file app.cpp.
|
virtual |
Called on transition to state FinalizeShutdown.
This default implementation calls the function alib::Shutdown passing ShutdownPhases::Destruct as the parameter.
Prior to that, some memory cleaning and debug exercises are done.
Definition at line 530 of file app.cpp.
|
virtual |
Flushes what is collected in the fields cOut and cErr to their respective streams and clears the buffers. With that, this method - while named with the prefix onSd and scheduled at the end of the StateMachine::Program - may be directly called by descendants whenever they wish to flush the current collected output stream, for example, after important milestones, before long-running operations, or prior to program termination when early partial output is desired.
Definition at line 510 of file app.cpp.
|
virtual |
|
virtual |
Processes a single parsed CLI command. The default implementation handles the built-in Commands.
| cmd | The command to execute. |
true if the command was handled successfully; false to indicate that the command was not recognized or could not be executed. Implementations may set cliStop to terminate further processing. Definition at line 645 of file app.cpp.