Manages a collection of program options. More...
#include <Properties.h>
Public Member Functions | |
Properties () | |
Default constructor; creates an empty set. More... | |
Properties (const String &filename, const PropertiesDesc &desc, bool allow_unregistered=false) | |
Constructor; load properties from a filename. More... | |
void | load (const String &filename, const PropertiesDesc &desc, bool allow_unregistered=false) |
Loads a configuration file with properties. More... | |
void | parse_args (int argc, char *argv[], const PropertiesDesc &desc, const PropertiesDesc *hidden=0, const PositionalDesc *p=0, bool allow_unregistered=false) |
Parses command line arguments. More... | |
void | parse_args (const std::vector< String > &args, const PropertiesDesc &desc, const PropertiesDesc *hidden=0, const PositionalDesc *p=0, bool allow_unregistered=false) |
Parses command line arguments. More... | |
void | notify () |
Calls user-defined notifier functions (if any) with final values. More... | |
template<typename T > | |
T | get (const String &name) const |
Get the value of option of type T. More... | |
template<typename T > | |
T | get (const String &name, const T &default_value) const |
Get the value of option of type T. More... | |
const boost::any & | operator[] (const String &name) const |
Get the underlying boost::any value of 'name'. More... | |
bool | defaulted (const String &name) const |
Check whether a property has a default value. More... | |
bool | has (const String &name) const |
Check whether a property exists. More... | |
InsRet | add (const String &name, const boost::any &v, bool defaulted=false) |
Add property to the map. More... | |
void | set (const String &name, const boost::any &v, bool defaulted=false) |
Set a property in the map, create if not found. More... | |
void | remove (const String &name) |
Remove a property from the map. More... | |
void | alias (const String &primary, const String &secondary, bool overwrite=false) |
Setup an alias for a property. More... | |
void | sync_aliases () |
Sync alias values. More... | |
void | get_names (std::vector< String > &names) |
Returns all property names. More... | |
void | print (std::ostream &out, bool include_default=false) |
Prints keys and values of the configuration map. More... | |
Static Public Member Functions | |
static String | to_str (const boost::any &a) |
Helper to print boost::any used by property values. More... | |
Private Types | |
typedef Po::variable_value | Value |
typedef Po::variables_map | Map |
typedef std::pair < Map::iterator, bool > | InsRet |
typedef std::map< String, String > | AliasMap |
Private Attributes | |
bool | m_need_alias_sync |
Whether the aliases need to be synced. More... | |
Map | m_map |
The map containing all properties. More... | |
AliasMap | m_alias_map |
A map with all aliases. More... | |
Manages a collection of program options.
Implements getters and setters, aliases and default values.
Definition at line 208 of file Properties.h.
|
private |
Definition at line 212 of file Properties.h.
|
private |
Definition at line 211 of file Properties.h.
|
private |
Definition at line 210 of file Properties.h.
|
private |
Definition at line 209 of file Properties.h.
|
inline |
Default constructor; creates an empty set.
Definition at line 216 of file Properties.h.
|
inline |
Constructor; load properties from a filename.
filename | The name of the file with the properties |
desc | A Property description with valid properties, default values |
allow_unregistered | If true, unknown/unregistered properties are accepted |
Definition at line 227 of file Properties.h.
|
inline |
Add property to the map.
name | The name of the property |
v | The value of the property |
defaulted | True if the value is default |
Definition at line 360 of file Properties.h.
Setup an alias for a property.
The primary property has higher priority, meaning when aliases are sync'ed the primary value can override secondary value
primary | The primary property name |
secondary | The secondary property name |
overwrite | True if an existing alias should be overwritten |
Definition at line 203 of file Properties.cc.
|
inline |
Check whether a property has a default value.
name | The name of the property |
Definition at line 337 of file Properties.h.
|
inline |
Get the value of option of type T.
Throws if option is not defined.
name | The name of the property |
Error::CONFIG_GET_ERROR | if the requested property is not defined |
Definition at line 287 of file Properties.h.
|
inline |
Get the value of option of type T.
Returns supplied default value if not found. Try use the first form in usual cases and supply default values in the config descriptions, as it validates the name and is less error prone.
name | The name of the property |
default_value | The default value to return if not found |
Definition at line 307 of file Properties.h.
|
inline |
Returns all property names.
names | reference to vector to hold names of all properties |
Definition at line 415 of file Properties.h.
|
inline |
Check whether a property exists.
name | The name of the property |
Definition at line 346 of file Properties.h.
void Properties::load | ( | const String & | filename, |
const PropertiesDesc & | desc, | ||
bool | allow_unregistered = false |
||
) |
Loads a configuration file with properties.
filename | The name of the configuration file |
desc | A property description |
allow_unregistered | If true, unknown/unregistered properties are accepted |
Definition at line 144 of file Properties.cc.
void Properties::notify | ( | ) |
Calls user-defined notifier functions (if any) with final values.
Definition at line 199 of file Properties.cc.
|
inline |
Get the underlying boost::any value of 'name'.
name | The name of the property |
Definition at line 327 of file Properties.h.
void Properties::parse_args | ( | int | argc, |
char * | argv[], | ||
const PropertiesDesc & | desc, | ||
const PropertiesDesc * | hidden = 0 , |
||
const PositionalDesc * | p = 0 , |
||
bool | allow_unregistered = false |
||
) |
Parses command line arguments.
argc | The argument count (from main) |
argv | The argument array (from main) |
desc | The options description |
hidden | The hidden options description |
p | The positional options description |
allow_unregistered | If true, unknown/unregistered properties are accepted |
Error::CONFIG_INVALID_ARGUMENT | on error |
Definition at line 172 of file Properties.cc.
void Properties::parse_args | ( | const std::vector< String > & | args, |
const PropertiesDesc & | desc, | ||
const PropertiesDesc * | hidden = 0 , |
||
const PositionalDesc * | p = 0 , |
||
bool | allow_unregistered = false |
||
) |
Parses command line arguments.
args | A vector of Strings with the command line arguments |
desc | The options description |
hidden | The hidden options description |
p | The positional options description |
allow_unregistered | If true, unknown/unregistered properties are accepted |
Error::CONFIG_INVALID_ARGUMENT | on error |
Definition at line 188 of file Properties.cc.
void Properties::print | ( | std::ostream & | out, |
bool | include_default = false |
||
) |
Prints keys and values of the configuration map.
out | The output stream |
include_default | If true then default values are included |
Definition at line 274 of file Properties.cc.
|
inline |
Remove a property from the map.
name | The name of the property |
Definition at line 384 of file Properties.h.
|
inline |
Set a property in the map, create if not found.
name | The name of the property |
v | The value of the property |
defaulted | True if the value is default |
Definition at line 372 of file Properties.h.
void Properties::sync_aliases | ( | ) |
Sync alias values.
After this operation all properties that are aliases to each other have the same value. Value priority: primary non-default > secondary non-default > primary default > secondary default
Definition at line 213 of file Properties.cc.
|
static |
Helper to print boost::any used by property values.
a | Reference to the boost::any value |
Definition at line 237 of file Properties.cc.
|
private |
A map with all aliases.
Definition at line 444 of file Properties.h.
|
private |
The map containing all properties.
Definition at line 441 of file Properties.h.
|
private |
Whether the aliases need to be synced.
Definition at line 438 of file Properties.h.