]> git.tdb.fi Git - libs/core.git/blobdiff - source/core/maputils.h
Add a function to remove an item from a map, checking for existence
[libs/core.git] / source / core / maputils.h
index 8c642fb9c57a1f9acda6ba5ab2d0dc5311316306..ecee0fbea45955dbf9bef8f8bf296bb387b3e414 100644 (file)
@@ -9,11 +9,17 @@ namespace Msp {
 
 namespace MapUtilsInternal {
 
+/* This dummy struct is used to introduce a conversion, making the overloaded
+operator below worse than the templated one provided in lexicalcast.h. */
+struct Any
+{
+       template<typename T>
+       Any(const T &) { }
+};
+
 /* This must be hidden in the internal namespace to avoid interfering with
-other things.  There may be problems if a key type has operator<< for ostream
-but not LexicalConverter. */
-template<typename T>
-void operator<<(LexicalConverter &, const T &)
+other things. */
+inline void operator<<(LexicalConverter &, Any)
 { }
 
 template<typename T>
@@ -77,6 +83,15 @@ const typename T::iterator insert_unique(T &map, const typename T::key_type &key
        return map.insert(typename T::value_type(key, item)).first;
 }
 
+template<typename T>
+void remove_existing(T &map, const typename T::key_type &key)
+{
+       if(!map.count(key))
+               throw key_error(key);
+
+       map.erase(key);
+}
+
 } // namespace Msp
 
 #endif