charon-core  0.3.1
ParameterFile Class Reference

This class serves to store parameters used within the Charon Project. More...

#include <charon-core/ParameterFile.h>

Classes

class  IoError
 I/O error exception thrown by ParameterFile instances. More...
 
class  Unset
 Exception thrown when trying to access unset parameters. More...
 

Public Member Functions

 ParameterFile ()
 Default constructor.
 
 ParameterFile (std::string fileName)
 Initialize Parameterfile reading content of an existing file. More...
 
 ~ParameterFile ()
 Default destructor.
 
void clear ()
 Clear parameter list.
 
void erase (std::string parameter)
 Delete a parameter from the parameter list. More...
 
void fromStream (std::istream &strm)
 Restore parameters reading from the given stream. More...
 
template<>
std::string get (std::string parameter) const
 specialization for return type std::string
 
template<>
bool get (std::string parameter) const
 specialization for return type bool
 
template<class T >
get (std::string parameter) const
 Get the value of a specified parameter. More...
 
template<class T >
get (std::string parameter, T defaultValue)
 Get the value of a specified parameter. More...
 
charon_DEPRECATED std::vector
< std::string > 
getEveryParameter (const std::string &param) const
 Look for parameters ignoring the instance name. More...
 
std::vector< std::string > getKeyList (std::string beginsWith="") const
 Look for parameters beginning with a given string. More...
 
template<>
std::vector< std::string > getList (std::string parameter) const
 specialization for return type std::vector<std::string>
 
template<class T >
std::vector< T > getList (std::string parameter) const
 If multiple values are set, return a list containing these values. More...
 
template<class T >
std::vector< T > getList (std::string parameter, std::string defaultValue)
 If multiple values are set, return a list containing these values. More...
 
bool isSet (std::string parameter) const
 Check if a givem parameter has already been set. More...
 
bool load (std::string fileName)
 Load parameters from the given file. More...
 
void resetSetParams ()
 Clear list of modifications. More...
 
bool save (std::string fileName) const
 Save parameters and values to a plain text file. More...
 
template<class T >
void set (std::string parameter, const T &value=T())
 Set a parameter to the given single value. More...
 
template<class T >
void set (std::string parameter, const std::vector< T > &value)
 Set a parameter to multiple values. More...
 
void setConvertSlashes (bool convertSlashes)
 Change property convertSlashes. More...
 
void setDelimiter (char delimiter)
 Change property delimiter. More...
 
void setNotFoundWarningsOn (bool noFoundWarnings)
 Set property noFoundWarnings. More...
 
void showSetParams () const
 Print a list of all set parameters to sout.
 
void toStream (std::ostream &strm) const
 Save parameters in the same order as they where inserted. More...
 

Static Public Member Functions

static charon_DEPRECATED
std::string 
objName (std::string fullName)
 get object name component of a full name More...
 
static charon_DEPRECATED
std::string 
parName (std::string fullName)
 get parameter component of a full name More...
 

Private Member Functions

void _set (std::string parameter, std::string value)
 Store a string value to the parameter list. More...
 
void _toLower (std::string &input) const
 Get the lowercase version of a string. More...
 

Private Attributes

Parameter store
std::vector< std::string > _parameterLines
 this vector keeps a copy of all parameters in order to preserve their ordering in the actual file it is used for saving the file in the same order and appending new parameters at the end of the file
 
std::map< std::string,
std::string > 
_params
 maps parameters to their respective values
 
std::map< std::string,
std::string > 
_setParams
 stores modified and new parameters in a seperate map (see showSetParams())
 
Properties to change behaviour
bool _noFoundWarnings
 warn if parameter was not found
 
bool _convertSlashes
 convert linux to windows slashes and vice versa (depends on define CHARON_LINUX/CHARON_WINDOWS in StringTool)
 
char _delimiter
 delimiter for lists of values (default is ';')
 

Detailed Description

Parameters of different types can be stored and loaded from a plain text file.

The parameters are stored in an std::map<std::string, std::string>. So you can only store parameter types which can be transformed into string representations (using the ostream operator<<()) and be restored (using the istream operator>>()). A second map serves to save information which parameters have been newly set or modified. These information can be retrieved using showSetParams(). All other function operate only on the real parameter store. You can reset this second list (without losing the stored parameters) by calling resetSetParams().

Remarks
The names of the parameters are case insensitive.
You can reassign SplitStream sout to redirect debugging messages into logfiles etc.

Definition at line 68 of file ParameterFile.h.

Constructor & Destructor Documentation

ParameterFile::ParameterFile ( std::string  fileName)
Parameters
fileNameName of the plaintext-file to read.

Definition at line 43 of file ParameterFile.cpp.

References load().

Member Function Documentation

void ParameterFile::_set ( std::string  parameter,
std::string  value 
)
private
Parameters
parameterName of the parameter to store
valueString representation of the value.
Remarks
Modified parameter (when overwriting an existing value) is marked as modified.

Definition at line 59 of file ParameterFile.cpp.

References _parameterLines, _params, _setParams, _toLower(), and isSet().

void ParameterFile::_toLower ( std::string &  input) const
inlineprivate
Parameters
inputInput string
Returns
string transformed to lowercase

Definition at line 146 of file ParameterFile.hxx.

