1 #ifndef MSP_CORE_MAPUTILS_H_
2 #define MSP_CORE_MAPUTILS_H_
6 #include <msp/strings/lexicalcast.h>
10 namespace MapUtilsInternal {
12 /* This must be hidden in the internal namespace to avoid interfering with
13 other things. There may be problems if a key type has operator<< for ostream
14 but not LexicalConverter. */
16 void operator<<(LexicalConverter &, const T &)
20 static std::string stringify_key(const T &k)
24 LexicalConverter conv((Fmt()));
28 catch(const lexical_error &)
30 return "<cannot display value>";
34 } // namespace Internal
36 class key_error: public std::runtime_error
40 key_error(const T &k):
41 runtime_error(make_what(typeid(T), MapUtilsInternal::stringify_key(k)))
44 virtual ~key_error() throw() { }
47 static std::string make_what(const std::type_info &, const std::string &);
52 typename T::mapped_type &get_item(T &map, const typename T::key_type &key)
54 typename T::iterator i = map.find(key);
62 const typename T::mapped_type &get_item(const T &map, const typename T::key_type &key)
64 typename T::const_iterator i = map.find(key);
72 const typename T::iterator insert_unique(T &map, const typename T::key_type &key, const typename T::mapped_type &item)
77 return map.insert(typename T::value_type(key, item)).first;