Map: Support Braced List Initialization (#1153)

* Map: support braced list initialization

* Use swap assignment

---------

Co-authored-by: complexlogic <complexlogic@users.noreply.github.com>
This commit is contained in:
complexlogic
2023-10-07 05:27:14 +00:00
committed by GitHub
parent c332aa04f2
commit 8a800b8c38
3 changed files with 53 additions and 0 deletions

View File

@ -28,6 +28,8 @@
#include <map>
#include <memory>
#include <initializer_list>
#include <utility>
#include "taglib.h"
@ -74,6 +76,11 @@ namespace TagLib {
*/
Map(const Map<Key, T> &m);
/*!
* Constructs a Map with the contents of the braced initializer list.
*/
Map(std::initializer_list<std::pair<const Key, T>> init);
/*!
* Destroys this instance of the Map.
*/
@ -195,6 +202,11 @@ namespace TagLib {
*/
Map<Key, T> &operator=(const Map<Key, T> &m);
/*!
* Replace the contents of the map with those of the braced initializer list
*/
Map<Key, T> &operator=(std::initializer_list<std::pair<const Key, T>> init);
/*!
* Exchanges the content of this map by the content of \a m.
*/

View File

@ -37,9 +37,13 @@ public:
MapPrivate() = default;
#ifdef WANT_CLASS_INSTANTIATION_OF_MAP
MapPrivate(const std::map<class KeyP, class TP>& m) : map(m) {}
MapPrivate(std::initializer_list<std::pair<const class KeyP, class TP>> init) : map(init) {}
std::map<class KeyP, class TP> map;
#else
MapPrivate(const std::map<KeyP, TP>& m) : map(m) {}
MapPrivate(std::initializer_list<std::pair<const KeyP, TP>> init) : map(init) {}
std::map<KeyP, TP> map;
#endif
};
@ -53,6 +57,12 @@ Map<Key, T>::Map() :
template <class Key, class T>
Map<Key, T>::Map(const Map<Key, T> &) = default;
template <class Key, class T>
Map<Key, T>::Map(std::initializer_list<std::pair<const Key, T>> init) :
d(std::make_shared<MapPrivate<Key, T>>(init))
{
}
template <class Key, class T>
Map<Key, T>::~Map() = default;
@ -180,6 +190,13 @@ T &Map<Key, T>::operator[](const Key &key)
template <class Key, class T>
Map<Key, T> &Map<Key, T>::operator=(const Map<Key, T> &) = default;
template <class Key, class T>
Map<Key, T> &Map<Key, T>::operator=(std::initializer_list<std::pair<const Key, T>> init)
{
Map(init).swap(*this);
return *this;
}
template <class Key, class T>
void Map<Key, T>::swap(Map<Key, T> &m)
{