walberla::config::Config Class Reference

Detailed Description

Configuration handler & parameter file parser.

Author
Klaus Iglberger

The Config class parses parameter files and stores value-parameter pairs. These files may contain an arbitrary number of parameters in any order of the following form:
key value;
Additionally, the files may contain an arbitrary number of parameter blocks of the following form:
key { ... },
which can again contain an arbitrary number of parameters and parameter blocks in any order.
During the parameter extraction, the file parser checks for duplicate parameters within a single parameter block (which also includes the global block). Furthermore, the file parser is case insensitive with respect to the used keys. Therefore 'key', 'KEY' and 'Key' are handled equally and are recognized as duplicate parameters.

#include <Config.h>

Classes

class  Block
 Parameter block. More...
 
class  BlockHandle
 Handle for a Block object. More...
 
class  Parameter
 

Public Types

enum  ErrorCode { noerror = 0, undefined = 1, badcast = 2 }
 
typedef std::vector< BlockHandleBlocks
 Container for block handles. More...
 
typedef List::size_type size_type
 Size type for a Block count. More...
 
typedef Map::iterator iterator
 Iterator over the contained parameters. More...
 
typedef Map::const_iterator const_iterator
 Constant iterator over the contained parameters. More...
 

Public Member Functions

 Config ()
 Default constructor for the Config class. More...
 
 ~Config ()
 Destructor for the Config class. More...
 
template<typename Type >
Config::Parameter< Type > getParameter (const std::string &key) const
 
Operators
Configoperator= (const Config &c)
 Copy assignment operator for the Config class. More...
 
 operator bool () const
 Conversion operator for the Config class. More...
 
Error functions
void clear ()
 Clearing the internal status of the file config object. More...
 
std::string error () const
 Returns the error messages. More...
 
Get functions
bool isDefined (std::string key) const
 Checks if a parameter was defined in the parameter file. More...
 
template<typename T >
Parameter< T > getParameter (const std::string &key) const
 Returns an extracted parameter. More...
 
template<typename T >
Parameter< T > getParameter (const std::string &key, const T &defaultValue) const
 Returns an extracted parameter. More...
 
iterator begin ()
 Returns an iterator to the first globally defined parameter. More...
 
const_iterator begin () const
 Returns an iterator to the first globally defined parameter. More...
 
iterator end ()
 Returns an iterator one past the last globally defined parameter. More...
 
const_iterator end () const
 Returns an iterator one past the last globally defined parameter. More...
 
size_type getNumBlocks () const
 
size_type getNumBlocks (const std::string &key) const
 
BlockHandle getBlock (const std::string &key) const
 
BlockHandle getOneBlock (const std::string &key) const
 
void getBlocks (const std::string &key, Blocks &blocks, size_t min=0, size_t max=std::numeric_limits< size_t >::max()) const
 Adds to the given blocks all extracted blocks with key key. More...
 
void getBlocks (Blocks &blocks) const
 Adds to the given blocks all globally defined blocks. More...
 
BlockHandle getGlobalBlock () const
 gives a handle to the global block. More...
 
BlockgetWritableGlobalBlock ()
 returns the global block. More...
 

Private Types

typedef std::string Key
 Parameter key. More...
 
typedef std::string Value
 Parameter value. More...
 
typedef std::map< Key, Value, CaseInsensitiveCompareMap
 Parameter map. More...
 
typedef std::list< BlockList
 List for parameter blocks. More...
 
typedef std::map< Value, ValueValueReplacementMap
 Parameter value replacement map. More...
 
typedef std::stringstream::pos_type sstreamPos
 Stream position. More...
 
typedef std::pair< sstreamPos, unsigned int > Pair
 Pair consisting of a stream position and a line number. More...
 
typedef std::vector< PairLineVector
 Vector for Pair. More...
 

Private Attributes

Member variables
bool stateFlag_
 Internal status of the config object. More...
 
std::ostringstream error_
 Container for all error messages. More...
 
Block block_
 The global parameter block. More...
 
ValueReplacementMap valueReplacements_
 value replacements More...
 

Static Private Attributes

static const unsigned int maxInclusionLevel = 30
 

Utility functions

void readParameterFile (const char *const filename)
 Extraction of the parameter file filename. More...
 
void listParameters () const
 Output function for the contained parameters. More...
 
