diff options
Diffstat (limited to 'src/corelib/tools/qlinkedlist.h')
-rw-r--r-- | src/corelib/tools/qlinkedlist.h | 99 |
1 files changed, 64 insertions, 35 deletions
diff --git a/src/corelib/tools/qlinkedlist.h b/src/corelib/tools/qlinkedlist.h index 1e6d4df474..8970d39be0 100644 --- a/src/corelib/tools/qlinkedlist.h +++ b/src/corelib/tools/qlinkedlist.h @@ -40,18 +40,21 @@ #ifndef QLINKEDLIST_H #define QLINKEDLIST_H +#include <QtCore/qglobal.h> + +#ifndef QT_NO_LINKED_LIST + #include <QtCore/qiterator.h> #include <QtCore/qrefcount.h> +#include <QtCore/qcontainertools_impl.h> +#include <QtCore/qdatastream.h> +#include <QtCore/qtypeinfo.h> +#include <algorithm> +#include <initializer_list> #include <iterator> #include <list> -#include <algorithm> - -#if defined(Q_COMPILER_INITIALIZER_LISTS) -# include <initializer_list> -#endif - QT_BEGIN_NAMESPACE @@ -80,24 +83,23 @@ class QLinkedList union { QLinkedListData *d; QLinkedListNode<T> *e; }; public: - inline QLinkedList() Q_DECL_NOTHROW : d(const_cast<QLinkedListData *>(&QLinkedListData::shared_null)) { } + inline QLinkedList() noexcept : d(const_cast<QLinkedListData *>(&QLinkedListData::shared_null)) { } inline QLinkedList(const QLinkedList<T> &l) : d(l.d) { d->ref.ref(); if (!d->sharable) detach(); } -#if defined(Q_COMPILER_INITIALIZER_LISTS) inline QLinkedList(std::initializer_list<T> list) - : d(const_cast<QLinkedListData *>(&QLinkedListData::shared_null)) + : QLinkedList(list.begin(), list.end()) {} + template <typename InputIterator, QtPrivate::IfIsInputIterator<InputIterator> = true> + inline QLinkedList(InputIterator first, InputIterator last) + : QLinkedList() { - std::copy(list.begin(), list.end(), std::back_inserter(*this)); + std::copy(first, last, std::back_inserter(*this)); } -#endif ~QLinkedList(); QLinkedList<T> &operator=(const QLinkedList<T> &); -#ifdef Q_COMPILER_RVALUE_REFS - QLinkedList(QLinkedList<T> &&other) Q_DECL_NOTHROW + QLinkedList(QLinkedList<T> &&other) noexcept : d(other.d) { other.d = const_cast<QLinkedListData *>(&QLinkedListData::shared_null); } - QLinkedList<T> &operator=(QLinkedList<T> &&other) Q_DECL_NOTHROW + QLinkedList<T> &operator=(QLinkedList<T> &&other) noexcept { QLinkedList moved(std::move(other)); swap(moved); return *this; } -#endif - inline void swap(QLinkedList<T> &other) Q_DECL_NOTHROW { qSwap(d, other.d); } + inline void swap(QLinkedList<T> &other) noexcept { qSwap(d, other.d); } bool operator==(const QLinkedList<T> &l) const; inline bool operator!=(const QLinkedList<T> &l) const { return !(*this == l); } @@ -137,10 +139,10 @@ public: inline iterator() : i(nullptr) {} inline iterator(Node *n) : i(n) {} #if QT_VERSION < QT_VERSION_CHECK(6,0,0) - iterator(const iterator &other) Q_DECL_NOTHROW : i(other.i) {} - iterator &operator=(const iterator &other) Q_DECL_NOTHROW { i = other.i; return *this; } - iterator(iterator &&other) Q_DECL_NOTHROW : i(other.i) {} - iterator &operator=(iterator &&other) Q_DECL_NOTHROW { return *this = other; } + iterator(const iterator &other) noexcept : i(other.i) {} + iterator &operator=(const iterator &other) noexcept { i = other.i; return *this; } + iterator(iterator &&other) noexcept : i(other.i) {} + iterator &operator=(iterator &&other) noexcept { return *this = other; } #endif inline T &operator*() const { return i->t; } inline T *operator->() const { return &i->t; } @@ -176,10 +178,10 @@ public: inline const_iterator(Node *n) : i(n) {} inline const_iterator(iterator ci) : i(ci.i){} #if QT_VERSION < QT_VERSION_CHECK(6,0,0) - const_iterator(const const_iterator &other) Q_DECL_NOTHROW : i(other.i) {} - const_iterator &operator=(const const_iterator &other) Q_DECL_NOTHROW { i = other.i; return *this; } - const_iterator(const_iterator &&other) Q_DECL_NOTHROW : i(other.i) {} - const_iterator &operator=(const_iterator &&other) Q_DECL_NOTHROW { return *this = other; } + const_iterator(const const_iterator &other) noexcept : i(other.i) {} + const_iterator &operator=(const const_iterator &other) noexcept { i = other.i; return *this; } + const_iterator(const_iterator &&other) noexcept : i(other.i) {} + const_iterator &operator=(const_iterator &&other) noexcept { return *this = other; } #endif inline const T &operator*() const { return i->t; } inline const T *operator->() const { return &i->t; } @@ -203,20 +205,20 @@ public: typedef std::reverse_iterator<const_iterator> const_reverse_iterator; inline iterator begin() { detach(); return e->n; } - inline const_iterator begin() const Q_DECL_NOTHROW { return e->n; } - inline const_iterator cbegin() const Q_DECL_NOTHROW { return e->n; } - inline const_iterator constBegin() const Q_DECL_NOTHROW { return e->n; } + inline const_iterator begin() const noexcept { return e->n; } + inline const_iterator cbegin() const noexcept { return e->n; } + inline const_iterator constBegin() const noexcept { return e->n; } inline iterator end() { detach(); return e; } - inline const_iterator end() const Q_DECL_NOTHROW { return e; } - inline const_iterator cend() const Q_DECL_NOTHROW { return e; } - inline const_iterator constEnd() const Q_DECL_NOTHROW { return e; } + inline const_iterator end() const noexcept { return e; } + inline const_iterator cend() const noexcept { return e; } + inline const_iterator constEnd() const noexcept { return e; } reverse_iterator rbegin() { return reverse_iterator(end()); } reverse_iterator rend() { return reverse_iterator(begin()); } - const_reverse_iterator rbegin() const Q_DECL_NOTHROW { return const_reverse_iterator(end()); } - const_reverse_iterator rend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); } - const_reverse_iterator crbegin() const Q_DECL_NOTHROW { return const_reverse_iterator(end()); } - const_reverse_iterator crend() const Q_DECL_NOTHROW { return const_reverse_iterator(begin()); } + const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); } + const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); } + const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); } + const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); } iterator insert(iterator before, const T &t); iterator erase(iterator pos); @@ -270,6 +272,15 @@ private: iterator detach_helper2(iterator); void freeData(QLinkedListData*); }; +template <typename T> +Q_DECLARE_TYPEINFO_BODY(QLinkedList<T>, Q_MOVABLE_TYPE|Q_RELOCATABLE_TYPE); + +#if defined(__cpp_deduction_guides) && __cpp_deduction_guides >= 201606 +template <typename InputIterator, + typename ValueType = typename std::iterator_traits<InputIterator>::value_type, + QtPrivate::IfIsInputIterator<InputIterator> = true> +QLinkedList(InputIterator, InputIterator) -> QLinkedList<ValueType>; +#endif template <typename T> inline QLinkedList<T>::~QLinkedList() @@ -340,7 +351,7 @@ void QLinkedList<T>::freeData(QLinkedListData *x) { Node *y = reinterpret_cast<Node*>(x); Node *i = y->n; - Q_ASSERT(x->ref.atomic.load() == 0); + Q_ASSERT(x->ref.atomic.loadRelaxed() == 0); while (i != y) { Node *n = i; i = i->n; @@ -565,6 +576,24 @@ QLinkedList<T> QLinkedList<T>::operator+(const QLinkedList<T> &l) const Q_DECLARE_SEQUENTIAL_ITERATOR(LinkedList) Q_DECLARE_MUTABLE_SEQUENTIAL_ITERATOR(LinkedList) +#ifndef QT_NO_DATASTREAM +template <typename T> +inline QDataStream &operator>>(QDataStream &s, QLinkedList<T> &l) +{ + return QtPrivate::readListBasedContainer(s, l); +} + +template <typename T> +inline QDataStream &operator<<(QDataStream &s, const QLinkedList<T> &l) +{ + return QtPrivate::writeSequentialContainer(s, l); +} +#endif + QT_END_NAMESPACE +Q_DECLARE_SEQUENTIAL_CONTAINER_METATYPE(QLinkedList) + +#endif // QT_NO_LINKED_LIST + #endif // QLINKEDLIST_H |