MorphoGraphX
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Public Member Functions | List of all members
mgx::util::Parms Class Reference

A utility class to parse L-Studio like parameter files. More...

#include <Parms.hpp>

Public Member Functions

 Parms (int verboseLevel=1)
 Default constructor, with no file.
 
 Parms (const QString &parmFile, int verboseLevel=1)
 Constructor of the parameter file.
 
void verboseLevel (int vl)
 Change the verbosity level. More...
 
bool isLoaded () const
 Returns true if the parameter object has been correctly loaded.
 
bool empty () const
 Returns true if the parameter file was empty (e.g. More...
 
template<typename T >
bool operator() (const QString &section, const QString &key, T &value)
 This operator retrieve a single parameter. More...
 
bool operator() (const QString &section, const QString &key, bool &value)
 Retrieve a single parameter. More...
 
bool operator() (const QString &section, const QString &key, int &value)
 Retrieve a single parameter. More...
 
bool operator() (const QString &section, const QString &key, float &value)
 Retrieve a single parameter. More...
 
bool operator() (const QString &section, const QString &key, double &value)
 Retrieve a single parameter. More...
 
bool operator() (const QString &section, const QString &key, std::string &value)
 Retrieve a single parameter. More...
 
bool operator() (const QString &section, const QString &key, QString &value)
 Retrieve a single parameter. More...
 
template<typename T >
bool operator() (const QString &section, const QString &key, T &value, const T &def)
 Variation on the previous, but if the [section]key is not found, an information message is issued (instead of an error) and value is set up to def.
 
template<typename Container >
bool all (const QString &section, const QString &key, Container &value)
 This operator retrieves all parameters with same [section]key. More...
 
bool all (const QString &section, const QString &key, std::vector< bool > &value)
 Retrieve a all parameters with same [section]key. More...
 
bool all (const QString &section, const QString &key, std::vector< int > &value)
 Retrieve a all parameters with same [section]key. More...
 
bool all (const QString &section, const QString &key, std::vector< float > &value)
 Retrieve a all parameters with same [section]key. More...
 
bool all (const QString &section, const QString &key, std::vector< double > &value)
 Retrieve a all parameters with same [section]key. More...
 
bool all (const QString &section, const QString &key, std::vector< std::string > &value)
 Retrieve a all parameters with same [section]key. More...
 
bool all (const QString &section, const QString &key, std::vector< QString > &value)
 Retrieve a all parameters with same [section]key. More...
 
bool all (const QString &section, const QString &key, QStringList &value)
 Retrieve a all parameters with same [section]key. More...
 
template<typename T , typename Container >
bool all (const QString &section, std::map< QString, Container > &values)
 This operator retrieves all parameters with same [section]. More...
 
bool all (const QString &section, std::map< QString, std::vector< bool > > &value)
 Retrieve a all parameters with same [section]. More...
 
bool all (const QString &section, std::map< QString, std::vector< int > > &value)
 Retrieve a all parameters with same [section]. More...
 
bool all (const QString &section, std::map< QString, std::vector< float > > &value)
 Retrieve a all parameters with same [section]. More...
 
bool all (const QString &section, std::map< QString, std::vector< double > > &value)
 Retrieve a all parameters with same [section]. More...
 
bool all (const QString &section, std::map< QString, std::vector< std::string > > &value)
 Retrieve a all parameters with same [section]key. More...
 
bool all (const QString &section, std::map< QString, std::vector< QString > > &value)
 Retrieve a all parameters with same [section]key. More...
 
bool all (const QString &section, std::map< QString, QStringList > &value)
 Retrieve a all parameters with same [section]key. More...
 

Detailed Description

A utility class to parse L-Studio like parameter files.

Format of the parameter file

The basic information in the parameter file is a key associated to a value, possibly with a C++-like comment:

key: value // Comment

Keys can be organized in sections:

[Section1]
key1: value1
key2: value2
...
[Section2]
key1: value3
key2: value4
...

Empty line or comment-only line are ignored, any other line will raise an error. The default section is named with an empty string "".

Usage example of util::Parm

Here is an example:

int a,b;
QString s1, s2;
std::vector<double> all_d;
util::Parms parms( "view.v", 2 );
// First read simple parameters
parms( "", "a", a );
parms( "Main", "b", b, 0 );
parms( "Main", "string1", s1 );
parms( "Main", "string2", s2 );
// Then read all the keys "double" in section "Values"
parms.all( "Values, "double", all_d);

Reading typed parameters

To read user-defined typed as parameter, it is enough to overload the function

QTextStream& operator>>( QTextStream&, const T& ).

T being the type of the parameter to read.

Key duplicates

If the same key is used may times in the same section, either all the values can be retrieved using the Parms::all() method, or only the last can be retrieved using the normal Parms::operator()().

Verbosity

The class accepts 5 verbosity levels:

Debug information output the raw string before evaluation to set up a parameter.

Member Function Documentation

template<typename Container >
bool mgx::util::Parms::all ( const QString section,
const QString key,
Container &  value 
)

This operator retrieves all parameters with same [section]key.

Parameters
sectionSection in which the parameter is looked for
keyKey to look for
valueVariable to set up

value is filled with the different values found having same [section]key. If none, value is simply empty. The only error that can arise is a reading error, if one parameter has invalid value. This parameter will simply be ignored, all other parameters being read.

Returns
True if there was no error while converting the different parameters from their string representation.
bool mgx::util::Parms::all ( const QString section,
const QString key,
std::vector< bool > &  value 
)

Retrieve a all parameters with same [section]key.

See Also
all( const QString& section, const QString& key, std::vector<T>& value )
bool mgx::util::Parms::all ( const QString section,
const QString key,
std::vector< int > &  value 
)

Retrieve a all parameters with same [section]key.

See Also
all( const QString& section, const QString& key, std::vector<T>& value )
bool mgx::util::Parms::all ( const QString section,
const QString key,
std::vector< float > &  value 
)

Retrieve a all parameters with same [section]key.

See Also
all( const QString& section, const QString& key, std::vector<T>& value )
bool mgx::util::Parms::all ( const QString section,
const QString key,
std::vector< double > &  value 
)

Retrieve a all parameters with same [section]key.

See Also
all( const QString& section, const QString& key, std::vector<T>& value )
bool mgx::util::Parms::all ( const QString section,
const QString key,
std::vector< std::string > &  value 
)

Retrieve a all parameters with same [section]key.

See Also
all( const QString& section, const QString& key, std::vector<T>& value )
bool mgx::util::Parms::all ( const QString section,
const QString key,
std::vector< QString > &  value 
)

Retrieve a all parameters with same [section]key.

See Also
all( const QString& section, const QString& key, std::vector<T>& value )
bool mgx::util::Parms::all ( const QString section,
const QString key,
QStringList value 
)

Retrieve a all parameters with same [section]key.

See Also
all( const QString& section, const QString& key, std::vector<T>& value )
template<typename T , typename Container >
bool mgx::util::Parms::all ( const QString section,
std::map< QString, Container > &  values 
)

This operator retrieves all parameters with same [section].

Parameters
sectionSection in which the parameter is looked for
valueVariable to set up

value is filled with the different keys found in [section] and, for each key, the vector of all the values associated to it. If none, value is simply empty. The only error that can arise is a reading error, if one parameter has invalid value. This parameter will simply be ignored, all other parameters being read.

Returns
True if there was no error while converting the different parameters from their string representation.
bool mgx::util::Parms::all ( const QString section,
std::map< QString, std::vector< bool > > &  value 
)

Retrieve a all parameters with same [section].

See Also
all( const QString& section, std::map<QString, std::vector<T> >& value )
bool mgx::util::Parms::all ( const QString section,
std::map< QString, std::vector< int > > &  value 
)

Retrieve a all parameters with same [section].

See Also
all( const QString& section, std::map<QString, std::vector<T> >& value )
bool mgx::util::Parms::all ( const QString section,
std::map< QString, std::vector< float > > &  value 
)

Retrieve a all parameters with same [section].

See Also
all( const QString& section, std::map<QString, std::vector<T> >& value )
bool mgx::util::Parms::all ( const QString section,
std::map< QString, std::vector< double > > &  value 
)

Retrieve a all parameters with same [section].

See Also
all( const QString& section, std::map<QString, std::vector<T> >& value )
bool mgx::util::Parms::all ( const QString section,
std::map< QString, std::vector< std::string > > &  value 
)

Retrieve a all parameters with same [section]key.

See Also
all( const QString& section, std::map<QString, std::vector<T> >& value )
bool mgx::util::Parms::all ( const QString section,
std::map< QString, std::vector< QString > > &  value 
)

Retrieve a all parameters with same [section]key.

See Also
all( const QString& section, std::map<QString, std::vector<T> >& value )
bool mgx::util::Parms::all ( const QString section,
std::map< QString, QStringList > &  value 
)

Retrieve a all parameters with same [section]key.

See Also
all( const QString& section, std::map<QString, std::vector<T> >& value )
bool mgx::util::Parms::empty ( ) const
inline

Returns true if the parameter file was empty (e.g.

no valid values were found)

template<typename T >
bool mgx::util::Parms::operator() ( const QString section,
const QString key,
T &  value 
)

This operator retrieve a single parameter.

Parameters
sectionSection in which the parameter is looked for
keyKey to look for
valueVariable to set up if possible

If the [section]key exists, value is set up to the parameter value. Any key placed before the first section is considered in the first secion. If the [section]key is multiply defined, only the last one is taken and a warning is issued (see Verbosity). If the [section]key does not exist, or its content cannot be interpreted as the requested type, then ar error is issued.

Returns
True if there was no error while converting the different parameters from their string representation and the [section]key existes.
bool mgx::util::Parms::operator() ( const QString section,
const QString key,
bool &  value 
)

Retrieve a single parameter.

Boolean value must read "true" or "false", case being meaningless.

See Also
operator()( const QString& section, const QString& key, T& value )
bool mgx::util::Parms::operator() ( const QString section,
const QString key,
int &  value 
)

Retrieve a single parameter.

See Also
operator()( const QString& section, const QString& key, T& value )
bool mgx::util::Parms::operator() ( const QString section,
const QString key,
float &  value 
)

Retrieve a single parameter.

See Also
operator()( const QString& section, const QString& key, T& value )
bool mgx::util::Parms::operator() ( const QString section,
const QString key,
double &  value 
)

Retrieve a single parameter.

See Also
operator()( const QString& section, const QString& key, T& value )
bool mgx::util::Parms::operator() ( const QString section,
const QString key,
std::string &  value 
)

Retrieve a single parameter.

For string, the value is the whole line with whitespaces and comment stripped before and after the parameter.

See Also
operator()( const QString& section, const QString& key, T& value )
bool mgx::util::Parms::operator() ( const QString section,
const QString key,
QString value 
)

Retrieve a single parameter.

For string, the value is the whole line with whitespaces and comment stripped before and after the parameter.

See Also
operator()( const QString& section, const QString& key, T& value )
void mgx::util::Parms::verboseLevel ( int  vl)
inline

Change the verbosity level.

See Also
Verbosity

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