1 /* This file is part of libDAI - http://www.libdai.org/
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
13 #include <dai/properties.h>
14 #include <dai/exceptions.h>
20 std::ostream
& operator<< (std::ostream
& os
, const Property
& p
) {
22 if( p
.second
.type() == typeid(size_t) )
23 os
<< boost::any_cast
<size_t>(p
.second
);
24 else if( p
.second
.type() == typeid(std::string
) )
25 os
<< boost::any_cast
<std::string
>(p
.second
);
26 else if( p
.second
.type() == typeid(double) )
27 os
<< boost::any_cast
<double>(p
.second
);
28 else if( p
.second
.type() == typeid(bool) )
29 os
<< boost::any_cast
<bool>(p
.second
);
30 else if( p
.second
.type() == typeid(PropertySet
) )
31 os
<< boost::any_cast
<PropertySet
>(p
.second
);
33 DAI_THROW(UNKNOWN_PROPERTY_TYPE
);
38 /// Writes a PropertySet object to an output stream
39 std::ostream
& operator<< (std::ostream
& os
, const PropertySet
& ps
) {
41 for( PropertySet::const_iterator p
= ps
.begin(); p
!= ps
.end(); p
++ ) {
51 /// Reads a PropertySet object from an input stream, storing values as strings
52 std::istream
& operator>> (std::istream
& is
, PropertySet
& ps
) {
58 // Check whether s is of the form "[.*]"
59 if( (s
.length() < 2) || (s
.at(0) != '[') || (s
.at(s
.length()-1)) != ']' )
60 DAI_THROWE(MALFORMED_PROPERTY
,"Malformed PropertySet: " + s
);
62 size_t N
= s
.length() - 1;
63 for( size_t token_start
= 1; token_start
< N
; ) {
66 // scan until '=' is found
67 for( token_end
= token_start
+ 1; token_end
< N
; token_end
++ )
68 if( s
[token_end
] == '=' )
71 std::string key
= s
.substr(token_start
, token_end
- token_start
);
73 DAI_THROWE(MALFORMED_PROPERTY
,"Malformed Property: " + key
);
75 token_start
= token_end
+ 1;
76 // scan until matching ',' is found
78 for( token_end
= token_start
; token_end
< N
; token_end
++ ) {
79 if( s
[token_end
] == '[' )
81 else if( s
[token_end
] == ']' )
83 else if( (s
[token_end
] == ',') && (level
== 0) )
87 DAI_THROWE(MALFORMED_PROPERTY
,"Malformed Property: " + s
.substr(token_start
, token_end
- token_start
));
89 std::string value
= s
.substr(token_start
, token_end
- token_start
);
91 // store the key,value pair
94 // go on with the next one
95 token_start
= token_end
+ 1;
102 } // end of namespace dai