#include <dai/properties.h>
Public Member Functions | |
Constructors and destructors | |
PropertySet () | |
Default constructor. | |
PropertySet (const std::string &s) | |
Construct from a string. | |
Setting property keys/values | |
PropertySet & | set (const PropertyKey &key, const PropertyValue &val) |
Sets a property (a key key with a corresponding value val). | |
PropertySet & | set (const PropertySet &newProps) |
Set properties according to newProps, overriding properties that already exist with new values. | |
PropertySet | operator() (const PropertyKey &key, const PropertyValue &val) const |
Shorthand for (temporarily) adding properties. | |
template<typename ValueType> | |
PropertySet & | setAsString (const PropertyKey &key, const ValueType &val) |
Sets a property (a key key with a corresponding value val, which is first converted from ValueType to string). | |
template<typename ValueType> | |
void | convertTo (const PropertyKey &key) |
Converts the type of the property value corresponding with key from string to ValueType (if necessary). | |
Queries | |
size_t | size () const |
Return number of key-value pairs. | |
void | clear () |
Removes all key-value pairs. | |
size_t | erase (const PropertyKey &key) |
Removes key-value pair with given key. | |
bool | hasKey (const PropertyKey &key) const |
Check if a property with the given key is defined. | |
std::set< PropertyKey > | keys () const |
Returns a set containing all keys. | |
const PropertyValue & | get (const PropertyKey &key) const |
Gets the value corresponding to key. | |
template<typename ValueType> | |
ValueType | getAs (const PropertyKey &key) const |
Gets the value corresponding to key, cast to ValueType. | |
template<typename ValueType> | |
ValueType | getStringAs (const PropertyKey &key) const |
Gets the value corresponding to key, cast to ValueType, converting from a string if necessary. | |
Friends | |
Input/output | |
std::ostream & | operator<< (std::ostream &os, const PropertySet &ps) |
Writes a PropertySet object to an output stream. | |
std::istream & | operator>> (std::istream &is, PropertySet &ps) |
Reads a PropertySet object from an input stream. |
Properties are used for specifying parameters of algorithms in a convenient way, where the values of the parameters can be of different types (e.g., strings, doubles, integers, enums). A PropertySet is an attempt to mimic the functionality of a Python dictionary object in C++, using the boost::any class.
A PropertySet can be converted to and from a string, using the following format:
[key1=val1,key2=val2,...,keyn=valn]
That is,
Also, a PropertySet provides functionality for converting the representation of individual values from some arbitrary type to and from std::string.
example.cpp, example_sprinkler_em.cpp, and example_sprinkler_gibbs.cpp.
dai::PropertySet::PropertySet | ( | ) | [inline] |
Default constructor.
dai::PropertySet::PropertySet | ( | const std::string & | s | ) | [inline] |
Construct from a string.
s | string in the format "[key1=val1,key2=val2,...,keyn=valn]" |
PropertySet& dai::PropertySet::set | ( | const PropertyKey & | key, | |
const PropertyValue & | val | |||
) | [inline] |
Sets a property (a key key with a corresponding value val).
PropertySet& dai::PropertySet::set | ( | const PropertySet & | newProps | ) | [inline] |
Set properties according to newProps, overriding properties that already exist with new values.
PropertySet dai::PropertySet::operator() | ( | const PropertyKey & | key, | |
const PropertyValue & | val | |||
) | const [inline] |
Shorthand for (temporarily) adding properties.
PropertySet p()("method","BP")("verbose",1)("tol",1e-9)
PropertySet& dai::PropertySet::setAsString | ( | const PropertyKey & | key, | |
const ValueType & | val | |||
) | [inline] |
Sets a property (a key key with a corresponding value val, which is first converted from ValueType to string).
The implementation makes use of boost::lexical_cast.
ValueType | Type from which the value should be cast |
IMPOSSIBLE_TYPECAST | if the type cast cannot be done |
void dai::PropertySet::convertTo | ( | const PropertyKey & | key | ) | [inline] |
Converts the type of the property value corresponding with key from string to ValueType (if necessary).
The implementation makes use of boost::lexical_cast
ValueType | Type to which the value should be cast |
IMPOSSIBLE_TYPECAST | if the type cast cannot be done |
size_t dai::PropertySet::size | ( | ) | const [inline] |
Return number of key-value pairs.
void dai::PropertySet::clear | ( | ) | [inline] |
Removes all key-value pairs.
size_t dai::PropertySet::erase | ( | const PropertyKey & | key | ) | [inline] |
Removes key-value pair with given key.
bool dai::PropertySet::hasKey | ( | const PropertyKey & | key | ) | const [inline] |
Check if a property with the given key is defined.
std::set<PropertyKey> dai::PropertySet::keys | ( | ) | const [inline] |
Returns a set containing all keys.
const PropertyValue& dai::PropertySet::get | ( | const PropertyKey & | key | ) | const [inline] |
Gets the value corresponding to key.
OBJECT_NOT_FOUND | if the key cannot be found in *this |
ValueType dai::PropertySet::getAs | ( | const PropertyKey & | key | ) | const [inline] |
Gets the value corresponding to key, cast to ValueType.
ValueType | Type to which the value should be cast |
OBJECT_NOT_FOUND | if the key cannot be found in *this | |
IMPOSSIBLE_TYPECAST | if the type cast cannot be done |
ValueType dai::PropertySet::getStringAs | ( | const PropertyKey & | key | ) | const [inline] |
Gets the value corresponding to key, cast to ValueType, converting from a string if necessary.
If the type of the value is already equal to ValueType, no conversion is done. Otherwise, the type of the value should be a std::string, in which case boost::lexical_cast is used to convert this to ValueType.
ValueType | Type to which the value should be cast/converted |
OBJECT_NOT_FOUND | if the key cannot be found in *this | |
IMPOSSIBLE_TYPECAST | if the type cast cannot be done |
std::ostream& operator<< | ( | std::ostream & | os, | |
const PropertySet & | ps | |||
) | [friend] |
Writes a PropertySet object to an output stream.
It uses the format "[key1=val1,key2=val2,...,keyn=valn]"
.
UNKNOWN_PROPERTY_TYPE | if the type of a property value is not supported. |
std::istream& operator>> | ( | std::istream & | is, | |
PropertySet & | ps | |||
) | [friend] |
Reads a PropertySet object from an input stream.
It expects a string in the format "[key1=val1,key2=val2,...,keyn=valn]"
. Values are stored as strings.
MALFORMED_PROPERTY | if the string is not in the expected format |