Map: Add method value() to look up without creating entry

This commit is contained in:
Urs Fleisch 2022-01-30 19:22:19 +01:00
parent 05486d00bf
commit a4bb904a01
4 changed files with 30 additions and 0 deletions

View File

@ -153,6 +153,14 @@ namespace TagLib {
*/
Map<Key, T> &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.
*

View File

@ -155,6 +155,13 @@ unsigned int Map<Key, T>::size() const
return static_cast<unsigned int>(d->map.size());
}
template <class Key, class T>
const T Map<Key, T>::value(const Key &key, const T &defaultValue) const
{
ConstIterator it = d->map.find(key);
return it != d->map.end() ? it->second : defaultValue;
}
template <class Key, class T>
const T &Map<Key, T>::operator[](const Key &key) const
{

View File

@ -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());

View File

@ -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.
*