13 #ifndef __defined_libdai_properties_h
14 #define __defined_libdai_properties_h
19 #include <boost/any.hpp>
25 #include <boost/lexical_cast.hpp>
38 typedef std::pair<PropertyKey, PropertyValue>
Property;
46 std::ostream&
operator<< ( std::ostream & os,
const Property &p );
73 class PropertySet :
private std::map<PropertyKey, PropertyValue> {
92 PropertySet&
set(
const PropertyKey& key,
const PropertyValue& val ) {
94 this->operator[](key) = val;
100 const std::map<PropertyKey, PropertyValue> *m = &newProps;
102 set( i.first, i.second );
114 return copy.
set(key,val);
122 template<
typename ValueType>
125 return set( key, boost::lexical_cast<std::string>(val) );
126 }
catch( boost::bad_lexical_cast & ) {
127 DAI_THROWE(IMPOSSIBLE_TYPECAST,
"Cannot cast value of property '" + key +
"' to string.");
136 template<
typename ValueType>
138 PropertyValue val =
get(key);
139 if( val.type() !=
typeid(ValueType) ) {
140 DAI_ASSERT( val.type() ==
typeid(std::string) );
142 set(key, boost::lexical_cast<ValueType>(getAs<std::string>(key)));
143 }
catch(boost::bad_lexical_cast &) {
144 DAI_THROWE(IMPOSSIBLE_TYPECAST,
"Cannot cast value of property '" + key +
"' from string to desired type.");
154 size_t size()
const {
156 return std::map<PropertyKey, PropertyValue>::size();
161 std::map<PropertyKey, PropertyValue>::clear();
165 size_t erase(
const PropertyKey &key ) {
166 return std::map<PropertyKey, PropertyValue>::erase( key );
170 bool hasKey(
const PropertyKey& key )
const {
171 PropertySet::const_iterator x = find(key);
172 return (x != this->end());
176 std::set<PropertyKey>
keys()
const {
177 std::set<PropertyKey> res;
179 for( i = begin(); i != end(); i++ )
180 res.insert( i->first );
187 const PropertyValue&
get(
const PropertyKey& key )
const {
188 PropertySet::const_iterator x = find(key);
189 if( x == this->end() )
190 DAI_THROWE(OBJECT_NOT_FOUND,
"PropertySet::get cannot find property '" + key +
"'");
199 template<
typename ValueType>
200 ValueType
getAs(
const PropertyKey& key )
const {
202 return boost::any_cast<ValueType>(
get(key));
203 }
catch(
const boost::bad_any_cast & ) {
204 DAI_THROWE(IMPOSSIBLE_TYPECAST,
"Cannot cast value of property '" + key +
"' to desired type.");
217 template<
typename ValueType>
219 PropertyValue val =
get(key);
220 if( val.type() ==
typeid(ValueType) ) {
221 return boost::any_cast<ValueType>(val);
222 }
else if( val.type() ==
typeid(std::string) ) {
224 return boost::lexical_cast<ValueType>(getAs<std::string>(key));
225 }
catch(boost::bad_lexical_cast &) {
226 DAI_THROWE(IMPOSSIBLE_TYPECAST,
"Cannot cast value of property '" + key +
"' from string to desired type.");
229 DAI_THROWE(IMPOSSIBLE_TYPECAST,
"Cannot cast value of property '" + key +
"' from string to desired type.");
246 std::stringstream ss;
260 std::stringstream ss( s );
friend std::ostream & operator<<(std::ostream &os, const PropertySet &ps)
Writes a PropertySet object to an output stream.
Definition: properties.cpp:40
ValueType getStringAs(const PropertyKey &key) const
Gets the value corresponding to key, cast to ValueType, converting from a string if necessary...
Definition: properties.h:218
#define DAI_THROWE(cod, msg)
Macro that simplifies throwing an exception with a user-defined error message.
Definition: exceptions.h:57
void convertTo(const PropertyKey &key)
Converts the type of the property value corresponding with key from string to ValueType (if necessary...
Definition: properties.h:137
std::set< PropertyKey > keys() const
Returns a set containing all keys.
Definition: properties.h:176
std::pair< PropertyKey, PropertyValue > Property
A Property is a pair of a key and a corresponding value.
Definition: properties.h:38
size_t size() const
Return number of key-value pairs.
Definition: properties.h:155
bool hasKey(const PropertyKey &key) const
Check if a property with the given key is defined.
Definition: properties.h:170
PropertySet & set(const PropertySet &newProps)
Set properties according to newProps, overriding properties that already exist with new values...
Definition: properties.h:99
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...
Definition: properties.h:123
void clear()
Removes all key-value pairs.
Definition: properties.h:160
std::ostream & operator<<(std::ostream &os, const FactorGraph &fg)
Writes a FactorGraph to an output stream.
Definition: factorgraph.cpp:73
PropertySet & set(const PropertyKey &key, const PropertyValue &val)
Sets a property (a key key with a corresponding value val)
Definition: properties.h:93
#define bforeach
An alias to the BOOST_FOREACH macro from the boost::bforeach library.
Definition: util.h:48
Defines the Exception class and macros for throwing exceptions and doing assertions.
Defines general utility functions and adds an abstraction layer for platform-dependent functionality...
PropertySet()
Default constructor.
Definition: properties.h:78
PropertySet operator()(const PropertyKey &key, const PropertyValue &val) const
Shorthand for (temporarily) adding properties.
Definition: properties.h:112
size_t erase(const PropertyKey &key)
Removes key-value pair with given key.
Definition: properties.h:165
friend std::istream & operator>>(std::istream &is, PropertySet &ps)
Reads a PropertySet object from an input stream.
Definition: properties.cpp:53
Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue) ...
Definition: properties.h:73
boost::any PropertyValue
Type of the value of a Property.
Definition: properties.h:35
void fromString(const std::string &s)
Reads a PropertySet from a string.
Definition: properties.h:259
Namespace for libDAI.
Definition: alldai.cpp:16
ValueType getAs(const PropertyKey &key) const
Gets the value corresponding to key, cast to ValueType.
Definition: properties.h:200
#define DAI_ASSERT(condition)
Assertion mechanism, similar to the standard assert() macro. It is always active, even if NDEBUG is d...
Definition: exceptions.h:60
PropertySet(const std::string &s)
Construct from a string.
Definition: properties.h:83
std::string PropertyKey
Type of the key of a Property.
Definition: properties.h:32
std::string toString() const
Formats a PropertySet as a string.
Definition: properties.h:245