Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue)
More...
Inherits std::map< K, T >.
Public Member Functions |
|
| PropertySet () |
| Default constructor.
|
| PropertySet (const std::string &s) |
| Construct from a string.
|
|
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)
|
|
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.
|
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue)
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,
- the whole PropertySet is wrapped in square brackets ("[", "]")
- all properties in the PropertySet are seperated by a comma (",")
- each Property consists of:
- the name of the key
- an equality sign ("=")
- its value (represented as a string)
Also, a PropertySet provides functionality for converting the representation of individual values from some arbitrary type to and from std::string.
- Note
- Not all types are automatically supported; if a type is unknown, an UNKNOWN_PROPERTY_TYPE exception is thrown. Adding support for a new type can be done in the body of the operator<<(std::ostream &, const Property &).
- Examples:
- example.cpp, example_sprinkler_em.cpp, and example_sprinkler_gibbs.cpp.
template<typename ValueType >
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.
- Template Parameters
-
ValueType | Type to which the value should be cast/converted |
- Exceptions
-
OBJECT_NOT_FOUND | if the key cannot be found in *this |
IMPOSSIBLE_TYPECAST | if the type cast cannot be done |