diff options
Diffstat (limited to 'src/corelib/serialization/qjsonarray.h')
-rw-r--r-- | src/corelib/serialization/qjsonarray.h | 341 |
1 files changed, 206 insertions, 135 deletions
diff --git a/src/corelib/serialization/qjsonarray.h b/src/corelib/serialization/qjsonarray.h index 4466210ce6..26a04e9196 100644 --- a/src/corelib/serialization/qjsonarray.h +++ b/src/corelib/serialization/qjsonarray.h @@ -1,41 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtCore module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #ifndef QJSONARRAY_H #define QJSONARRAY_H @@ -48,7 +12,6 @@ QT_BEGIN_NAMESPACE class QDebug; -class QStringList; typedef QList<QVariant> QVariantList; class Q_CORE_EXPORT QJsonArray @@ -60,8 +23,8 @@ public: ~QJsonArray(); - QJsonArray(const QJsonArray &other); - QJsonArray &operator =(const QJsonArray &other); + QJsonArray(const QJsonArray &other) noexcept; + QJsonArray &operator =(const QJsonArray &other) noexcept; QJsonArray(QJsonArray &&other) noexcept; @@ -75,136 +38,233 @@ public: static QJsonArray fromVariantList(const QVariantList &list); QVariantList toVariantList() const; - int size() const; - inline int count() const { return size(); } + qsizetype size() const; + inline qsizetype count() const { return size(); } bool isEmpty() const; - QJsonValue at(int i) const; + QJsonValue at(qsizetype i) const; QJsonValue first() const; QJsonValue last() const; void prepend(const QJsonValue &value); void append(const QJsonValue &value); - void removeAt(int i); - QJsonValue takeAt(int i); + void removeAt(qsizetype i); + QJsonValue takeAt(qsizetype i); inline void removeFirst() { removeAt(0); } inline void removeLast() { removeAt(size() - 1); } - void insert(int i, const QJsonValue &value); - void replace(int i, const QJsonValue &value); + void insert(qsizetype i, const QJsonValue &value); + void replace(qsizetype i, const QJsonValue &value); bool contains(const QJsonValue &element) const; - QJsonValueRef operator[](int i); - QJsonValue operator[](int i) const; + QJsonValueRef operator[](qsizetype i); + QJsonValue operator[](qsizetype i) const; +#if QT_CORE_REMOVED_SINCE(6, 8) bool operator==(const QJsonArray &other) const; bool operator!=(const QJsonArray &other) const; - +#endif void swap(QJsonArray &other) noexcept { - qSwap(a, other.a); + a.swap(other.a); } class const_iterator; class iterator { public: - QJsonArray *a; - int i; typedef std::random_access_iterator_tag iterator_category; - typedef int difference_type; + typedef qsizetype difference_type; typedef QJsonValue value_type; typedef QJsonValueRef reference; - typedef QJsonValueRefPtr pointer; - - inline iterator() : a(nullptr), i(0) { } - explicit inline iterator(QJsonArray *array, int index) : a(array), i(index) { } - - inline QJsonValueRef operator*() const { return QJsonValueRef(a, i); } -#ifdef Q_QDOC - inline QJsonValueRef* operator->() const; -#else - inline QJsonValueRefPtr operator->() const { return QJsonValueRefPtr(a, i); } + typedef QJsonValueRef *pointer; + + inline iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { } + explicit inline iterator(QJsonArray *array, qsizetype index) : item(array, index) { } + + constexpr iterator(const iterator &other) = default; + iterator &operator=(const iterator &other) + { + item.rebind(other.item); + return *this; + } + + inline QJsonValueRef operator*() const { return item; } + inline const QJsonValueConstRef *operator->() const { return &item; } + inline QJsonValueRef *operator->() { return &item; } + inline QJsonValueRef operator[](qsizetype j) const { return *(*this + j); } + +#if QT_CORE_REMOVED_SINCE(6, 8) + inline bool operator==(const iterator &o) const + { return item.d == o.item.d && item.index == o.item.index; } + inline bool operator!=(const iterator &o) const { return !operator==(o); } + inline bool operator<(const iterator &other) const + { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; } + inline bool operator<=(const iterator &other) const + { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; } + inline bool operator>(const iterator &other) const { return !operator<=(other); } + inline bool operator>=(const iterator &other) const { return !operator<(other); } + inline bool operator==(const const_iterator &o) const + { return item.d == o.item.d && item.index == o.item.index; } + inline bool operator!=(const const_iterator &o) const { return !operator==(o); } + inline bool operator<(const const_iterator &other) const + { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; } + inline bool operator<=(const const_iterator &other) const + { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; } + inline bool operator>(const const_iterator &other) const { return !operator<=(other); } + inline bool operator>=(const const_iterator &other) const { return !operator<(other); } #endif - inline QJsonValueRef operator[](int j) const { return QJsonValueRef(a, i + j); } - - inline bool operator==(const iterator &o) const { return i == o.i; } - inline bool operator!=(const iterator &o) const { return i != o.i; } - inline bool operator<(const iterator& other) const { return i < other.i; } - inline bool operator<=(const iterator& other) const { return i <= other.i; } - inline bool operator>(const iterator& other) const { return i > other.i; } - inline bool operator>=(const iterator& other) const { return i >= other.i; } - inline bool operator==(const const_iterator &o) const { return i == o.i; } - inline bool operator!=(const const_iterator &o) const { return i != o.i; } - inline bool operator<(const const_iterator& other) const { return i < other.i; } - inline bool operator<=(const const_iterator& other) const { return i <= other.i; } - inline bool operator>(const const_iterator& other) const { return i > other.i; } - inline bool operator>=(const const_iterator& other) const { return i >= other.i; } - inline iterator &operator++() { ++i; return *this; } - inline iterator operator++(int) { iterator n = *this; ++i; return n; } - inline iterator &operator--() { i--; return *this; } - inline iterator operator--(int) { iterator n = *this; i--; return n; } - inline iterator &operator+=(int j) { i+=j; return *this; } - inline iterator &operator-=(int j) { i-=j; return *this; } - inline iterator operator+(int j) const { return iterator(a, i+j); } - inline iterator operator-(int j) const { return iterator(a, i-j); } - inline int operator-(iterator j) const { return i - j.i; } + inline iterator &operator++() { ++item.index; return *this; } + inline iterator operator++(int) { iterator n = *this; ++item.index; return n; } + inline iterator &operator--() { item.index--; return *this; } + inline iterator operator--(int) { iterator n = *this; item.index--; return n; } + inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; } + inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; } + inline iterator operator+(qsizetype j) const { iterator r = *this; return r += j; } + inline iterator operator-(qsizetype j) const { return operator+(-j); } + inline qsizetype operator-(iterator j) const { return item.index - j.item.index; } + + private: + // Helper functions + static bool comparesEqual_helper(const iterator &lhs, const iterator &rhs) noexcept + { + return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index; + } + + static bool comparesEqual_helper(const iterator &lhs, const const_iterator &rhs) noexcept + { + return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index; + } + + static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs, + const iterator &rhs) noexcept + { + Q_ASSERT(lhs.item.d == rhs.item.d); + return Qt::compareThreeWay(lhs.item.index, rhs.item.index); + } + + static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs, + const const_iterator &rhs) noexcept + { + Q_ASSERT(lhs.item.d == rhs.item.d); + return Qt::compareThreeWay(lhs.item.index, rhs.item.index); + } + + // Compare friends + friend bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept + { + return comparesEqual_helper(lhs, rhs); + } + friend Qt::strong_ordering compareThreeWay(const iterator &lhs, + const iterator &rhs) noexcept + { + return compareThreeWay_helper(lhs, rhs); + } + Q_DECLARE_STRONGLY_ORDERED(iterator) + friend bool comparesEqual(const iterator &lhs, const const_iterator &rhs) noexcept + { + return comparesEqual_helper(lhs, rhs); + } + friend Qt::strong_ordering compareThreeWay(const iterator &lhs, + const const_iterator &rhs) noexcept + { + return compareThreeWay_helper(lhs, rhs); + } + Q_DECLARE_STRONGLY_ORDERED(iterator, const_iterator) + + QJsonValueRef item; + friend class QJsonArray; }; friend class iterator; class const_iterator { public: - const QJsonArray *a; - int i; typedef std::random_access_iterator_tag iterator_category; typedef qptrdiff difference_type; typedef QJsonValue value_type; - typedef QJsonValue reference; - typedef QJsonValuePtr pointer; - - inline const_iterator() : a(nullptr), i(0) { } - explicit inline const_iterator(const QJsonArray *array, int index) : a(array), i(index) { } -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - inline const_iterator(const const_iterator &o) : a(o.a), i(o.i) {} // ### Qt 6: Removed so class can be trivially-copyable -#endif - inline const_iterator(const iterator &o) : a(o.a), i(o.i) {} - - inline QJsonValue operator*() const { return a->at(i); } -#ifdef Q_QDOC - inline QJsonValue* operator->() const; -#else - inline QJsonValuePtr operator->() const { return QJsonValuePtr(a->at(i)); } + typedef const QJsonValueRef reference; + typedef const QJsonValueRef *pointer; + + inline const_iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { } + explicit inline const_iterator(const QJsonArray *array, qsizetype index) + : item(const_cast<QJsonArray *>(array), index) { } + inline const_iterator(const iterator &o) : item(o.item) { } + + constexpr const_iterator(const const_iterator &other) = default; + const_iterator &operator=(const const_iterator &other) + { + item.rebind(other.item); + return *this; + } + + inline const QJsonValueConstRef operator*() const { return item; } + inline const QJsonValueConstRef *operator->() const { return &item; } + + inline QJsonValueConstRef operator[](qsizetype j) const { return *(*this + j); } +#if QT_CORE_REMOVED_SINCE(6, 8) + inline bool operator==(const const_iterator &o) const + { return item.d == o.item.d && item.index == o.item.index; } + inline bool operator!=(const const_iterator &o) const { return !operator==(o); } + inline bool operator<(const const_iterator &other) const + { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; } + inline bool operator<=(const const_iterator &other) const + { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; } + inline bool operator>(const const_iterator &other) const { return !operator<=(other); } + inline bool operator>=(const const_iterator &other) const { return !operator<(other); } #endif - inline QJsonValue operator[](int j) const { return a->at(i+j); } - inline bool operator==(const const_iterator &o) const { return i == o.i; } - inline bool operator!=(const const_iterator &o) const { return i != o.i; } - inline bool operator<(const const_iterator& other) const { return i < other.i; } - inline bool operator<=(const const_iterator& other) const { return i <= other.i; } - inline bool operator>(const const_iterator& other) const { return i > other.i; } - inline bool operator>=(const const_iterator& other) const { return i >= other.i; } - inline const_iterator &operator++() { ++i; return *this; } - inline const_iterator operator++(int) { const_iterator n = *this; ++i; return n; } - inline const_iterator &operator--() { i--; return *this; } - inline const_iterator operator--(int) { const_iterator n = *this; i--; return n; } - inline const_iterator &operator+=(int j) { i+=j; return *this; } - inline const_iterator &operator-=(int j) { i-=j; return *this; } - inline const_iterator operator+(int j) const { return const_iterator(a, i+j); } - inline const_iterator operator-(int j) const { return const_iterator(a, i-j); } - inline int operator-(const_iterator j) const { return i - j.i; } + inline const_iterator &operator++() { ++item.index; return *this; } + inline const_iterator operator++(int) { const_iterator n = *this; ++item.index; return n; } + inline const_iterator &operator--() { item.index--; return *this; } + inline const_iterator operator--(int) { const_iterator n = *this; item.index--; return n; } + inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; } + inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; } + inline const_iterator operator+(qsizetype j) const { const_iterator r = *this; return r += j; } + inline const_iterator operator-(qsizetype j) const { return operator+(-j); } + inline qsizetype operator-(const_iterator j) const { return item.index - j.item.index; } + + private: + // Helper functions + static bool comparesEqual_helper(const const_iterator &lhs, + const const_iterator &rhs) noexcept + { + return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index; + } + static Qt::strong_ordering compareThreeWay_helper(const const_iterator &lhs, + const const_iterator &rhs) noexcept + { + Q_ASSERT(lhs.item.d == rhs.item.d); + return Qt::compareThreeWay(lhs.item.index, rhs.item.index); + } + + // Compare friends + friend bool comparesEqual(const const_iterator &lhs, const const_iterator &rhs) noexcept + { + return comparesEqual_helper(lhs, rhs); + } + friend Qt::strong_ordering compareThreeWay(const const_iterator &lhs, + const const_iterator &rhs) noexcept + { + return compareThreeWay_helper(lhs, rhs); + } + Q_DECLARE_STRONGLY_ORDERED(const_iterator) + QJsonValueConstRef item; + friend class QJsonArray; }; friend class const_iterator; // stl style - inline iterator begin() { detach2(); return iterator(this, 0); } + inline iterator begin() { detach(); return iterator(this, 0); } inline const_iterator begin() const { return const_iterator(this, 0); } inline const_iterator constBegin() const { return const_iterator(this, 0); } inline const_iterator cbegin() const { return const_iterator(this, 0); } - inline iterator end() { detach2(); return iterator(this, size()); } + inline iterator end() { detach(); return iterator(this, size()); } inline const_iterator end() const { return const_iterator(this, size()); } inline const_iterator constEnd() const { return const_iterator(this, size()); } inline const_iterator cend() const { return const_iterator(this, size()); } - iterator insert(iterator before, const QJsonValue &value) { insert(before.i, value); return before; } - iterator erase(iterator it) { removeAt(it.i); return it; } + iterator insert(iterator before, const QJsonValue &value) + { insert(before.item.index, value); return before; } + iterator erase(iterator it) + { removeAt(it.item.index); return it; } // more Qt typedef iterator Iterator; @@ -224,34 +284,45 @@ public: inline void pop_front() { removeFirst(); } inline void pop_back() { removeLast(); } inline bool empty() const { return isEmpty(); } - typedef int size_type; + typedef qsizetype size_type; typedef QJsonValue value_type; typedef value_type *pointer; typedef const value_type *const_pointer; typedef QJsonValueRef reference; typedef QJsonValue const_reference; - typedef int difference_type; + typedef qsizetype difference_type; private: friend class QJsonValue; + friend class QJsonValueConstRef; + friend class QJsonValueRef; + friend class QJsonPrivate::Value; friend class QJsonDocument; friend class QCborArray; friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &); + friend Q_CORE_EXPORT bool comparesEqual(const QJsonArray &lhs, + const QJsonArray &rhs) noexcept; + + friend Q_CORE_EXPORT bool comparesEqual(const QJsonArray &lhs, + const QJsonValue &rhs) noexcept; + Q_DECLARE_EQUALITY_COMPARABLE(QJsonArray) + Q_DECLARE_EQUALITY_COMPARABLE(QJsonArray, QJsonValue) + QJsonArray(QCborContainerPrivate *array); - void initialize(); - void compact(); - // ### Qt 6: remove me and merge with detach2 - void detach(uint reserve = 0); - bool detach2(uint reserve = 0); - - // ### Qt 6: remove - void *dead = nullptr; + bool detach(qsizetype reserve = 0); + QExplicitlySharedDataPointer<QCborContainerPrivate> a; }; Q_DECLARE_SHARED(QJsonArray) +#if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED) +inline QJsonValueConstRef::QJsonValueConstRef(QJsonArray *a, qsizetype idx) + : d(a ? a->a.data() : nullptr), is_object(false), index(idx) +{} +#endif + Q_CORE_EXPORT size_t qHash(const QJsonArray &array, size_t seed = 0); #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) |