X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fmaputils.h;h=26b72cc29e7b1891e96e06552ebb67645f679ebb;hp=0f863b46fdf7dea307f251be4823a4e63cdcbc4e;hb=991fabc1956b73a4007859058fb44171000b452e;hpb=676f6faa92d23c2096a6a85cac819925dbdc1a88 diff --git a/source/core/maputils.h b/source/core/maputils.h index 0f863b4..26b72cc 100644 --- a/source/core/maputils.h +++ b/source/core/maputils.h @@ -28,7 +28,7 @@ static std::string stringify_key(const T &k) try { LexicalConverter conv((Fmt())); - conv< 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 +67,35 @@ 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 +void remove_existing(T &map, const typename T::key_type &key) +{ + if(!map.count(key)) + throw key_error(key); + + map.erase(key); } } // namespace Msp