Internal helper class that replaces variables in a string with their respective values. More...
#include <InstantIO/VariableReplacer.h>
Classes | |
class | Element |
Abstract superclass of all elements of a format string. | |
class | Text |
A text element of a format string. | |
class | Variable |
A variable element of a format string. | |
Public Member Functions | |
VariableReplacer () | |
Creates a new VariableReplacer object with an empty format string. | |
VariableReplacer (const std::string &format) | |
Initializes the Variable replacer object with a format string. | |
~VariableReplacer () | |
Destroys the VariableReplacer object. | |
void | setFormat (const std::string &format) |
Sets a new format string. | |
void | clear () |
Clears the format string. | |
std::string | toString (const Config::ParamBag ¶ms) const |
Converts the VariableReplacer object to a string. |
Internal helper class that replaces variables in a string with their respective values.
When you create a VariableReplacer object, you have to specify a format string. This format string consists of text and variables. Variables are text strings, surrounded by curly braces.
Then, you can call the toString method of the VariableReplacer object with a set of name/value-pairs. This method returns the format string where all variables are replaced by their respective values, i.e. the method looks up the set for the name of the variable and replaces the variable with its value.
The following piece of code demonstrates how to use the VariableReplacer class:
VariableReplacer vr("Connecting to host {host}, port {port}"); ParamBag params; params.setParam("host", "my_computer"); params.setParam("port", 80); string result = vr.toString();
The variable "result" contains the following string after executing the code above: "Connecting to host my_computer, port 80".
InstantIO::VariableReplacer::VariableReplacer | ( | const std::string & | format | ) | [inline] |
Initializes the Variable replacer object with a format string.
The format string is described in the class documentation.
format | The format string. |
void InstantIO::VariableReplacer::setFormat | ( | const std::string & | format | ) |
Sets a new format string.
format | The format string. |
std::string InstantIO::VariableReplacer::toString | ( | const Config::ParamBag & | params | ) | const |
Converts the VariableReplacer object to a string.
This method returns the format string specified in the constructor. It looks up the names of all variables contained in the format string in a set of name/value-pairs. When it finds a variable name in the set, it replaces it by the respective value, otherwise it simply removes the variable from the format string.
params | The set of name/value-pairs. |