diff --git a/taglib/toolkit/tmap.h b/taglib/toolkit/tmap.h index f54e5a2a..2f920d2f 100644 --- a/taglib/toolkit/tmap.h +++ b/taglib/toolkit/tmap.h @@ -153,6 +153,14 @@ namespace TagLib { */ Map &erase(const Key &key); + /*! + * Returns the value associated with \a key. + * + * If the map does not contain \a key, it returns defaultValue. + * If no defaultValue is specified, it returns a default-constructed value. + */ + const T value(const Key &key, const T &defaultValue = T()) const; + /*! * Returns a reference to the value associated with \a key. * diff --git a/taglib/toolkit/tmap.tcc b/taglib/toolkit/tmap.tcc index 2e4ed5eb..18eec110 100644 --- a/taglib/toolkit/tmap.tcc +++ b/taglib/toolkit/tmap.tcc @@ -155,6 +155,13 @@ unsigned int Map::size() const return static_cast(d->map.size()); } +template +const T Map::value(const Key &key, const T &defaultValue) const +{ + ConstIterator it = d->map.find(key); + return it != d->map.end() ? it->second : defaultValue; +} + template const T &Map::operator[](const Key &key) const { diff --git a/taglib/toolkit/tpropertymap.cpp b/taglib/toolkit/tpropertymap.cpp index 04bd3b7c..e1b9746c 100644 --- a/taglib/toolkit/tpropertymap.cpp +++ b/taglib/toolkit/tpropertymap.cpp @@ -117,6 +117,12 @@ PropertyMap &PropertyMap::merge(const PropertyMap &other) return *this; } +const StringList PropertyMap::value(const String &key, + const StringList &defaultValue) const +{ + return SimplePropertyMap::value(key.upper(), defaultValue); +} + const StringList &PropertyMap::operator[](const String &key) const { return SimplePropertyMap::operator[](key.upper()); diff --git a/taglib/toolkit/tpropertymap.h b/taglib/toolkit/tpropertymap.h index 58bab904..de497869 100644 --- a/taglib/toolkit/tpropertymap.h +++ b/taglib/toolkit/tpropertymap.h @@ -191,6 +191,15 @@ namespace TagLib { */ PropertyMap &merge(const PropertyMap &other); + /*! + * Returns the value associated with \a key. + * + * If the map does not contain \a key, it returns defaultValue. + * If no defaultValue is specified, it returns an empty string list. + */ + const StringList value(const String &key, + const StringList &defaultValue = StringList()) const; + /*! * Returns a reference to the value associated with \a key. *