X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fcore%2Fmaputils.h;fp=source%2Fcore%2Fmaputils.h;h=1a7c0ae6ce8e952d1ab9b10113e47172dc4b3136;hp=0000000000000000000000000000000000000000;hb=9519381422961a538a3ebbd1e6563d170215f14e;hpb=1f0843257065789231a9949e0a81b79afd7bbebe diff --git a/source/core/maputils.h b/source/core/maputils.h new file mode 100644 index 0000000..1a7c0ae --- /dev/null +++ b/source/core/maputils.h @@ -0,0 +1,38 @@ +#ifndef MSP_CORE_MAPUTILS_H_ +#define MSP_CORE_MAPUTILS_H_ + +#include +#include + +namespace Msp { + +class key_error: public std::runtime_error +{ +public: + key_error(const std::type_info &); +}; + + +template +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)); + + return i->second; +} + +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); + if(i==map.end()) + throw key_error(typeid(T)); + + return i->second; +} + +} // namespace Msp + +#endif