util::Parms Class Reference

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

#include <util/parms.h>

List of all members.

Public Member Functions

bool all (const QString &section, std::map< QString, QStringList > &value)
 Retrieve a all parameters with same [section]key.
bool all (const QString &section, std::map< QString, std::vector< QString > > &value)
 Retrieve a all parameters with same [section]key.
bool all (const QString &section, std::map< QString, std::vector< std::string > > &value)
 Retrieve a all parameters with same [section]key.
bool all (const QString &section, std::map< QString, std::vector< double > > &value)
 Retrieve a all parameters with same [section].
bool all (const QString &section, std::map< QString, std::vector< float > > &value)
 Retrieve a all parameters with same [section].
bool all (const QString &section, std::map< QString, std::vector< int > > &value)
 Retrieve a all parameters with same [section].
bool all (const QString &section, std::map< QString, std::vector< bool > > &value)
 Retrieve a all parameters with same [section].
template<typename T , typename Container >
bool all (const QString &section, std::map< QString, Container > &values)
 This operator retrieves all parameters with same [section].
bool all (const QString &section, const QString &key, QStringList &value)
 Retrieve a all parameters with same [section]key.
bool all (const QString &section, const QString &key, std::vector< QString > &value)
 Retrieve a all parameters with same [section]key.
bool all (const QString &section, const QString &key, std::vector< std::string > &value)
 Retrieve a all parameters with same [section]key.
bool all (const QString &section, const QString &key, std::vector< double > &value)
 Retrieve a all parameters with same [section]key.
bool all (const QString &section, const QString &key, std::vector< float > &value)
 Retrieve a all parameters with same [section]key.
bool all (const QString &section, const QString &key, std::vector< int > &value)
 Retrieve a all parameters with same [section]key.
bool all (const QString &section, const QString &key, std::vector< bool > &value)
 Retrieve a all parameters with same [section]key.
template<typename Container >
bool all (const QString &section, const QString &key, Container &value)
 This operator retrieves all parameters with same [section]key.
bool isLoaded () const
 Returns true if the parameter object has been correctly loaded.
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.
bool operator() (const QString &section, const QString &key, QString &value)
 Retrieve a single parameter.
bool operator() (const QString &section, const QString &key, std::string &value)
 Retrieve a single parameter.
bool operator() (const QString &section, const QString &key, double &value)
 Retrieve a single parameter.
bool operator() (const QString &section, const QString &key, float &value)
 Retrieve a single parameter.
bool operator() (const QString &section, const QString &key, int &value)
 Retrieve a single parameter.
bool operator() (const QString &section, const QString &key, bool &value)
 Retrieve a single parameter.
template<typename T >
bool operator() (const QString &section, const QString &key, T &value)
 This operator retrieve a single parameter.
 Parms (const QString &parmFile, int verboseLevel=1)
 Constructor of the parameter file.
void verboseLevel (int vl)
 Change the verbosity level.

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.

Definition at line 105 of file parms.h.


Constructor & Destructor Documentation

util::Parms::Parms ( const QString parmFile,
int  verboseLevel = 1 
)

Constructor of the parameter file.

Definition at line 27 of file parms.cpp.

References verboseLevel().

00028     : ParmFileName( parmFile )
00029   {
00030     verboseLevel( vl );
00031     init();
00032   }


Member Function Documentation

bool 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 )

Definition at line 228 of file parms.cpp.

00229   {
00230     return all<QString>( section, value );
00231   }

bool 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 )

Definition at line 223 of file parms.cpp.

00224   {
00225     return all<QString>( section, value );
00226   }

bool 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 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 )

Definition at line 218 of file parms.cpp.

00219   {
00220     return all<double>( section, value );
00221   }

bool 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 )

Definition at line 213 of file parms.cpp.

00214   {
00215     return all<float>( section, value );
00216   }

bool 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 )

Definition at line 208 of file parms.cpp.

00209   {
00210     return all<int>( section, value );
00211   }

bool 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 )

Definition at line 203 of file parms.cpp.

00204   {
00205     return all<bool>( section, value );
00206   }

template<typename T , typename Container >
bool util::Parms::all ( const QString section,
std::map< QString, Container > &  values 
) [inline]

This operator retrieves all parameters with same [section].

Parameters:
section Section in which the parameter is looked for
value Variable 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.

Definition at line 561 of file parms.h.

References QString::clear(), forall, and QString::size().

00562   {
00563     bool valid = true;
00564     CheckExist = false;
00565     typedef std::map<QString, QStringList>::value_type value_type;
00566     result.clear();
00567     int pos = section.size()+1;
00568     forall( const value_type& pair, Parameters)
00569     {
00570       QString sec(pair.first.mid(0, pos-1));
00571       if(sec != section)
00572         continue;
00573       QString key(pair.first.mid(pos));
00574       Container& value = result[key];
00575       value.clear();
00576       forall( const QString& val, pair.second )
00577       {
00578         T single_value = T();
00579         if( readValue( val, single_value ) )
00580         {
00581           value.push_back( single_value );
00582         }
00583         else
00584         {
00585           if( VerboseLevel > 2 )
00586           {
00587             err << "Parms::all:Error reading key [" << section << "]" << key
00588                 << " with value " << val << endl;
00589           }
00590           valid = false;
00591         }
00592       }
00593     }
00594     CheckExist = true;
00595     return valid;
00596   }

