X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fmaputils.h;h=8c642fb9c57a1f9acda6ba5ab2d0dc5311316306;hb=e0266badf098a5cec2512eb693bca771e35fd3ab;hp=1b36c69569b6973992d381c7d39060e412341c89;hpb=79482ba7aea1b79c7a310c940cc0292532ef3bcb;p=libs%2Fcore.git diff --git a/source/core/maputils.h b/source/core/maputils.h index 1b36c69..8c642fb 100644 --- a/source/core/maputils.h +++ b/source/core/maputils.h @@ -3,13 +3,48 @@ #include #include +#include namespace Msp { +namespace MapUtilsInternal { + +/* This must be hidden in the internal namespace to avoid interfering with +other things. There may be problems if a key type has operator<< for ostream +but not LexicalConverter. */ +template +void operator<<(LexicalConverter &, const T &) +{ } + +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 &); + 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 &); }; @@ -18,7 +53,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; } @@ -28,7 +63,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; } @@ -37,7 +72,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; }