Dv::Props Class Reference

Storing elements in a Props object:. More...

#include <props.h>

Inheritance diagram for Dv::Props:
Inheritance graph
[legend]
Collaboration diagram for Dv::Props:
Collaboration graph
[legend]

List of all members.

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)
Propsmerge (const Props &source, const std::string &scope="") throw (std::logic_error)
 Load all key-value pairs from another props object under a certain scope.
Propsselect (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.

Detailed Description

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.


Constructor & Destructor Documentation

Dv::Props::Props (  )  [inline]

Definition at line 35 of file props.h.

Dv::Props::Props ( std::istream &  is  )  [inline]

Definition at line 36 of file props.h.

References read().


Member Function Documentation

std::istream& Dv::Props::read ( std::istream &  is,
int  n 
)

Referenced by Props().

template<typename K >
Reference Dv::Props::operator[] ( const K &  key  )  [inline]

Definition at line 150 of file props.h.

References Dv::tostring().

template<typename K >
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_.

bool Dv::Props::operator!= ( const Props props  )  [inline]

Definition at line 160 of file props.h.

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:

 Props  dbconfig;
 Props  config;
 config.merge(dbconfig,"database");

If dbconfig["name"]=="admindb" then config["database::name"]=="admindb" after the merge.

Parameters:
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.
Returns:
reference to *this.
Warning:
&source must be different from this.
See also:
Dv::Props::select
Props& Dv::Props::select ( const std::string &  scope,
Props target 
) const

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.

 Props  config;
 Props  dbconfig;
 config.select("database",dbconfig);

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.

Parameters:
target the Props object the selected pairs will be added to.
scope the selected pairs have ``scope::'' as a key.
Returns:
a reference to target.
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.

Parameters:
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]).
Exceptions:
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:

  • Variables start with ``${'' or ``%{'' except if the ``$'' or ``'' is preceeded by a backslash (``@'').
  • A backslash is a normal character, except if it is immediately followed by a ``$'' or a ``'', in which case it is not copied.
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.

Parameters:
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]).
Exceptions:
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:

  • Variables start with ``${'' or ``%{'' except if the ``$'' or ``'' is preceeded by a backslash (``@'').
  • A backslash is a normal character, except if it is immediately followed by a ``$'' or a ``'', in which case it is not copied.
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.

Parameters:
sin std::string that is appended to return value.
f function that transforms strings. ${name} in sin is replaced by f((*this)[name]).
Returns:
std::string with subsitutions performed.
Exceptions:
invalid_argument is thrown upon a syntax error.
See also:
Props::substitute

Friends And Related Function Documentation

std::istream& operator>> ( std::istream &  is,
Props props 
) [friend]

Definition at line 38 of file props.h.

std::ostream& operator<< ( std::ostream &  os,
const Props map 
) [friend]

Print a map to a stream.

Each line has the form

 key: value
Parameters:
os stream to print to
map to print
Returns:
os

Reimplemented from Dv::StringMap< std::string >.

Reference operator>> ( const Props props,
const std::string &  key 
) [friend]

Supports "reading" key values from a Props object.

 Dv::Props props;
 std::string name
 int i;
 Props >> "name" >> name >> "int" >> i;
See also:
Props::Reference::operator>>

Definition at line 145 of file props.h.


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

dvutil-1.0.10 [ 5 December, 2009]