Misc small changes:
[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 Properties object to an output stream
43 std::ostream& operator<< (std::ostream & os, const Property & p);
44
45
46 /// The Properties class represents a set of properties
47 class Properties : public std::map<PropertyKey, PropertyValue> {
48 public:
49 /// Gets a property
50 const PropertyValue & Get(const PropertyKey &key) const {
51 Properties::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 Properties & 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 /// Shorthand for (temporarily) adding properties, e.g. Properties p()("method","BP")("verbose",1)("tol",1e-9)
92 Properties operator()(const PropertyKey &key, const PropertyValue &val) const { Properties copy = *this; return copy.Set(key,val); }
93
94 /// Check if a property with given key exists
95 bool hasKey(const PropertyKey &key) const { Properties::const_iterator x = find(key); return (x != this->end()); }
96
97 /// Sends a Properties object to an output stream
98 friend std::ostream& operator<< (std::ostream & os, const Properties & ps);
99
100 /// Reads a Properties object from an input stream
101 friend std::istream& operator >> (std::istream& is, Properties & ps);
102 };
103
104
105 } // end of namespace dai
106
107
108 #endif