bool 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 )

Definition at line 198 of file parms.cpp.

00199   {
00200     return all<QStringList>( section, key, value );
00201   }

bool 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 )

Definition at line 193 of file parms.cpp.

00194   {
00195     return all<std::vector<QString> >( section, key, value );
00196   }

bool 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 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 )

Definition at line 188 of file parms.cpp.

00189   {
00190     return all<std::vector<double> >( section, key, value );
00191   }

bool 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 )

Definition at line 183 of file parms.cpp.

00184   {
00185     return all<std::vector<float> >( section, key, value );
00186   }

bool 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 )

Definition at line 178 of file parms.cpp.

00179   {
00180     return all<std::vector<int> >( section, key, value );
00181   }

bool 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 )

Definition at line 173 of file parms.cpp.

00174   {
00175     return all<std::vector<bool> >( section, key, value );
00176   }

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

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

Parameters:
section Section in which the parameter is looked for
key Key to look for
value Variable 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.

Definition at line 529 of file parms.h.

References forall.

Referenced by util::GraphInset::readParms(), cell_system::CellSystem< TissueClass, RealModel >::readRules(), and cell_system::CellSystem< TissueClass, RealModel >::readSymbols().

00530   {
00531     bool valid = true;
00532     typedef typename Container::value_type T;
00533     CheckExist = false;
00534     QStringList values;
00535     if( !extractValues( section, key, values ) )
00536       return false;
00537     value.clear();
00538     std::insert_iterator<Container> it(value, value.end());
00539     forall( const QString& val, values )
00540     {
00541       T single_value = T();
00542       if( readValue( val, single_value ) )
00543       {
00544         *it++ = (const T&)single_value;
00545       }
00546       else
00547       {
00548         if( VerboseLevel > 2 )
00549         {
00550           err << "Parms::all:Error reading key [" << section << "]" << key
00551             << " with value " << val << "\n" << flush;
00552         }
00553         valid = false;
00554       }
00555     }
00556     CheckExist = true;
00557     return valid;
00558   }

bool util::Parms::isLoaded (  )  const [inline]

Returns true if the parameter object has been correctly loaded.

Definition at line 124 of file parms.h.

00124 { return loaded; }

template<typename T >
bool util::Parms::operator() ( const QString section,
const QString key,
T &  value,
const T &  def 
) [inline]

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.

Definition at line 510 of file parms.h.

00511   {
00512     bool found = true;
00513     CheckExist = false;
00514     if( !( *this )( section, key, value ) )
00515     {
00516       found = false;
00517       if( VerboseLevel > 2 )
00518       {
00519         err << "Parms::operator()::Info key [" << section << "]"
00520           << key << " not found, using default value" << endl;
00521       }
00522       value = def;
00523     }
00524     CheckExist = true;
00525     return found;
00526   }

bool 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 )

Definition at line 168 of file parms.cpp.

00169   {
00170     return operator()<QString>( section, key, value );
00171   }

bool 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 )

Definition at line 163 of file parms.cpp.

00164   {
00165     return operator()<std::string>( section, key, value );
00166   }

bool 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 )

Definition at line 158 of file parms.cpp.

00159   {
00160     return operator()<double>( section, key, value );
00161   }

bool 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 )

Definition at line 153 of file parms.cpp.

00154   {
00155     return operator()<float>( section, key, value );
00156   }

bool 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 )

Definition at line 148 of file parms.cpp.

00149   {
00150     return operator()<int>( section, key, value );
00151   }

bool 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 )

Definition at line 143 of file parms.cpp.

00144   {
00145     return operator()<bool>( section, key, value );
00146   }

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

This operator retrieve a single parameter.

Parameters:
section Section in which the parameter is looked for
key Key to look for
value Variable 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.

Definition at line 485 of file parms.h.

00486   {
00487     QStringList values;
00488     if( !extractValues( section, key, values ) )
00489       return false;
00490 
00491     if( ( values.size() > 1 ) && ( VerboseLevel > 1 ) )
00492     {
00493       err << "Parms::operator():Warning multiple value for key [" << section << "]"
00494                   << key << ", last one used.\n" << flush;
00495     }
00496 
00497     if( !readValue( values.back(), value ) )
00498     {
00499       if( VerboseLevel > 0 )
00500       {
00501         err << "Parms::operator():Error getting value for key [" << section << "]" <<  key
00502             << " value " << values.back() << "\n" << flush;
00503       }
00504       return false;
00505     }
00506     return true;
00507   }

void util::Parms::verboseLevel ( int  vl  )  [inline]

Change the verbosity level.

See also:
Verbosity

Definition at line 119 of file parms.h.

Referenced by Parms().

00119 { VerboseLevel = ( vl < 0 ) ? 0 : vl; }


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Fri May 31 15:38:34 2013 for VVE by  doxygen 1.6.3