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 }
 
using Blocks = std::vector< BlockHandle >
 Container for block handles. More...
 
using size_type = List::size_type
 Size type for a Block count. More...
 
using iterator = Map::iterator
 Iterator over the contained parameters. More...
 
using const_iterator = Map::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 &def) const
 Returns an extracted parameter. More...
 
iterator begin ()
 Returns an iterator to the first globally defined parameter. More...
 
const_iterator begin () const
 
iterator end ()
 Returns an iterator one past the last globally defined parameter. More...
 
const_iterator end () const
 
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

using Key = std::string
 Parameter key. More...
 
using Value = std::string
 Parameter value. More...
 
using Map = std::map< Key, Value, CaseInsensitiveCompare >
 Parameter map. More...
 
using List = std::list< Block >
 List for parameter blocks. More...
 
using ValueReplacementMap = std::map< Value, Value >
 Parameter value replacement map. More...
 
using sstreamPos = std::stringstream::pos_type
 Stream position. More...
 
using Pair = std::pair< sstreamPos, unsigned int >
 Pair consisting of a stream position and a line number. More...
 
using LineVector = std::vector< Pair >
 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

◆ Blocks

Container for block handles.

◆ const_iterator

using walberla::config::Config::const_iterator = Map::const_iterator

Constant iterator over the contained parameters.

◆ iterator

using walberla::config::Config::iterator = Map::iterator

Iterator over the contained parameters.

◆ Key

using walberla::config::Config::Key = std::string
private

Parameter key.

◆ LineVector

using walberla::config::Config::LineVector = std::vector<Pair>
private

Vector for Pair.

◆ List

using walberla::config::Config::List = std::list<Block>
private

List for parameter blocks.

◆ Map

◆ Pair

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

Pair consisting of a stream position and a line number.

◆ size_type

using walberla::config::Config::size_type = List::size_type

Size type for a Block count.

◆ sstreamPos

using walberla::config::Config::sstreamPos = std::stringstream::pos_type
private

Stream position.

◆ Value

using walberla::config::Config::Value = std::string
private

Parameter value.

◆ ValueReplacementMap

Parameter value replacement map.

Member Enumeration Documentation

◆ ErrorCode

Enumerator
noerror 
undefined 
badcast 

Constructor & Destructor Documentation

◆ Config()

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

Default constructor for the Config class.

◆ ~Config()

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

Destructor for the Config class.

Member Function Documentation

◆ addValueReplacement()

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

◆ begin() [1/2]

Config::const_iterator walberla::config::Config::begin ( )
inline

Returns an iterator to the first globally defined parameter.

Returns
Iterator to the first globally defined parameter.

◆ begin() [2/2]

const_iterator walberla::config::Config::begin ( ) const
inline

◆ clear()

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.

◆ convertToLowerCase()

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

◆ end() [1/2]

Config::const_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.

◆ end() [2/2]

const_iterator walberla::config::Config::end ( ) const
inline

◆ error()

std::string walberla::config::Config::error ( ) const
inline

Returns the error messages.

Returns
Error messages.

◆ extractBlock()

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.

◆ getBlock()

BlockHandle walberla::config::Config::getBlock ( const std::string &  key) const
inline

◆ getBlocks() [1/2]

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

◆ getBlocks() [2/2]

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

◆ getDirectory()

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.

◆ getFilename()

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.

◆ getGlobalBlock()

Config::BlockHandle walberla::config::Config::getGlobalBlock ( ) const
inline

gives a handle to the global block.

Returns
Handle to global block

◆ getLineNumber()

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.

◆ getNumBlocks() [1/2]

size_type walberla::config::Config::getNumBlocks ( ) const
inline

◆ getNumBlocks() [2/2]

size_type walberla::config::Config::getNumBlocks ( const std::string &  key) const
inline

◆ getOneBlock()

BlockHandle walberla::config::Config::getOneBlock ( const std::string &  key) const
inline

◆ getParameter() [1/3]

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.

◆ getParameter() [2/3]

template<typename Type >
Config::Parameter<Type> walberla::config::Config::getParameter ( const std::string &  key) const
inline

◆ getParameter() [3/3]

template<typename T >
Config::Parameter< T > walberla::config::Config::getParameter ( const std::string &  key,
const T &  def 
) 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

◆ getWritableGlobalBlock()

Config::Block & walberla::config::Config::getWritableGlobalBlock ( )
inline

returns the global block.

Returns
Global block

◆ isDefined()

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.

◆ listParameters()

void walberla::config::Config::listParameters ( ) const
inline

Output function for the contained parameters.

Returns
void

◆ operator bool()

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.

◆ operator=()

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

Copy assignment operator for the Config class.

Parameters
cThe Config object to be copied.

◆ parseFromFile()

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.

◆ parseFromString()

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

◆ readParameterFile()

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

Extraction of the parameter file filename.

Parameters
filenameName of the parameter file.
Returns
void

◆ removeTrailingWhiteSpaces()

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_

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

The global parameter block.

◆ error_

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

Container for all error messages.

◆ maxInclusionLevel

const unsigned int walberla::config::Config::maxInclusionLevel = 30
staticprivate

◆ stateFlag_

bool walberla::config::Config::stateFlag_
private

Internal status of the config object.

An error is indicated by false.

◆ valueReplacements_

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: