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 
79 
81 
83  PropertySet( const std::string& s ) {
84  std::stringstream ss;
85  ss << s;
86  ss >> *this;
87  }
89 
91 
92 
93  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 
155  size_t size() const {
157  }
158 
160  void clear() {
162  }
163 
165  size_t erase( const PropertyKey &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 
237 
242  friend std::ostream& operator<< ( std::ostream& os, const PropertySet& ps );
243 
245 
249  friend std::istream& operator>> ( std::istream& is, PropertySet& ps );
251 };
252 
253 
254 } // end of namespace dai
255 
256 
257 #endif