charon-core
0.3.1
|
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 > | |
T | get (std::string parameter) const |
Get the value of a specified parameter. More... | |
template<class T > | |
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 ¶m) 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 ';') | |
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().
Definition at line 68 of file ParameterFile.h.
ParameterFile::ParameterFile | ( | std::string | fileName | ) |
fileName | Name of the plaintext-file to read. |
Definition at line 43 of file ParameterFile.cpp.
References load().
|
private |
parameter | Name of the parameter to store |
value | String representation of the value. |
Definition at line 59 of file ParameterFile.cpp.
References _parameterLines, _params, _setParams, _toLower(), and isSet().
|
inlineprivate |
input | Input string |
Definition at line 146 of file ParameterFile.hxx.
void ParameterFile::erase | ( | std::string | parameter | ) |
parameter | Name of the parameter to remove |
std::invalid_argument | failure 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 | ) |
strm | Input stream to read from |
Definition at line 194 of file ParameterFile.cpp.
References _parameterLines, _set(), sout, and StringTool::trim().
|
inline |
parameter | Name of parameter to get |
invalid_argument | error message in case of unset parameter |
Definition at line 66 of file ParameterFile.hxx.
References _params, and _toLower().
|
inline |
parameter | Name of the parameter to get |
defaultValue | value to set if parameter unset |
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.
param | Parameter name to look for |
Definition at line 138 of file ParameterFile.cpp.
References _parameterLines.
std::vector< std::string > ParameterFile::getKeyList | ( | std::string | beginsWith = "" | ) | const |
beginsWith | Beginning of parameter name |
Definition at line 116 of file ParameterFile.cpp.
References _parameterLines, and _toLower().
|
inline |
The values are sored separated by the delimiter stored in thec orresponding property.
parameter | Name of the parameter to look for |
std::invalid_argument | Error message when parameter not set |
Definition at line 93 of file ParameterFile.hxx.
References _delimiter, _params, _toLower(), and StringTool::explode().
|
inline |
The values are sored separated by the delimiter stored in thec orresponding property.
parameter | Name of the parameter to look for |
defaultValue | Value to set in case of unset parameter |
Definition at line 118 of file ParameterFile.hxx.
References _noFoundWarnings, isSet(), set(), and sout.
|
inline |
parameter | Name of the parameter to check |
Definition at line 40 of file ParameterFile.hxx.
References _params, and _toLower().
bool ParameterFile::load | ( | std::string | fileName | ) |
fileName | Name of the file to read from |
IoError | error description on read errors |
Definition at line 97 of file ParameterFile.cpp.
References clear(), FileTool::exists(), and fromStream().
|
inlinestatic |
fullName | full name descriptor (e.g. "obj1.par1") |
Definition at line 139 of file ParameterFile.hxx.
|
inlinestatic |
fullName | full name descriptor (e.g. "obj1.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 |
fileName | Name of the file to write to. The file will be overwritten. |
IoError | error string on file write-error |
Definition at line 84 of file ParameterFile.cpp.
References toStream().
|
inline |
parameter | Name of the parameter to set |
value | Value to set |
Definition at line 46 of file ParameterFile.hxx.
References _set().
|
inline |
Use getList to restore this list of values.
parameter | Name of the parameter to set |
value | List containing the values to set |
Definition at line 53 of file ParameterFile.hxx.
References _set().
void ParameterFile::setConvertSlashes | ( | bool | convertSlashes | ) |
convertSlashes | New value |
Definition at line 55 of file ParameterFile.cpp.
References _convertSlashes.
void ParameterFile::setDelimiter | ( | char | delimiter | ) |
delimiter | New value |
Definition at line 51 of file ParameterFile.cpp.
References _delimiter.
void ParameterFile::setNotFoundWarningsOn | ( | bool | noFoundWarnings | ) |
noFoundWarnings | New value |
Definition at line 80 of file ParameterFile.cpp.
References _noFoundWarnings.
void ParameterFile::toStream | ( | std::ostream & | strm | ) | const |
strm | Stream to write to |
Definition at line 182 of file ParameterFile.cpp.
References _parameterLines.