- Solved the proliferation of type checks for different DAI_ENUM's in properties.cpp
[libdai.git] / include / dai / properties.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #ifndef __defined_libdai_properties_h
23 #define __defined_libdai_properties_h
24
25
26 #include <iostream>
27 #include <sstream>
28 #include <boost/any.hpp>
29 #include <map>
30 #include <cassert>
31 #include <typeinfo>
32
33
34 namespace dai {
35
36
37 typedef std::string PropertyKey;
38 typedef boost::any PropertyValue;
39 typedef std::pair<PropertyKey, PropertyValue> Property;
40
41
42 /// Sends a Property object to an output stream
43 std::ostream& operator<< (std::ostream & os, const Property & p);
44
45
46 /// The PropertySet class represents a set of properties
47 class PropertySet : public std::map<PropertyKey, PropertyValue> {
48 public:
49 /// Gets a property
50 const PropertyValue & Get(const PropertyKey &key) const {
51 PropertySet::const_iterator x = find(key);
52 #ifdef DAI_DEBUG
53 if( x == this->end() )
54 std::cerr << "Get cannot find property " << key << std::endl;
55 #endif
56 assert( x != this->end() );
57 return x->second;
58 }
59
60 /// Sets a property
61 PropertySet & Set(const PropertyKey &key, const PropertyValue &val) { this->operator[](key) = val; return *this; }
62
63 /// Gets a property, casted as ValueType
64 template<typename ValueType>
65 ValueType GetAs(const PropertyKey &key) const {
66 try {
67 return boost::any_cast<ValueType>(Get(key));
68 } catch( const boost::bad_any_cast & ) {
69 std::cerr << "Cannot cast property " << key << " to ";
70 std::cerr << typeid(ValueType).name() << std::endl;
71 return boost::any_cast<ValueType>(Get(key));
72 }
73 }
74
75 /// Converts a property from string to ValueType, if necessary
76 template<typename ValueType>
77 void ConvertTo(const PropertyKey &key) {
78 PropertyValue val = Get(key);
79 if( val.type() != typeid(ValueType) ) {
80 assert( val.type() == typeid(std::string) );
81
82 std::stringstream ss;
83 ss << GetAs<std::string>(key);
84 ValueType result;
85 ss >> result;
86
87 Set(key, result);
88 }
89 }
90
91 /// Converts a property from string to ValueType (if necessary)
92 template<typename ValueType>
93 ValueType getStringAs(const PropertyKey &key) const {
94 PropertyValue val = Get(key);
95 if( val.type() == typeid(std::string) ) {
96 std::stringstream ss;
97 ss << GetAs<std::string>(key);
98 ValueType result;
99 ss >> result;
100 return result;
101 } else if( val.type() == typeid(ValueType) ) {
102 return boost::any_cast<ValueType>(val);
103 } else
104 assert( 0 == 1 );
105 }
106
107 /// Converts a property from ValueType to string (if necessary)
108 template<typename ValueType>
109 PropertySet & setAsString(const PropertyKey &key, ValueType &val) {
110 if( val.type() == typeid(std::string) ) {
111 return Set(key, val);
112 } else {
113 std::stringstream ss (std::stringstream::out);
114 ss << val;
115 return Set(key, ss.str());
116 }
117 }
118
119 /// Shorthand for (temporarily) adding properties, e.g. PropertySet p()("method","BP")("verbose",1)("tol",1e-9)
120 PropertySet operator()(const PropertyKey &key, const PropertyValue &val) const { PropertySet copy = *this; return copy.Set(key,val); }
121
122 /// Check if a property with given key exists
123 bool hasKey(const PropertyKey &key) const { PropertySet::const_iterator x = find(key); return (x != this->end()); }
124
125 /// Sends a PropertySet object to an output stream
126 friend std::ostream& operator<< (std::ostream & os, const PropertySet & ps);
127
128 /// Reads a PropertySet object from an input stream
129 friend std::istream& operator >> (std::istream& is, PropertySet & ps);
130 };
131
132
133 } // end of namespace dai
134
135
136 #endif