Cleaned up error handling by introducing the DAI_THROWE macro.
[libdai.git] / include / dai / properties.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 /// \file
24 /// \brief Defines the Property and PropertySet classes
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_properties_h
29 #define __defined_libdai_properties_h
30
31
32 #include <iostream>
33 #include <sstream>
34 #include <boost/any.hpp>
35 #include <map>
36 #include <cassert>
37 #include <typeinfo>
38 #include <dai/exceptions.h>
39 #include <dai/util.h>
40 #include <boost/lexical_cast.hpp>
41
42
43 namespace dai {
44
45
46 /// Type of the key of a Property
47 typedef std::string PropertyKey;
48
49 /// Type of the value of a Property
50 typedef boost::any PropertyValue;
51
52 /// A Property is a pair of a key and a corresponding value
53 typedef std::pair<PropertyKey, PropertyValue> Property;
54
55
56 /// Writes a Property object to an output stream
57 std::ostream& operator<< (std::ostream & os, const Property & p);
58
59
60 /// Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue)
61 class PropertySet : private std::map<PropertyKey, PropertyValue> {
62 public:
63 /// Default constructor
64 PropertySet() {}
65
66 /// Construct PropertySet from a string
67 PropertySet( const std::string &s ) {
68 std::stringstream ss;
69 ss << s;
70 ss >> *this;
71 }
72
73 /// Gets a property
74 const PropertyValue & Get(const PropertyKey &key) const {
75 PropertySet::const_iterator x = find(key);
76 #ifdef DAI_DEBUG
77 if( x == this->end() )
78 std::cerr << "PropertySet::Get cannot find property " << key << std::endl;
79 #endif
80 assert( x != this->end() );
81 return x->second;
82 }
83
84 /// Sets a property
85 PropertySet & Set(const PropertyKey &key, const PropertyValue &val) { this->operator[](key) = val; return *this; }
86
87 /// Set properties according to those in newProps, overriding properties that already exist with new values
88 PropertySet & Set( const PropertySet &newProps ) {
89 const std::map<PropertyKey, PropertyValue> *m = &newProps;
90 foreach(value_type i, *m)
91 Set( i.first, i.second );
92 return *this;
93 }
94
95 /// Gets a property, casted as ValueType
96 template<typename ValueType>
97 ValueType GetAs(const PropertyKey &key) const {
98 try {
99 return boost::any_cast<ValueType>(Get(key));
100 } catch( const boost::bad_any_cast & ) {
101 DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property " + key + " to desired type.");
102 return ValueType();
103 }
104 }
105
106 /// Converts a property from string to ValueType (if necessary)
107 template<typename ValueType>
108 void ConvertTo(const PropertyKey &key) {
109 PropertyValue val = Get(key);
110 if( val.type() != typeid(ValueType) ) {
111 assert( val.type() == typeid(std::string) );
112 try {
113 Set(key, boost::lexical_cast<ValueType>(GetAs<std::string>(key)));
114 } catch(boost::bad_lexical_cast &) {
115 DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property " + key + " from string to desired type.");
116 }
117 }
118 }
119
120 /// Converts a property from string to ValueType (if necessary)
121 template<typename ValueType>
122 ValueType getStringAs(const PropertyKey &key) const {
123 PropertyValue val = Get(key);
124 if( val.type() == typeid(ValueType) ) {
125 return boost::any_cast<ValueType>(val);
126 } else if( val.type() == typeid(std::string) ) {
127 try {
128 return boost::lexical_cast<ValueType>(GetAs<std::string>(key));
129 } catch(boost::bad_lexical_cast &) {
130 DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property " + key + " from string to desired type.");
131 }
132 } else
133 DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property " + key + " from string to desired type.");
134 return ValueType();
135 }
136
137 /// Converts a property from ValueType to string (if necessary)
138 template<typename ValueType>
139 PropertySet & setAsString(const PropertyKey &key, ValueType &val) {
140 try {
141 return Set( key, boost::lexical_cast<std::string>(val) );
142 } catch( boost::bad_lexical_cast & ) {
143 DAI_THROWE(IMPOSSIBLE_TYPECAST,"Cannot cast value of property " + key + " to string.");
144 }
145 }
146
147 /// Shorthand for (temporarily) adding properties, e.g. PropertySet p()("method","BP")("verbose",1)("tol",1e-9)
148 PropertySet operator()(const PropertyKey &key, const PropertyValue &val) const { PropertySet copy = *this; return copy.Set(key,val); }
149
150 /// Check if a property with the given key exists
151 bool hasKey(const PropertyKey &key) const { PropertySet::const_iterator x = find(key); return (x != this->end()); }
152
153 /// Returns a set containing all keys
154 std::set<PropertyKey> allKeys() const {
155 std::set<PropertyKey> res;
156 const_iterator i;
157 for( i = begin(); i != end(); i++ )
158 res.insert( i->first );
159 return res;
160 }
161
162 // Friends
163 friend std::ostream& operator<< (std::ostream & os, const PropertySet & ps);
164 friend std::istream& operator>> (std::istream& is, PropertySet & ps);
165 };
166
167
168 } // end of namespace dai
169
170
171 #endif