libDAI
|
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) More...
#include <dai/properties.h>
Public Member Functions | |
Constructors and destructors | |
PropertySet () | |
Default constructor. More... | |
PropertySet (const std::string &s) | |
Construct from a string. More... | |
Setting property keys/values | |
PropertySet & | set (const PropertyKey &key, const PropertyValue &val) |
Sets a property (a key key with a corresponding value val) More... | |
PropertySet & | set (const PropertySet &newProps) |
Set properties according to newProps, overriding properties that already exist with new values. More... | |
PropertySet | operator() (const PropertyKey &key, const PropertyValue &val) const |
Shorthand for (temporarily) adding properties. More... | |
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) More... | |
template<typename ValueType > | |
void | convertTo (const PropertyKey &key) |
Converts the type of the property value corresponding with key from string to ValueType (if necessary) More... | |
Queries | |
size_t | size () const |
Return number of key-value pairs. More... | |
void | clear () |
Removes all key-value pairs. More... | |
size_t | erase (const PropertyKey &key) |
Removes key-value pair with given key. More... | |
bool | hasKey (const PropertyKey &key) const |
Check if a property with the given key is defined. More... | |
std::set< PropertyKey > | keys () const |
Returns a set containing all keys. More... | |
const PropertyValue & | get (const PropertyKey &key) const |
Gets the value corresponding to key. More... | |
template<typename ValueType > | |
ValueType | getAs (const PropertyKey &key) const |
Gets the value corresponding to key, cast to ValueType. More... | |
template<typename ValueType > | |
ValueType | getStringAs (const PropertyKey &key) const |
Gets the value corresponding to key, cast to ValueType, converting from a string if necessary. More... | |
Input/output | |
std::string | toString () const |
Formats a PropertySet as a string. More... | |
void | fromString (const std::string &s) |
Reads a PropertySet from a string. More... | |
std::ostream & | operator<< (std::ostream &os, const PropertySet &ps) |
Writes a PropertySet object to an output stream. More... | |
std::istream & | operator>> (std::istream &is, PropertySet &ps) |
Reads a PropertySet object from an input stream. More... | |
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,
Also, a PropertySet provides functionality for converting the representation of individual values from some arbitrary type to and from std::string.
|
inline |
Default constructor.
|
inline |
Construct from a string.
s | string in the format "[key1=val1,key2=val2,...,keyn=valn]" |
|
inline |
Sets a property (a key key with a corresponding value val)
|
inline |
Set properties according to newProps, overriding properties that already exist with new values.
|
inline |
Shorthand for (temporarily) adding properties.
|
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 |
|
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 |
|
inline |
Return number of key-value pairs.
|
inline |
Removes all key-value pairs.
|
inline |
Removes key-value pair with given key.
|
inline |
Check if a property with the given key is defined.
|
inline |
Returns a set containing all keys.
|
inline |
Gets the value corresponding to key.
OBJECT_NOT_FOUND | if the key cannot be found in *this |
|
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 |
|
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 |
|
inline |
Formats a PropertySet as a string.
|
inline |
Reads a PropertySet from a string.
|
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. |
|
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 |