Storing elements in a Props object:. More...
#include <props.h>
Classes | |
class | Reference |
Public Member Functions | |
Props () | |
Props (std::istream &is) | |
std::istream & | read (std::istream &is, int n) |
template<typename K > | |
Reference | operator[] (const K &key) |
template<typename K > | |
const Reference | operator() (const K &key) const |
bool | operator== (const Props &props) |
bool | operator!= (const Props &props) |
Props & | merge (const Props &source, const std::string &scope="") throw (std::logic_error) |
Load all key-value pairs from another props object under a certain scope. | |
Props & | select (const std::string &scope, Props &target) const |
Insert selected subkey=value pairs of this object into the target. | |
void | substitute (std::istream &sin, std::ostream &sout, std::string(*f)(const std::string &)=0) const throw (std::invalid_argument) |
Recursively substitute variable occurrences in sin to yield sout. | |
void | substitute (const std::string &sin, std::string &sout, std::string(*f)(const std::string &)=0) const throw (std::invalid_argument) |
Recursively substitute variable occurrences in sin to yield sout. | |
std::string | substitute (const std::string &sin, std::string(*f)(const std::string &)=0) const throw (std::invalid_argument) |
Same as above but returns result std::string. | |
Friends | |
std::istream & | operator>> (std::istream &is, Props &props) |
std::ostream & | operator<< (std::ostream &is, const Props &props) |
Print a map to a stream. | |
Reference | operator>> (const Props &props, const std::string &key) |
Supports "reading" key values from a Props object. |
Storing elements in a Props object:.
props[key] = value;
where key and value can be of any type that can be converted to a string using Dv::tostring.
Retrieving can be done in 2 ways: using operator[]
props[key].get<T>() // convert string to type T int i = props[key]; // conversion to int will be inferred std::cout << props[key]; // string will be printed
and using operator() which works also with 'const' props objects
props(key) = 3 // will not compile props(key).get<T>() // same as above but props can be const int i = props(key) // same as above but props can be const std::cout << props(key); // string will be printed
Definition at line 33 of file props.h.
Dv::Props::Props | ( | std::istream & | is | ) | [inline] |
std::istream& Dv::Props::read | ( | std::istream & | is, | |
int | n | |||
) |
Referenced by Props().
Reference Dv::Props::operator[] | ( | const K & | key | ) | [inline] |
Definition at line 150 of file props.h.
References Dv::tostring().
const Reference Dv::Props::operator() | ( | const K & | key | ) | const [inline] |
Definition at line 155 of file props.h.
References Dv::tostring().
bool Dv::Props::operator== | ( | const Props & | props | ) | [inline] |
Definition at line 159 of file props.h.
References Dv::StringMap< T >::map_.
Props& Dv::Props::merge | ( | const Props & | source, | |
const std::string & | scope = "" | |||
) | throw (std::logic_error) |
Load all key-value pairs from another props object under a certain scope.
All key-value pairs from source will be added to *this, where the key will be prefixed with scope::
if the size of scope is not zero.
Example usage:
If dbconfig["name"]=="admindb"
then config["database::name"]=="admindb"
after the merge.
source | the Props object from which key=value pairs will be copied. | |
scope | only key=value pairs where key starts with scope:: will be copied. |
Insert selected subkey=value pairs of this object into the target.
The selected pairs are those for which the key starts with the scope std::string followed by '::'. The pairs will be inserted with a key consisting of the remainder of the original key, after removing the scope std::string and '::'.
Example.
In the example, if config["database::name"] == "admindb"
then dbconfig["name"] == "admindb"
after the call to select
.
The function returns a reference to its second first argument.
target | the Props object the selected pairs will be added to. | |
scope | the selected pairs have ``scope:: '' as a key. |
void Dv::Props::substitute | ( | std::istream & | sin, | |
std::ostream & | sout, | |||
std::string(*)(const std::string &) | f = 0 | |||
) | const throw (std::invalid_argument) |
Recursively substitute variable occurrences in sin to yield sout.
sin | stream that is appended to sout. | |
sout | stream that is appended to. | |
f | function that transforms strings. ${name} in sin is replaced by f((*this)[name]). |
invalid_argument | is thrown upon a syntax error. |
Sin is a stream, the contents which is appended to sout except for variable occurrences of the form ${key} or %{key} which are replaced by f((*this)[key]) and (*this)[key], respectively (the ``$'' sign can be escaped using ``\'').
Undefined (in *this) variables are replaced by the empty std::string.
Example:
std::string query("select * from"); props.substitute("person where name = '${name}' and age = %{age}", query, MySql::escape);
Syntax:
void Dv::Props::substitute | ( | const std::string & | sin, | |
std::string & | sout, | |||
std::string(*)(const std::string &) | f = 0 | |||
) | const throw (std::invalid_argument) |
Recursively substitute variable occurrences in sin to yield sout.
sin | std::string that is appended to sout. | |
sout | std::string that is appended to. | |
f | function that transforms strings. ${name} in sin is replaced by f((*this)[name]). |
invalid_argument | is thrown upon a syntax error. |
Sin is a std::string which is appended to sout except for variable occurrences of the form ${key} or %{key} which are replaced by f((*this)[key]) and (*this)[key], respectively (the ``$'' sign can be escaped using ``\'').
Undefined (in *this) variables are replaced by the empty std::string.
Syntax:
std::string Dv::Props::substitute | ( | const std::string & | sin, | |
std::string(*)(const std::string &) | f = 0 | |||
) | const throw (std::invalid_argument) |
Same as above but returns result std::string.
sin | std::string that is appended to return value. | |
f | function that transforms strings. ${name} in sin is replaced by f((*this)[name]). |
invalid_argument | is thrown upon a syntax error. |
std::istream& operator>> | ( | std::istream & | is, | |
Props & | props | |||
) | [friend] |
std::ostream& operator<< | ( | std::ostream & | os, | |
const Props & | map | |||
) | [friend] |
Print a map to a stream.
Each line has the form
key: value
os | stream to print to | |
map | to print |
Reimplemented from Dv::StringMap< std::string >.
dvutil-1.0.10 | [ 5 December, 2009] |