Two tiny improvements
[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 /// Gets a property
63 const PropertyValue & Get(const PropertyKey &key) const {
64 PropertySet::const_iterator x = find(key);
65 #ifdef DAI_DEBUG
66 if( x == this->end() )
67 std::cerr << "PropertySet::Get cannot find property " << key << std::endl;
68 #endif
69 assert( x != this->end() );
70 return x->second;
71 }
72
73 /// Sets a property
74 PropertySet & Set(const PropertyKey &key, const PropertyValue &val) { this->operator[](key) = val; return *this; }
75
76 /// Set properties according to those in newProps, overriding properties that already exist with new values
77 PropertySet & Set( const PropertySet &newProps ) {
78 const std::map<PropertyKey, PropertyValue> *m = &newProps;
79 foreach(value_type i, *m) {
80 Set(i.first, i.second);
81 }
82 return *this;
83 }
84
85 /// Gets a property, casted as ValueType
86 template<typename ValueType>
87 ValueType GetAs(const PropertyKey &key) const {
88 try {
89 return boost::any_cast<ValueType>(Get(key));
90 } catch( const boost::bad_any_cast & ) {
91 std::cerr << "Cannot cast property " << key << " to ";
92 std::cerr << typeid(ValueType).name() << std::endl;
93 return boost::any_cast<ValueType>(Get(key));
94 }
95 }
96
97 /// Converts a property from string to ValueType (if necessary)
98 template<typename ValueType>
99 void ConvertTo(const PropertyKey &key) {
100 PropertyValue val = Get(key);
101 if( val.type() != typeid(ValueType) ) {
102 assert( val.type() == typeid(std::string) );
103
104 std::stringstream ss;
105 ss << GetAs<std::string>(key);
106 ValueType result;
107 ss >> result;
108
109 Set(key, result);
110 }
111 }
112
113 /// Converts a property from string to ValueType (if necessary)
114 template<typename ValueType>
115 ValueType getStringAs(const PropertyKey &key) const {
116 PropertyValue val = Get(key);
117 if( val.type() == typeid(std::string) ) {
118 std::stringstream ss;
119 ss << GetAs<std::string>(key);
120 ValueType result;
121 ss >> result;
122 return result;
123 } else if( val.type() == typeid(ValueType) ) {
124 return boost::any_cast<ValueType>(val);
125 } else {
126 DAI_THROW(IMPOSSIBLE_TYPECAST);
127 return ValueType();
128 }
129 }
130
131 /// Converts a property from ValueType to string (if necessary)
132 template<typename ValueType>
133 PropertySet & setAsString(const PropertyKey &key, ValueType &val) {
134 if( val.type() == typeid(std::string) ) {
135 return Set(key, val);
136 } else {
137 std::stringstream ss (std::stringstream::out);
138 ss << val;
139 return Set(key, ss.str());
140 }
141 }
142
143 /// Shorthand for (temporarily) adding properties, e.g. PropertySet p()("method","BP")("verbose",1)("tol",1e-9)
144 PropertySet operator()(const PropertyKey &key, const PropertyValue &val) const { PropertySet copy = *this; return copy.Set(key,val); }
145
146 /// Check if a property with the given key exists
147 bool hasKey(const PropertyKey &key) const { PropertySet::const_iterator x = find(key); return (x != this->end()); }
148
149 // Friends
150 friend std::ostream& operator<< (std::ostream & os, const PropertySet & ps);
151 friend std::istream& operator>> (std::istream& is, PropertySet & ps);
152 };
153
154
155 } // end of namespace dai
156
157
158 #endif