void addValueReplacement (const Value &original, const Value &replacement)
 
static void convertToLowerCase (std::string &s)
 Conversion of the character in s to lower case characters. More...
 
static std::string getDirectory (const std::string &path)
 Returns the directory part of the given path. More...
 
static std::string getFilename (const std::string &path)
 Returns the filename part of the given path. More...
 
void removeTrailingWhiteSpaces (std::string &s)
 Removes the trailing white spaces from the given string. More...
 
unsigned int getLineNumber (const LineVector &lineNumbers, sstreamPos pos) const
 Calculation of the line number of a command. More...
 
void parseFromFile (const char *const filename, Block &block, unsigned int level)
 Parses the given file and extracts the parameters to the given block. More...
 
void parseFromString (const std::string &str, Block &block, unsigned int level)
 Parses the given file and extracts the parameters to the given block. More...
 
void extractBlock (const char *filename, std::stringstream &input, Block &block, const LineVector &lineNumbers, unsigned int lineNumber, unsigned int level)
 Extracting a parameter block from the preprocessed input stream. More...
 

Member Typedef Documentation

Container for block handles.

typedef Map::const_iterator walberla::config::Config::const_iterator

Constant iterator over the contained parameters.

typedef Map::iterator walberla::config::Config::iterator

Iterator over the contained parameters.

typedef std::string walberla::config::Config::Key
private

Parameter key.

typedef std::vector<Pair> walberla::config::Config::LineVector
private

Vector for Pair.

typedef std::list<Block> walberla::config::Config::List
private

List for parameter blocks.

typedef std::pair<sstreamPos,unsigned int> walberla::config::Config::Pair
private

Pair consisting of a stream position and a line number.

typedef List::size_type walberla::config::Config::size_type

Size type for a Block count.

typedef std::stringstream::pos_type walberla::config::Config::sstreamPos
private

Stream position.

typedef std::string walberla::config::Config::Value
private

Parameter value.

Parameter value replacement map.

Member Enumeration Documentation

Enumerator
noerror 
undefined 
badcast 

Constructor & Destructor Documentation

walberla::config::Config::Config ( )

Default constructor for the Config class.

walberla::config::Config::~Config ( )
default

Destructor for the Config class.

Member Function Documentation

void walberla::config::Config::addValueReplacement ( const Value original,
const Value replacement 
)
inline
Config::iterator walberla::config::Config::begin ( )
inline

Returns an iterator to the first globally defined parameter.

Returns
Iterator to the first globally defined parameter.
Config::const_iterator walberla::config::Config::begin ( ) const
inline

Returns an iterator to the first globally defined parameter.

Returns
Iterator to the first globally defined parameter.
void walberla::config::Config::clear ( )
inline

Clearing the internal status of the file config object.

Returns
void

This function resets the internal status of the config object and clears all error messages.

void walberla::config::Config::convertToLowerCase ( std::string &  s)
inlinestatic

Conversion of the character in s to lower case characters.

Parameters
sReference to the string that gets converted.
Returns
void
Config::iterator walberla::config::Config::end ( )
inline

Returns an iterator one past the last globally defined parameter.

Returns
Iterator one past the last globally defined parameter.
Config::const_iterator walberla::config::Config::end ( ) const
inline

Returns an iterator one past the last globally defined parameter.

Returns
Iterator one past the last globally defined parameter.
std::string walberla::config::Config::error ( ) const
inline

Returns the error messages.

Returns
Error messages.
void walberla::config::Config::extractBlock ( const char *  filename,
std::stringstream &  input,
Block block,
const LineVector lineNumbers,
unsigned int  lineNumber,
unsigned int  level 
)
private

Extracting a parameter block from the preprocessed input stream.

Parameters
filenameName of the parameter file
inputThe preprocessed input stream.
blockThe block whose parameters are extracted.
lineNumbersThe line numbers belonging to the preprocessed input stream.
lineNumberStarting line of the block in the parameter file.
levelThe current inclusion level.
BlockHandle walberla::config::Config::getBlock ( const std::string &  key) const
inline
void walberla::config::Config::getBlocks ( const std::string &  key,
Blocks blocks,
size_t  min = 0,
size_t  max = std::numeric_limits< size_t >::max() 
) const
inline

Adds to the given blocks all extracted blocks with key key.

