libDAI
properties.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
11 
12 
13 #ifndef __defined_libdai_properties_h
14 #define __defined_libdai_properties_h
15 
16 
17 #include <iostream>
18 #include <sstream>
19 #include <boost/any.hpp>
20 #include <map>
21 #include <vector>
22 #include <typeinfo>
23 #include <dai/exceptions.h>
24 #include <dai/util.h>
25 #include <boost/lexical_cast.hpp>
26 
27 
28 namespace dai {
29 
30 
32 typedef std::string PropertyKey;
33 
35 typedef boost::any PropertyValue;
36 
38 typedef std::pair<PropertyKey, PropertyValue> Property;
39 
40 
42 
46 std::ostream& operator<< ( std::ostream & os, const Property &p );
47 
48 
50 
73 class PropertySet : private std::map<PropertyKey, PropertyValue> {
74  public:
76 
77  PropertySet() {}
79 
81 
83  PropertySet( const std::string& s ) {
84  std::stringstream ss;
85  ss << s;
86  ss >> *this;
87  }
89 
91 
92  PropertySet& set( const PropertyKey& key, const PropertyValue& val ) {
94  this->operator[](key) = val;
95  return *this;
96  }
97 
99  PropertySet& set( const PropertySet& newProps ) {
100  const std::map<PropertyKey, PropertyValue> *m = &newProps;
101  bforeach(value_type i, *m)
102  set( i.first, i.second );
103  return *this;
104  }
105 
107 
112  PropertySet operator()( const PropertyKey& key, const PropertyValue& val ) const {
113  PropertySet copy = *this;
114  return copy.set(key,val);
115  }
116 
118 
122  template<typename ValueType>
123  PropertySet& setAsString( const PropertyKey& key, const ValueType& val ) {
124  try {
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.");
128  }
129  }
130 
132 
136  template<typename ValueType>
137  void convertTo( const PropertyKey& key ) {
138  PropertyValue val = get(key);
139  if( val.type() != typeid(ValueType) ) {
140  DAI_ASSERT( val.type() == typeid(std::string) );
141  try {
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.");
145  }
146  }
147  }
149 
151 
153 
154  size_t size() const {
156  return std::map<PropertyKey, PropertyValue>::size();
157  }
158 
160  void clear() {
161  std::map<PropertyKey, PropertyValue>::clear();
162  }
163 
165  size_t erase( const PropertyKey &key ) {
166  return std::map<PropertyKey, PropertyValue>::erase( key );
167  }
168 
170  bool hasKey( const PropertyKey& key ) const {
171  PropertySet::const_iterator x = find(key);
172  return (x != this->end());
173  }
174 
176  std::set<PropertyKey> keys() const {
177  std::set<PropertyKey> res;
178  const_iterator i;
179  for( i = begin(); i != end(); i++ )
180  res.insert( i->first );
181  return res;
182  }
183 
185 
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 + "'");
191  return x->second;
192  }
193 
195 
199  template<typename ValueType>
200  ValueType getAs( const PropertyKey& key ) const {
201  try {
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.");
205  return ValueType();
206  }
207  }
208 
210 
217  template<typename ValueType>
218  ValueType getStringAs( const PropertyKey& key ) const {
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) ) {
223  try {
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.");
227  }
228  } else
229  DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property '" + key + "' from string to desired type.");
230  return ValueType();
231  }
233 
235 
236 
242  friend std::ostream& operator<< ( std::ostream& os, const PropertySet& ps );
243 
245  std::string toString() const {
246  std::stringstream ss;
247  ss << *this;
248  return ss.str();
249  }
250 
252 
256  friend std::istream& operator>> ( std::istream& is, PropertySet& ps );
257 
259  void fromString( const std::string& s ) {
260  std::stringstream ss( s );
261  ss >> *this;
262  }
264 };
265 
266 
267 } // end of namespace dai
268 
269 
270 #endif
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