Definition
An instance of data type param_handler allows to maintain the configuration of an executable uniformly. It stores a list of configuration variables of the types bool, int, double, string and enum which can be accessed via a long mnemonic label or a short flag. The actual list of configuration variables is set by the operation add_parameter(). If there exists a parameter file in the execution directory of the program using param_handler then the initialization values for P can be taken from the file instead of the list filled by add_parameter().
Creation
param_handler | P(const char* paramfile=".last", bool initialRead = true); | |
introduces an unitialized parameter handler P of type param_handler. paramfile is the default file where the configuration information is stored at the end of program execution or by a call to write_parameter_file(). If initialRead is true the object is also initialized from the default file, if it exists. | ||
param_handler | P(int argc, char** argv, const char* paramfile=".last", bool initialRead = true); | |
introduces an unitialized parameter handler P of type param_handler. paramfile is the default file where the configuration information is stored at the end of program execution or by a call to write_parameter_file(). If initialRead is true the object is also initialized from the default file, if it exists. | ||
|
Operations
void | P.add_parameter(string param) | |
incrementally extends P by adding a new parameter to
it. The string param must have the form "Label:Flag:Type:Value"
where Label is a mnemonic name for the parameter, Flag
is an abbreviation for it, Type is one of
{bool, double, int, string,enum(...)} and Value is
the initialization value of the parameter.
Precondition: P.init_all() has not been called. |
||
void | param_handler::init_all() | initializes the expected parameter setting of the application. Afterwards the list of parameters added by add_parameter is fixed for all existing objects of type param_handler and cannot be changed anymore. This function also triggers the parsing of the command line parameters; when the flag -h is encountered, a help message is displayed for all param_handlers and the program is terminated. |
void | P.get_parameter(string label, double& value) | |
sets val to the variable referenced by label which can
be Label or Flag of the definitions in
add_parameter. This operation exists also for bool&,
int& and string&. The int& operation can also be used
for the enum type. Precondition: P.init_all() has been called. |
||
void | P.set_parameter(string label, double value) | |
sets a the variable referenced by label which can be
Label or Flag of the definitions in add_parameter to
the value val. This operation exists also for bool, and
string. Precondition: P.init_all() has been called. |
||
void | P.print_help() | prints a formated list of all parameters to the standard
output. Precondition: P.init_all() has been called. |
void | P.write_parameter_file() | All configuration information is written to the parameter
file set by the constructor or the operation
set_parameter_file. Precondition: P.init_all() has been called. |
void | P.write_parameter_file(string paramfile) | |
All configuration information is stored in file paramfile. | ||
void | P.set_parameter_file(string paramfile) | |
All configuration information is stored in the parameter file paramfile. |
Implementation
param_handler is implemented by an internal hashing scheme which maps labeled configuration variables to internal storage places. The access operations take constant time.
Example
Consider a program foo.c with the following content.
main(int argc, char* argv[]) { enum {Morning=10,Noon,Evening}; param_handler P(argc,argv,".myparams"); P.add_parameter("Input File:-f:string:I-File"); P.add_parameter("Rounds:-n:int:10"); P.add_parameter("Start Value:-s:double:0.1"); P.add_parameter("Read from file:-r:bool:false"); P.add_parameter("Daytime:-dt:enum(Morning=10,Noon,Evening):Morning"); param_handler::init_all(); int rounds; P.get_parameter("-n",rounds); for (int i = 0; i < rounds; i++) cout << "hello world\n"; }
After compiling foo.c into foo* you can use the program now in different modes. foo will just iterate 10 times. foo -n 100 will iterate 100 times and store the 100 in .myparams on exit. Another call of foo will iterate 100 times stemming from the entry in .myparams. A call of foo -h will display all possible flags of the program including the init values.