Params Class Reference

This class offers methods for easily using configuration files to change the values of variables in your programs. More...

#include <Params.h>

List of all members.

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.


Detailed Description

This class offers methods for easily using configuration files to change the values of variables in your programs.

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.

Author:
M. Kreutz
Date:
1998
Changes:
none
Status:
stable
Examples:

FileUtilClass.cpp.

Definition at line 84 of file Params.h.


Constructor & Destructor Documentation

virtual Params::~Params (  )  [inline, virtual]

Default destructor, that must be overloaded by the derived class.

Overload this virtual destructor in your class derived from class Params. If you don't overload the destructor, the default destructor will be used.

Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable

Definition at line 122 of file Params.h.

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.

Parameters:
argc Number of arguments stored in argv.
argv The parameters (arguments) taken from the main program.
Returns:
An instance of class Params.
Example
  #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 );
  }
Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable
Examples:
FileUtilClass.cpp.

Definition at line 119 of file Params.cpp.

References confFile.


Member Function Documentation

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.

  • is, os and type will have the same name as the parameters used in the derived class' io-method.
  • token is the string used in the configuration file to identify the current parameter.
  • variable is the name of the parameter used in your code
  • default is the value that will be assigned to the parameter as default value.

Example
  #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( );
  }
Here we use the program only to assign the given default value to the class variable and then write this default value together with its identifying token name to the configuration file "test.conf".
The principle is transferable to other configuration files.

Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable
Examples:
FileUtilClass.cpp.

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.

Parameters:
name The name of the configuration file.
Returns:
"false", if an error occured while writing to name, "true" otherwise.
Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable

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.

Parameters:
os The output stream.
Returns:
"false", if an error occured while writing to os, "true" otherwise.
Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable

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.

Parameters:
name The name of the configuration file.
Returns:
"false", if an error occured while reading from name, "true" otherwise.
Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable

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.

Parameters:
is The input stream.
Returns:
"false", if an error occured while reading from is, "true" otherwise.
Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable
Examples:
FileUtilClass.cpp.

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.

Returns:
none
Author:
M. Kreutz
Date:
1998
Changes
none
Status
stable
Examples:
FileUtilClass.cpp.

Definition at line 151 of file Params.cpp.

References io(), and FileUtil::SetDefault.


Member Data Documentation

std::string Params::confFile [protected]

Name of the configuration file.

Definition at line 128 of file Params.h.

Referenced by Params().


The documentation for this class was generated from the following files: