amroc/amr/IO/read-parameters.h File Reference

A simple class for reading some parameter values from a stream. More...

#include "mutator.h"
#include <iosfwd>
#include <string>

Go to the source code of this file.

Classes

class  MutableVars

Functions

template<class T >
void AddVar (MutableVars &MV, const std::string &name, T &v)
template<class T >
void AddVar (MutableVars &MV, const char *name, T &v)

Detailed Description

A simple class for reading some parameter values from a stream.

Purpose: say you have a number of numerical parameters declared in your program, i.e. an instance of a class for which you want sometimes to read in a different value than the predefined one. Then you could set up a huge parameter file which is scanned at startup time and where you give a value for each variable you want to be able to change sometimes. This is very errorprone, because you have to remember the ordering of the variables, the parameter file is not well readable (you cannot associate values with variables) and is larger then it has to be.

A better solution is the following: You give an unique name ( a string) to every variable you declare and want to be changeable. The parameterfile then contains only the names and values of the variables that are really subject to changes, the ordering does not matter, and you may easily read your parameter files. The only thing you have to do is to define operator >> for each class type you want to read in. The class that achieves this is MutableVars: you add the names and references to a variable you want to be mutable, and then you may read in parameter files. How does this look like in a piece of code?
    MutableVars MV; // define one instance of the bookkeeping class 
    double x;
    myclass A; // a variable of some class readable with >> A
    AddVar(MV, "x",x);  // add x to database
    AddVar(MV, "A",A);  // add A to database
    ...
    MV.ReadValues(param_file); // read a parameter file
    ...
   
In the parameter file, you may (but do not have to!!) write the following:
    x   0.123456
    ...
    A   <data of A> 
   
where data of A is what you would use when you want to read A directly: cin >> A;
Author:
Guntram Berti
Date:
09/01/96

Function Documentation

template<class T >
void AddVar ( MutableVars MV,
const char *  name,
T &  v 
) [inline]

References AddVar().

template<class T >
void AddVar ( MutableVars MV,
const std::string &  name,
T &  v 
) [inline]

References MutableVars::AddVariable(), and GetMutator().

Referenced by AddVar().

Generated on Thu Jun 30 02:19:01 2016 for AMROC's Parameter IO by  doxygen 1.6.3