X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcore%2Fmaputils.h;h=9a72d4d30f30e0339bec305b5589dc14e73be9d1;hb=HEAD;hp=ecee0fbea45955dbf9bef8f8bf296bb387b3e414;hpb=85c6b143c9b85ce09ee4f3cb842d3ae006b7a6fe;p=libs%2Fcore.git diff --git a/source/core/maputils.h b/source/core/maputils.h index ecee0fb..9a72d4d 100644 --- a/source/core/maputils.h +++ b/source/core/maputils.h @@ -4,6 +4,7 @@ #include #include #include +#include "mspcore_api.h" namespace Msp { @@ -28,7 +29,7 @@ static std::string stringify_key(const T &k) try { LexicalConverter conv((Fmt())); - conv< @@ -47,7 +48,7 @@ public: runtime_error(make_what(typeid(T), MapUtilsInternal::stringify_key(k))) { } - virtual ~key_error() throw() { } + ~key_error() throw() override = default; private: static std::string make_what(const std::type_info &, const std::string &); @@ -57,7 +58,7 @@ private: template typename T::mapped_type &get_item(T &map, const typename T::key_type &key) { - typename T::iterator i = map.find(key); + auto i = map.find(key); if(i==map.end()) throw key_error(key); @@ -67,20 +68,26 @@ typename T::mapped_type &get_item(T &map, const typename T::key_type &key) template const typename T::mapped_type &get_item(const T &map, const typename T::key_type &key) { - typename T::const_iterator i = map.find(key); + auto i = map.find(key); if(i==map.end()) throw key_error(key); return i->second; } +template +D *get_item(const T &map, const typename T::key_type &key) +{ + return dynamic_cast(get_item(map, key)); +} + template -const typename T::iterator insert_unique(T &map, const typename T::key_type &key, const typename T::mapped_type &item) +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(key); - return map.insert(typename T::value_type(key, item)).first; + return map.insert(std::make_pair(key, item)).first; } template