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>
try
{
LexicalConverter conv((Fmt()));
- conv<<k;
+ conv << k;
return conv.get();
}
catch(const lexical_error &)
template<typename T>
typename T::mapped_type &get_item(T &map, const typename T::key_type &key)
{
- typename T::iterator i = map.find(key);
+ auto i = map.find(key);
if(i==map.end())
throw key_error(key);
template<typename T>
const typename T::mapped_type &get_item(const T &map, const typename T::key_type &key)
{
- typename T::const_iterator i = map.find(key);
+ auto i = map.find(key);
if(i==map.end())
throw key_error(key);
return i->second;
}
+template<typename D, typename T>
+D *get_item(const T &map, const typename T::key_type &key)
+{
+ return dynamic_cast<D *>(get_item(map, key));
+}
+
template<typename T>
-const typename T::iterator insert_unique(T &map, const typename T::key_type &key, const typename T::mapped_type &item)
+typename T::iterator insert_unique(T &map, const typename T::key_type &key, const typename T::mapped_type &item)
{
if(map.count(key))
throw key_error(key);
- return map.insert(typename T::value_type(key, item)).first;
+ return map.insert(std::make_pair(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