Configuration Variables
Configuration variables allow to define the behaviour of the application. Such a variable can be set in various ways (see below) and can be read in the source code.
A configuration variable can be defined inside a source code file using one of these macros. The variable allows then to access its value.
The so defined variables can be simply accessed using the created global variable:
// This example uses the global variables defined with the configuration // variables to copy the given file to the given destination.// check configuration variables
if (g_copy_source.IsEmpty()) return maxon::OK ; if (g_copy_destination.IsEmpty()) return maxon::IllegalArgumentError( MAXON_SOURCE_LOCATION , "g_copy_source is set but g_copy_destination is empty." _s);// check URLs const maxon::Url sourceFile(g_copy_source); if (sourceFile.IoDetect() != maxon::IODETECT::FILE ) return maxon::IllegalArgumentError( MAXON_SOURCE_LOCATION , "g_copy_source is not a file." _s); const maxon::Url destinationDir(g_copy_destination); if (destinationDir.IoDetect() != maxon::IODETECT::DIRECTORY ) return maxon::IllegalArgumentError( MAXON_SOURCE_LOCATION , "g_copy_destination is not an existing folder." _s);
// copy const maxon::Url targetFile = (destinationDir + sourceFile.GetName()) iferr_return ; sourceFile.IoCopyFile(targetFile, g_copy_overwrite, false ) iferr_return ;
The value of a configuration variable can be set in multiple ways. It can be set
config.txt
next to the application.
A configuration variable can be accessed in any source code file using the maxon::Configuration class:
// execute task const maxon::Result<void> res = ExecuteTask();
// get value of configuration variable "g_printErrors" maxon::CONFIGURATIONENTRY_ORIGIN origin; maxon::CONFIGURATIONENTRY_STATE state; maxon::Bool printError = false ; iferr ( maxon::Configuration::QueryBool ( "g_printErrors" _s, printError, origin, state)) { // if an error occurred and if "g_printErrors" was set to "true" // print the error const maxon::Bool executionFailed = res == maxon::FAILED ; if (executionFailed && printError) { DiagnosticOutput ( "Error: @" , res. GetError ()); } }
It is also possible to register configuration variables at runtime. With these functions the variables are marked as used ( maxon::CONFIGURATIONENTRY_STATE::USED ) and the help text will be defined.
// configuration variable key const maxon::String customDebug { "g_customDebugLevel" }; const maxon::CONFIGURATION_CATEGORY category = maxon::CONFIGURATION_CATEGORY::REGULAR ; maxon::Configuration::AddHelpForString (customDebug, "" _s, category, "Print custom debug messages." ) iferr_return ; maxon ::Configuration::AddHelpForOption(customDebug, "all"_s, false, true, category, "Print all debug information.") iferr_return ; maxon ::Configuration::AddHelpForOption(customDebug, "warning"_s, false, false, category, "Print only warnings.") iferr_return ; maxon ::Configuration::AddHelpForOption(customDebug, "error"_s, false, false, category, "Print only errors.") iferr_return ; return maxon :: OK ; } MAXON_INITIALIZATION (AddCustomDebugLevel, nullptr );
This will be displayed in the console like this:
// Print custom debug messages. g_customDebugLevel=[ string |all|warning|error]These broadcast functions are used to copy a value into all modules that defined the given configuration variable.