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;
73 class PropertySet :
private std::map<PropertyKey, PropertyValue> {
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>
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.");
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 );
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>
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>
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.");