#include <Params.h>
Public Member Functions | |
| void | setDefault () |
| Set the default values for parameters. | |
| bool | scanFrom (std::istream &is) |
| Read values for parameters from the input stream "is". | |
| bool | scanFrom (const std::string &name) |
| Read values for parameters from the configuration file "name". | |
| bool | printTo (std::ostream &os) |
| Write values for parameters to the output stream "os". | |
| bool | printTo (const std::string &name) |
| Write values for parameters to the configuration file "name". | |
| virtual | ~Params () |
| Default destructor, that must be overloaded by the derived class. | |
Protected Member Functions | |
| Params (int argc=0, char **argv=NULL) | |
| Checks whether a configuration file is given. | |
| virtual void | io (std::istream &, std::ostream &, FileUtil::iotype)=0 |
| Interface between the methods of this class and the function FileUtil::io. | |
Protected Attributes | |
| std::string | confFile |
| Name of the configuration file. | |
If you have written a program that can be used for different situations you often have the problem of changing the content of the variables in your program that will define the situation the program is used for.
You must change the content of the variables to adapt it to a special situation and recompile your program.
More easily is the usage of configuration files, that will include predefined values for your program for different situations. This class here offers methods for easily using such configuration files. For flexibility (i.e. different formats of configuration files), this class can only be used by deriving your own class from it. Class Params has the role of an interface between your derived class and the functions offered by the file FileUtil.h (there you will also find a more detailed description of the general format of configuration files). For a simple example of deriving your own class from Params, see method io. Or take a look at a complete derivation (class NetParams) that is used for the creation of neural networks in package ReClaM.
Definition at line 84 of file Params.h.
| virtual Params::~Params | ( | ) | [inline, virtual] |
| Params::Params | ( | int | argc = 0, |
|
| char ** | argv = NULL | |||
| ) | [protected] |
Checks whether a configuration file is given.
The main program you use to include your own class, derived from class Params must take at least two additional parameters, when called. One of these parameter is "-conf", a flag that denotes, that the following parameter will be the name of a configuration file. The additional parameters are then used by this constructor to check for the occurrence of the mentioned flag and the name of the configuration file. If both parameters are found, the name of the configuration file is saved internally.
| argc | Number of arguments stored in argv. | |
| argv | The parameters (arguments) taken from the main program. |
#include "FileUtil/Params.h" // My own class, derived from the Params class: // class MyParams : public Params { public: // The derived constructor, that will display the // name of the configuration file or an error // message, if no filename was found: MyParams( int argc, char **argv ) : Params( argc, argv ) { if ( confFile.size( ) > 0 ) { cout << "Name of the configuration file: " << confFile << endl; } else { cerr << "No configuration file given!" << endl; } } // The derived destructor: ~MyParams( ) { } // This function must be derived, when using class Params: void io( std::istream& is, std::ostream& os, FileUtil::iotype type ) { ... } }; // End of derived class. // Call the main program with parameters "-conf [filename]": void main( int argc, char **argv ) { // Pass the parameters of the main program to the // constructor: MyParams param( argc, argv ); }
Definition at line 119 of file Params.cpp.
References confFile.
| virtual void Params::io | ( | std::istream & | , | |
| std::ostream & | , | |||
| FileUtil::iotype | ||||
| ) | [protected, pure virtual] |
Interface between the methods of this class and the function FileUtil::io.
This method must be overloaded by the derived class and will function as interface between the methods of this class and the io-function offered by FileUtil.h.
The implementation of the derived class method will then consist of several commands, each command is responsible for one parameter saved in the used configuration file.
Each of these commands will have the format:
FileUtil::io[_strict]( is, os, token, variable, default, type );
You can use FileUtil::io for configuration files with a more liberate format or FileUtil::io_strict for configuration files with a very strict format.
#include "FileUtil/Params.h" // My own derived class for managing my configuration files: // class MyParams : public Params { public: // Overload default constructor: MyParams( int argc, char **argv ) : Params( argc, argv ) { } // Guess, my configuration file includes the definition // for only one parameter, named "MyParam1" in the // file, but marking the value for the class variable // "my_param_1" (see below). The default value of this // class variable is "10.0": void io( std::istream& is, std::ostream& os, FileUtil::iotype type ) { FileUtil::io( is, os, "MyParam1", my_param_1, 10.0, type ); } // Method to show the current content of the class variable: void monitor( ) { cout << "Value of \"my_param_1\" = " << my_param_1 << endl; } private: // The variable with its value defined in the configuration // file: double my_param_1; }; void main( int argc, char **argv ) { MyParams param( argc, argv ); param.setDefault( ); param.printTo( "test.conf" ); param.monitor( ); }
Referenced by printTo(), scanFrom(), and setDefault().
| bool Params::printTo | ( | const std::string & | name | ) |
Write values for parameters to the configuration file "name".
Depending on your overloaded version of io, the values of parameters together with their identifying token names will be written to the configuration file name. See the example program for io, for a further description.
| name | The name of the configuration file. |
Definition at line 273 of file Params.cpp.
References printTo().
| bool Params::printTo | ( | std::ostream & | os | ) |
Write values for parameters to the output stream "os".
Depending on your overloaded version of io, the values of parameters together with their identifying token names will be written to the output stream os. See the example program for io, for a further description.
| os | The output stream. |
Definition at line 243 of file Params.cpp.
References io(), and FileUtil::PrintTo.
Referenced by printTo().
| bool Params::scanFrom | ( | const std::string & | name | ) |
Read values for parameters from the configuration file "name".
Read values for parameters from the configuration file "name".
Depending on your overloaded version of io, the values of parameters will be read from the configuration file name. See the example program for io, for a further description.
| name | The name of the configuration file. |
Definition at line 208 of file Params.cpp.
References FileUtil::readfile(), and scanFrom().
| bool Params::scanFrom | ( | std::istream & | is | ) |
Read values for parameters from the input stream "is".
Depending on your overloaded version of io, the values of parameters will be read from the input stream is. See the example program for io, for a further description.
| is | The input stream. |
Definition at line 178 of file Params.cpp.
References io(), and FileUtil::ScanFrom.
Referenced by scanFrom().
| void Params::setDefault | ( | ) |
Set the default values for parameters.
Depending on your overloaded version of io, parameters will be initialized by default values. See the example program for io, for a further description.
Definition at line 151 of file Params.cpp.
References io(), and FileUtil::SetDefault.
std::string Params::confFile [protected] |