void ParameterFile::erase ( std::string  parameter)
Parameters
parameterName of the parameter to remove
Exceptions
std::invalid_argumentfailure description if parameter is not set

Definition at line 241 of file ParameterFile.cpp.

References _parameterLines, _params, _setParams, _toLower(), and isSet().

void ParameterFile::fromStream ( std::istream &  strm)
Parameters
strmInput stream to read from

Definition at line 194 of file ParameterFile.cpp.

References _parameterLines, _set(), sout, and StringTool::trim().

template<class T >
T ParameterFile::get ( std::string  parameter) const
inline
Parameters
parameterName of parameter to get
Returns
Found value
Exceptions
invalid_argumenterror message in case of unset parameter

Definition at line 66 of file ParameterFile.hxx.

References _params, and _toLower().

template<class T >
T ParameterFile::get ( std::string  parameter,
defaultValue 
)
inline
Parameters
parameterName of the parameter to get
defaultValuevalue to set if parameter unset
Returns
Found value

Definition at line 80 of file ParameterFile.hxx.

References _noFoundWarnings, isSet(), and sout.

std::vector< std::string > ParameterFile::getEveryParameter ( const std::string &  param) const

For example, getEveryParameter("type") will return a vector every "type" parameter in this ParameterFile.

Parameters
paramParameter name to look for
Returns
List containing the found parameters. This vector has the same sorting as the lines in the textfile.

Definition at line 138 of file ParameterFile.cpp.

References _parameterLines.

std::vector< std::string > ParameterFile::getKeyList ( std::string  beginsWith = "") const
Parameters
beginsWithBeginning of parameter name
Returns
List containing the found parameters. This vector has the same sorting as the lines in the textfile.

Definition at line 116 of file ParameterFile.cpp.

References _parameterLines, and _toLower().

template<class T >
std::vector< T > ParameterFile::getList ( std::string  parameter) const
inline

The values are sored separated by the delimiter stored in thec orresponding property.

Parameters
parameterName of the parameter to look for
Returns
List containing the found values
Exceptions
std::invalid_argumentError message when parameter not set

Definition at line 93 of file ParameterFile.hxx.

References _delimiter, _params, _toLower(), and StringTool::explode().

template<class T >
std::vector< T > ParameterFile::getList ( std::string  parameter,
std::string  defaultValue 
)
inline

The values are sored separated by the delimiter stored in thec orresponding property.

Parameters
parameterName of the parameter to look for
defaultValueValue to set in case of unset parameter
Returns
List containing the found values

Definition at line 118 of file ParameterFile.hxx.

References _noFoundWarnings, isSet(), set(), and sout.

bool ParameterFile::isSet ( std::string  parameter) const
inline
Parameters
parameterName of the parameter to check
Returns
True if value already present

Definition at line 40 of file ParameterFile.hxx.

References _params, and _toLower().

bool ParameterFile::load ( std::string  fileName)
Parameters
fileNameName of the file to read from
Returns
True on successful read
Exceptions
IoErrorerror description on read errors

Definition at line 97 of file ParameterFile.cpp.

References clear(), FileTool::exists(), and fromStream().

std::string ParameterFile::objName ( std::string  fullName)
inlinestatic
Parameters
fullNamefull name descriptor (e.g. "obj1.par1")
Returns
object name part (e.g. "obj1")

Definition at line 139 of file ParameterFile.hxx.

std::string ParameterFile::parName ( std::string  fullName)
inlinestatic
Parameters
fullNamefull name descriptor (e.g. "obj1.par1")
Returns
parameter name part (e.g. "par1")

Definition at line 132 of file ParameterFile.hxx.

void ParameterFile::resetSetParams ( )

This does not change the stored parameters.

Definition at line 76 of file ParameterFile.cpp.

References _setParams.

bool ParameterFile::save ( std::string  fileName) const
Parameters
fileNameName of the file to write to. The file will be overwritten.
Returns
True on a successful write
Exceptions
IoErrorerror string on file write-error

Definition at line 84 of file ParameterFile.cpp.

References toStream().

template<class T >
void ParameterFile::set ( std::string  parameter,
const T &  value = T() 
)
inline
Parameters
parameterName of the parameter to set
valueValue to set

Definition at line 46 of file ParameterFile.hxx.

References _set().

template<class T >
void ParameterFile::set ( std::string  parameter,
const std::vector< T > &  value 
)
inline

Use getList to restore this list of values.

Parameters
parameterName of the parameter to set
valueList containing the values to set

Definition at line 53 of file ParameterFile.hxx.

References _set().

void ParameterFile::setConvertSlashes ( bool  convertSlashes)
Parameters
convertSlashesNew value

Definition at line 55 of file ParameterFile.cpp.

References _convertSlashes.

void ParameterFile::setDelimiter ( char  delimiter)
Parameters
delimiterNew value

Definition at line 51 of file ParameterFile.cpp.

References _delimiter.

void ParameterFile::setNotFoundWarningsOn ( bool  noFoundWarnings)
Parameters
noFoundWarningsNew value

Definition at line 80 of file ParameterFile.cpp.

References _noFoundWarnings.

void ParameterFile::toStream ( std::ostream &  strm) const
Parameters
strmStream to write to

Definition at line 182 of file ParameterFile.cpp.

References _parameterLines.


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