libDAI
dai::PropertySet Class Reference

Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) More...

#include <dai/properties.h>

Inherits std::map< K, T >.

List of all members.

Public Member Functions

Constructors and destructors
 PropertySet ()
 Default constructor.
 PropertySet (const std::string &s)
 Construct from a string.
Setting property keys/values
PropertySetset (const PropertyKey &key, const PropertyValue &val)
 Sets a property (a key key with a corresponding value val)
PropertySetset (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 >
PropertySetsetAsString (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< PropertyKeykeys () const
 Returns a set containing all keys.
const PropertyValueget (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.

Detailed Description

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.

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.


Constructor & Destructor Documentation

dai::PropertySet::PropertySet ( ) [inline]

Default constructor.

dai::PropertySet::PropertySet ( const std::string &  s) [inline]

Construct from a string.

Parameters:
sstring in the format "[key1=val1,key2=val2,...,keyn=valn]"

Member Function Documentation

PropertySet& dai::PropertySet::set ( const PropertyKey key,
const PropertyValue val 
) [inline]

Sets a property (a key key with a corresponding value val)

Examples:
example.cpp, example_sprinkler_em.cpp, and example_sprinkler_gibbs.cpp.
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.

Example:
PropertySet p()("method","BP")("verbose",1)("tol",1e-9)
template<typename ValueType >
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.

Template Parameters:
ValueTypeType from which the value should be cast
Exceptions:
IMPOSSIBLE_TYPECASTif the type cast cannot be done
template<typename ValueType >
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

Template Parameters:
ValueTypeType to which the value should be cast
Exceptions:
IMPOSSIBLE_TYPECASTif 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.

Exceptions:
OBJECT_NOT_FOUNDif the key cannot be found in *this
template<typename ValueType >
ValueType dai::PropertySet::getAs ( const PropertyKey key) const [inline]

Gets the value corresponding to key, cast to ValueType.

Template Parameters:
ValueTypeType to which the value should be cast
Exceptions:
OBJECT_NOT_FOUNDif the key cannot be found in *this
IMPOSSIBLE_TYPECASTif the type cast cannot be done
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:
ValueTypeType to which the value should be cast/converted
Exceptions:
OBJECT_NOT_FOUNDif the key cannot be found in *this
IMPOSSIBLE_TYPECASTif the type cast cannot be done

Friends And Related Function Documentation

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]".

Note:
Only a subset of all possible types is supported (see the implementation of this function). Adding support for more types has to be done by hand.
Exceptions:
UNKNOWN_PROPERTY_TYPEif 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.

Exceptions:
MALFORMED_PROPERTYif the string is not in the expected format

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