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>
155 set(
const std::string& name,
const T& value)
throw ();
170 template<
typename Ch,
typename Tr>
171 std::basic_ostream<Ch, Tr>&
172 operator<<(std::basic_ostream<Ch, Tr>& stream,
173 const std::pair<std::string, boost::any>& property);
175 template<
typename Ch,
typename Tr>
176 std::basic_ostream<Ch, Tr>&
177 operator<<(std::basic_ostream<Ch, Tr>& stream,
const Properties& properties);
184 if ((it = find(name)) == end()) {
186 (boost::format(
"no such property `%1%'") % name).str());
190 return boost::any_cast<T>(it->second);
191 }
catch (
const boost::bad_any_cast&) {
194 "properties: type mismatch in get for `%1%': requested: %2%; actual: %3%")
195 % name % typeName<T> () %
typeName(it->second.type()))
204 if ((it = find(name)) == end()) {
209 return boost::any_cast<T>(it->second);
210 }
catch (
const boost::bad_any_cast&) {
211 std::cerr << (boost::format(
212 "properties type mismatch in get for `%1%': requested: %2%; actual: %3%")
213 % name % typeName<T> () %
typeName(it->second.type())) << std::endl;
220 std::string value = get<std::string>(name);
222 return boost::lexical_cast<T>(value);
223 }
catch (
const std::bad_cast&) {
226 "properties: type conversion failure in getAs for `%1%': requested: %2%; value: \"%3%\"")
227 % name % typeName<T> () % value)
236 return getAs<T>(name);
242 template<
typename Target,
typename T>
245 return insert(
std::make_pair(name, static_cast<Target> (value))).second;
250 template<
typename Ch,
typename Tr>
251 std::basic_ostream<Ch, Tr>&
252 operator<<(std::basic_ostream<Ch, Tr>& stream,
const Properties& properties) {
255 for (Properties::const_iterator it = properties.begin(); it
256 != properties.end();) {
257 stream << it->first <<
": ";
259 if (it->second.type() ==
typeid(std::string)) {
260 stream <<
"\"" << boost::any_cast<std::string>(it->second) <<
"\"";
261 }
else if (it->second.type() ==
typeid(bool)) {
262 stream << boost::any_cast<bool>(it->second);
263 }
else if (it->second.type() ==
typeid(int)) {
264 stream << boost::any_cast<int>(it->second);
265 }
else if (it->second.type() ==
typeid(
unsigned int)) {
266 stream << boost::any_cast<unsigned int>(it->second);
267 }
else if (it->second.type() ==
typeid(double)) {
268 stream << boost::any_cast<double>(it->second);
270 stream <<
"<" +
typeName(it->second.type()) +
">";
273 stream << ((++it) != properties.end() ?
", " :
"");
bool set(const std::string &name, const T &value)
Sets a new property in the map.
std::string typeName(const std::type_info &type)
Returns a (demangled) string representation of type.
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.