List: Support Braced List Initialization (#1154)

* List: support braced list initialization

* Use swap for assignment

---------

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

View File

@ -27,6 +27,7 @@
#define TAGLIB_LIST_H
#include <list>
#include <initializer_list>
#include <memory>
#include "taglib.h"
@ -71,6 +72,11 @@ namespace TagLib {
*/
List(const List<T> &l);
/*!
* Construct a List with the contents of the braced initiliazer list
*/
List(std::initializer_list<T> init);
/*!
* Destroys this List instance. If auto deletion is enabled and this list
* contains a pointer type all of the members are also deleted.
@ -247,6 +253,13 @@ namespace TagLib {
*/
List<T> &operator=(const List<T> &l);
/*!
* Replace the contents of the list with those of the braced initializer list.
*
* If auto deletion is enabled and the list contains a pointer type, the members are also deleted
*/
List<T> &operator=(std::initializer_list<T> init);
/*!
* Exchanges the content of this list by the content of \a l.
*/

View File

@ -53,6 +53,7 @@ template <class TP> class List<T>::ListPrivate : public ListPrivateBase
public:
using ListPrivateBase::ListPrivateBase;
ListPrivate(const std::list<TP> &l) : list(l) {}
ListPrivate(std::initializer_list<TP> init) : list(init) {}
void clear() {
list.clear();
}
@ -68,6 +69,7 @@ template <class TP> class List<T>::ListPrivate<TP *> : public ListPrivateBase
public:
using ListPrivateBase::ListPrivateBase;
ListPrivate(const std::list<TP *> &l) : list(l) {}
ListPrivate(std::initializer_list<TP *> init) : list(init) {}
~ListPrivate() {
clear();
}
@ -96,6 +98,12 @@ List<T>::List() :
template <class T>
List<T>::List(const List<T> &) = default;
template <class T>
List<T>::List(std::initializer_list<T> init) :
d(std::make_shared<ListPrivate<T>>(init))
{
}
template <class T>
List<T>::~List() = default;
@ -293,6 +301,15 @@ const T &List<T>::operator[](unsigned int i) const
template <class T>
List<T> &List<T>::operator=(const List<T> &) = default;
template <class T>
List<T> &List<T>::operator=(std::initializer_list<T> init)
{
bool autoDelete = d->autoDelete;
List(init).swap(*this);
setAutoDelete(autoDelete);
return *this;
}
template <class T>
void List<T>::swap(List<T> &l)
{