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 virtual ~key_error() throw() { }
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 typename T::iterator i = map.find(key);
68 const typename T::mapped_type &get_item(const T &map, const typename T::key_type &key)
70 typename T::const_iterator i = map.find(key);
78 typename T::iterator insert_unique(T &map, const typename T::key_type &key, const typename T::mapped_type &item)
83 return map.insert(typename T::value_type(key, item)).first;
87 void remove_existing(T &map, const typename T::key_type &key)