1 #ifndef MSP_CORE_MAPUTILS_H_
2 #define MSP_CORE_MAPUTILS_H_
6 #include <msp/strings/lexicalcast.h>
10 namespace MapUtilsInternal {
12 /* This dummy struct is used to introduce a conversion, making the overloaded
13 operator below worse than the templated one provided in lexicalcast.h. */
20 /* This must be hidden in the internal namespace to avoid interfering with
22 inline void operator<<(LexicalConverter &, Any)
26 static std::string stringify_key(const T &k)
30 LexicalConverter conv((Fmt()));
34 catch(const lexical_error &)
36 return "<cannot display value>";
40 } // namespace Internal
42 class key_error: public std::runtime_error
46 key_error(const T &k):
47 runtime_error(make_what(typeid(T), MapUtilsInternal::stringify_key(k)))
50 ~key_error() throw() override = default;
53 static std::string make_what(const std::type_info &, const std::string &);
58 typename T::mapped_type &get_item(T &map, const typename T::key_type &key)
60 auto i = map.find(key);
68 const typename T::mapped_type &get_item(const T &map, const typename T::key_type &key)
70 auto i = map.find(key);
77 template<typename D, typename T>
78 D *get_item(const T &map, const typename T::key_type &key)
80 return dynamic_cast<D *>(get_item(map, key));
84 typename T::iterator insert_unique(T &map, const typename T::key_type &key, const typename T::mapped_type &item)
89 return map.insert(std::make_pair(key, item)).first;
93 void remove_existing(T &map, const typename T::key_type &key)