Parameters
keyThe key of the extracted blocks.
[out]blocksReference to the vector of blocks.
minminimum number of blocks the caller expects
maxmaximum number of blocks the caller can handle
Returns
void
void walberla::config::Config::getBlocks ( Blocks blocks) const
inline

Adds to the given blocks all globally defined blocks.

Parameters
[out]blocksReference to the vector of blocks.
Returns
void
std::string walberla::config::Config::getDirectory ( const std::string &  path)
inlinestaticprivate

Returns the directory part of the given path.

Parameters
pathThe path containing both directory and filename.
Returns
The directory part of the given path.
std::string walberla::config::Config::getFilename ( const std::string &  path)
inlinestaticprivate

Returns the filename part of the given path.

Parameters
pathThe path containing both directory and filename.
Returns
The filename part of the given path.
Config::BlockHandle walberla::config::Config::getGlobalBlock ( ) const
inline

gives a handle to the global block.

Returns
Handle to global block
unsigned int walberla::config::Config::getLineNumber ( const LineVector lineNumbers,
sstreamPos  pos 
) const
private

Calculation of the line number of a command.

Parameters
lineNumbers?
posPosition in the preprocessed input stream.
size_type walberla::config::Config::getNumBlocks ( ) const
inline
size_type walberla::config::Config::getNumBlocks ( const std::string &  key) const
inline
BlockHandle walberla::config::Config::getOneBlock ( const std::string &  key) const
inline
template<typename T >
Config::Parameter< Type > walberla::config::Config::getParameter ( const std::string &  key) const
inline

Returns an extracted parameter.

Parameters
keyThe key of the extracted parameter.
Returns
The extracted parameter.
template<typename T >
Config::Parameter< T > walberla::config::Config::getParameter ( const std::string &  key,
const T &  defaultValue 
) const
inline

Returns an extracted parameter.

Parameters
keyThe key of the extracted parameter.
defdefault value
Returns
The extracted parameter if key is defined, otherwise default value
template<typename Type >
Config::Parameter<Type> walberla::config::Config::getParameter ( const std::string &  key) const
inline
Config::Block & walberla::config::Config::getWritableGlobalBlock ( )
inline

returns the global block.

Returns
Global block
bool walberla::config::Config::isDefined ( std::string  key) const
inline

Checks if a parameter was defined in the parameter file.

Parameters
keyThe parameter key to be checked.
Returns
true if the parameter was defined, false if the parameter wasn't defined.
void walberla::config::Config::listParameters ( ) const
inline

Output function for the contained parameters.

Returns
void
walberla::config::Config::operator bool ( ) const
inline

Conversion operator for the Config class.

This conversion operator allows for the check of a Config object. The operator returns the internal status of the config object.

Config & walberla::config::Config::operator= ( const Config c)

Copy assignment operator for the Config class.

Parameters
cThe Config object to be copied.
void walberla::config::Config::parseFromFile ( const char *const  filename,
Block block,
unsigned int  level 
)
private

Parses the given file and extracts the parameters to the given block.

Parameters
filenameName of the parameter file.
blockParameter block for the found parameters.
levelThe current inclusion level.
void walberla::config::Config::parseFromString ( const std::string &  str,
Block block,
unsigned int  level 
)
private

Parses the given file and extracts the parameters to the given block.

Parameters
strConfig-generated input string.
blockParameter block for the found parameters.
levelThe current inclusion level.

By internal convention, each block first contains all parameter, and then all sub-blocks

void walberla::config::Config::readParameterFile ( const char *const  filename)

Extraction of the parameter file filename.

Parameters
filenameName of the parameter file.
Returns
void
void walberla::config::Config::removeTrailingWhiteSpaces ( std::string &  s)
inlineprivate

Removes the trailing white spaces from the given string.

Parameters
sThe string to be cleaned from trailing white spaces.
Returns
void

Member Data Documentation

Block walberla::config::Config::block_
private

The global parameter block.

std::ostringstream walberla::config::Config::error_
private

Container for all error messages.

const unsigned int walberla::config::Config::maxInclusionLevel = 30
staticprivate
bool walberla::config::Config::stateFlag_
private

Internal status of the config object.

An error is indicated by false.

ValueReplacementMap walberla::config::Config::valueReplacements_
private

value replacements

"$(mapElements.first)" in input file is replaced by "mapElement.second"


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