1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
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.
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.
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
22 #ifndef __defined_libdai_properties_h
23 #define __defined_libdai_properties_h
28 #include <boost/any.hpp>
37 typedef std::string PropertyKey
;
38 typedef boost::any PropertyValue
;
39 typedef std::pair
<PropertyKey
, PropertyValue
> Property
;
42 /// Sends a Property object to an output stream
43 std::ostream
& operator<< (std::ostream
& os
, const Property
& p
);
46 /// The PropertySet class represents a set of properties
47 class PropertySet
: public std::map
<PropertyKey
, PropertyValue
> {
50 const PropertyValue
& Get(const PropertyKey
&key
) const {
51 PropertySet::const_iterator x
= find(key
);
53 if( x
== this->end() )
54 std::cerr
<< "Get cannot find property " << key
<< std::endl
;
56 assert( x
!= this->end() );
61 PropertySet
& Set(const PropertyKey
&key
, const PropertyValue
&val
) { this->operator[](key
) = val
; return *this; }
63 /// Gets a property, casted as ValueType
64 template<typename ValueType
>
65 ValueType
GetAs(const PropertyKey
&key
) const {
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
));
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
) );
83 ss
<< GetAs
<std::string
>(key
);
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
) ) {
97 ss
<< GetAs
<std::string
>(key
);
101 } else if( val
.type() == typeid(ValueType
) ) {
102 return boost::any_cast
<ValueType
>(val
);
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
);
113 std::stringstream
ss (std::stringstream::out
);
115 return Set(key
, ss
.str());
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
); }
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()); }
125 /// Sends a PropertySet object to an output stream
126 friend std::ostream
& operator<< (std::ostream
& os
, const PropertySet
& ps
);
128 /// Reads a PropertySet object from an input stream
129 friend std::istream
& operator >> (std::istream
& is
, PropertySet
& ps
);
133 } // end of namespace dai