- [Frederik Eaton]: Misc smaller changes
[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
41
42 namespace dai {
43
44
45 /// Type of the key of a Property
46 typedef std::string PropertyKey;
47
48 /// Type of the value of a Property
49 typedef boost::any PropertyValue;
50
51 /// A Property is a pair of a key and a corresponding value
52 typedef std::pair<PropertyKey, PropertyValue> Property;
53
54
55 /// Writes a Property object to an output stream
56 std::ostream& operator<< (std::ostream & os, const Property & p);
57
58
59 /// Represents a set of properties, mapping keys (of type PropertyKey) to values (of type PropertyValue)
60 class PropertySet : private std::map<PropertyKey, PropertyValue> {
61 public:
62 /// Default constructor
63 PropertySet() {}
64
65 /// Construct PropertySet from a string
66 PropertySet( const std::string &s ) {
67 std::stringstream ss;
68 ss << s;
69 ss >> *this;
70 }
71
72 /// Gets a property
73 const PropertyValue & Get(const PropertyKey &key) const {
74 PropertySet::const_iterator x = find(key);
75 #ifdef DAI_DEBUG
76 if( x == this->end() )
77 std::cerr << "PropertySet::Get cannot find property " << key << std::endl;
78 #endif
79 assert( x != this->end() );
80 return x->second;
81 }
82
83 /// Sets a property
84 PropertySet & Set(const PropertyKey &key, const PropertyValue &val) { this->operator[](key) = val; return *this; }
85
86 /// Set properties according to those in newProps, overriding properties that already exist with new values
87 PropertySet & Set( const PropertySet &newProps ) {
88 const std::map<PropertyKey, PropertyValue> *m = &newProps;
89 foreach(value_type i, *m) {
90 Set(i.first, i.second);
91 }
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 std::cerr << "Cannot cast property " << key << " to ";
102 std::cerr << typeid(ValueType).name() << std::endl;
103 return boost::any_cast<ValueType>(Get(key));
104 }
105 }
106
107 /// Converts a property from string to ValueType (if necessary)
108 template<typename ValueType>
109 void ConvertTo(const PropertyKey &key) {
110 PropertyValue val = Get(key);
111 if( val.type() != typeid(ValueType) ) {
112 assert( val.type() == typeid(std::string) );
113
114 std::stringstream ss;
115 ss << GetAs<std::string>(key);
116 ValueType result;
117 ss >> result;
118
119 Set(key, result);
120 }
121 }
122
123 /// Converts a property from string to ValueType (if necessary)
124 template<typename ValueType>
125 ValueType getStringAs(const PropertyKey &key) const {
126 PropertyValue val = Get(key);
127 if( val.type() == typeid(ValueType) ) {
128 return boost::any_cast<ValueType>(val);
129 } else if( val.type() == typeid(std::string) ) {
130 std::stringstream ss;
131 ss << GetAs<std::string>(key);
132 ValueType result;
133 ss >> result;
134 return result;
135 } else {
136 DAI_THROW(IMPOSSIBLE_TYPECAST);
137 return ValueType();
138 }
139 }
140
141 /// Converts a property from ValueType to string (if necessary)
142 template<typename ValueType>
143 PropertySet & setAsString(const PropertyKey &key, ValueType &val) {
144 if( val.type() == typeid(std::string) ) {
145 return Set(key, val);
146 } else {
147 std::stringstream ss (std::stringstream::out);
148 ss << val;
149 return Set(key, ss.str());
150 }
151 }
152
153 /// Shorthand for (temporarily) adding properties, e.g. PropertySet p()("method","BP")("verbose",1)("tol",1e-9)
154 PropertySet operator()(const PropertyKey &key, const PropertyValue &val) const { PropertySet copy = *this; return copy.Set(key,val); }
155
156 /// Check if a property with the given key exists
157 bool hasKey(const PropertyKey &key) const { PropertySet::const_iterator x = find(key); return (x != this->end()); }
158
159 /// Returns a set containing all keys
160 std::set<PropertyKey> allKeys() const {
161 std::set<PropertyKey> res;
162 const_iterator i;
163 for( i = begin(); i != end(); i++ )
164 res.insert( i->first );
165 return res;
166 }
167
168 // Friends
169 friend std::ostream& operator<< (std::ostream & os, const PropertySet & ps);
170 friend std::istream& operator>> (std::istream& is, PropertySet & ps);
171 };
172
173
174 } // end of namespace dai
175
176
177 #endif