libDAI
List of all members
dai::PropertySet Class Reference

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

#include <dai/properties.h>

Inheritance diagram for dai::PropertySet:

Public Member Functions

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

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, and example_sprinkler_em.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, and example_sprinkler_em.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
std::string dai::PropertySet::toString ( ) const
inline

Formats a PropertySet as a string.

void dai::PropertySet::fromString ( const std::string &  s)
inline

Reads a PropertySet from a string.

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: