34 #include <boost/any.hpp> 35 #include <boost/lexical_cast.hpp> 36 #include <boost/format.hpp> 37 #include <boost/operators.hpp> 39 #include "rsc/rscexports.h" 57 class RSC_EXPORT
Properties:
public std::map<std::string, boost::any>,
58 public boost::equality_comparable<Properties> {
61 template<
typename Ch,
typename Tr>
62 friend std::basic_ostream<Ch, Tr>
64 operator<<(std::basic_ostream<Ch, Tr>& stream,
93 has(
const std::string& name)
const throw ();
101 get(
const std::string& name)
const;
108 get(
const std::string& name,
const T& default_)
const;
123 T getAs(
const std::string& name)
const;
139 T getAs(
const std::string& name,
const T& default_)
const;
153 template<
typename Target,
typename T>
154 Properties&
set(
const std::string& name,
const T& value)
throw ();
169 template<
typename Ch,
typename Tr>
170 std::basic_ostream<Ch, Tr>&
171 operator<<(std::basic_ostream<Ch, Tr>& stream,
172 const std::pair<std::string, boost::any>& property);
174 template<
typename Ch,
typename Tr>
175 std::basic_ostream<Ch, Tr>&
176 operator<<(std::basic_ostream<Ch, Tr>& stream,
const Properties& properties);
183 if ((it = find(name)) == end()) {
185 (boost::format(
"no such property `%1%'") % name).str());
189 return boost::any_cast<T>(it->second);
190 }
catch (
const boost::bad_any_cast&) {
193 "properties: type mismatch in get for `%1%': requested: %2%; actual: %3%")
194 % name % typeName<T> () %
typeName(it->second.type()))
203 if ((it = find(name)) == end()) {
208 return boost::any_cast<T>(it->second);
209 }
catch (
const boost::bad_any_cast&) {
210 std::cerr << (boost::format(
211 "properties type mismatch in get for `%1%': requested: %2%; actual: %3%")
212 % name % typeName<T> () %
typeName(it->second.type())) << std::endl;
219 std::string value = get<std::string>(name);
221 return boost::lexical_cast<T>(value);
222 }
catch (
const std::bad_cast&) {
225 "properties: type conversion failure in getAs for `%1%': requested: %2%; value: \"%3%\"")
226 % name % typeName<T> () % value)
235 return getAs<T>(name);
241 template<
typename Target,
typename T>
249 template<
typename Ch,
typename Tr>
250 std::basic_ostream<Ch, Tr>&
251 operator<<(std::basic_ostream<Ch, Tr>& stream,
const Properties& properties) {
254 for (Properties::const_iterator it = properties.begin(); it
255 != properties.end();) {
256 stream << it->first <<
": ";
258 if (it->second.type() ==
typeid(std::string)) {
259 stream <<
"\"" << boost::any_cast<std::string>(it->second) <<
"\"";
260 }
else if (it->second.type() ==
typeid(bool)) {
261 stream << boost::any_cast<bool>(it->second);
262 }
else if (it->second.type() ==
typeid(int)) {
263 stream << boost::any_cast<int>(it->second);
264 }
else if (it->second.type() ==
typeid(
unsigned int)) {
265 stream << boost::any_cast<unsigned int>(it->second);
266 }
else if (it->second.type() ==
typeid(double)) {
267 stream << boost::any_cast<double>(it->second);
269 stream <<
"<" +
typeName(it->second.type()) +
">";
272 stream << ((++it) != properties.end() ?
", " :
"");
std::string typeName(const std::type_info &type)
Returns a (demangled) string representation of type.
Properties & set(const std::string &name, const T &value)
Sets a new property in the map.
pair< _T1, _T2 > make_pair(_T1 __x, _T2 __y)
A convenience wrapper for creating a pair from two objects.
T get(const std::string &name) const
T getAs(const std::string &name) const
Parse the value of the property name as type T and return the parsed value.
bool operator==(const pair< _T1, _T2 > &__x, const pair< _T1, _T2 > &__y)
Two pairs of the same type are equal iff their members are equal.
Properties objects are basically glorified map<string, boost::any> objects.
ostream & operator<<(ostream &stream, const Printable &record)
Output operator on std::ostream for reference Printables.
This exception is thrown if a specified object does not exist.