X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fmaputils.h;h=0f863b46fdf7dea307f251be4823a4e63cdcbc4e;hb=b2333b53a0434eb6a131000c0b9bf06e4f603bd6;hp=9548714658d806fad41614357bc0e60fa8444cdf;hpb=933356a36607f5d4480172e285071d4d7dfc5a7b;p=libs%2Fcore.git diff --git a/source/core/maputils.h b/source/core/maputils.h index 9548714..0f863b4 100644 --- a/source/core/maputils.h +++ b/source/core/maputils.h @@ -3,14 +3,54 @@ #include #include +#include namespace Msp { +namespace MapUtilsInternal { + +/* This dummy struct is used to introduce a conversion, making the overloaded +operator below worse than the templated one provided in lexicalcast.h. */ +struct Any +{ + template + Any(const T &) { } +}; + +/* This must be hidden in the internal namespace to avoid interfering with +other things. */ +inline void operator<<(LexicalConverter &, Any) +{ } + +template +static std::string stringify_key(const T &k) +{ + try + { + LexicalConverter conv((Fmt())); + conv<"; + } +} + +} // namespace Internal + class key_error: public std::runtime_error { public: - key_error(const std::type_info &); - ~key_error() throw() { } + template + key_error(const T &k): + runtime_error(make_what(typeid(T), MapUtilsInternal::stringify_key(k))) + { } + + virtual ~key_error() throw() { } + +private: + static std::string make_what(const std::type_info &, const std::string &); }; @@ -19,7 +59,7 @@ typename T::mapped_type &get_item(T &map, const typename T::key_type &key) { typename T::iterator i = map.find(key); if(i==map.end()) - throw key_error(typeid(T)); + throw key_error(key); return i->second; } @@ -29,7 +69,7 @@ const typename T::mapped_type &get_item(const T &map, const typename T::key_type { typename T::const_iterator i = map.find(key); if(i==map.end()) - throw key_error(typeid(T)); + throw key_error(key); return i->second; } @@ -38,7 +78,7 @@ template const typename T::iterator insert_unique(T &map, const typename T::key_type &key, const typename T::mapped_type &item) { if(map.count(key)) - throw key_error(typeid(T)); + throw key_error(key); return map.insert(typename T::value_type(key, item)).first; }