123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 | // 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#include <QtCore/qjsonvalue.h>#include <QtCore/qiterator.h>#include <QtCore/qshareddata.h>#include <initializer_list> QT_BEGIN_NAMESPACE class QDebug;typedef QList<QVariant> QVariantList;class Q_CORE_EXPORT QJsonArray {public:QJsonArray();QJsonArray(std::initializer_list<QJsonValue> args);~QJsonArray();QJsonArray(const QJsonArray &other) noexcept; QJsonArray &operator=(const QJsonArray &other) noexcept;QJsonArray(QJsonArray &&other) noexcept; QJsonArray &operator=(QJsonArray &&other) noexcept {swap(other);return*this;}static QJsonArray fromStringList(const QStringList &list);static QJsonArray fromVariantList(const QVariantList &list); QVariantList toVariantList()const; qsizetype size()const;inline qsizetype count()const{returnsize(); }boolisEmpty()const; QJsonValue at(qsizetype i)const; QJsonValue first()const; QJsonValue last()const;voidprepend(const QJsonValue &value);voidappend(const QJsonValue &value);voidremoveAt(qsizetype i); QJsonValue takeAt(qsizetype i);inlinevoidremoveFirst() {removeAt(0); }inlinevoidremoveLast() {removeAt(size() -1); }voidinsert(qsizetype i,const QJsonValue &value);voidreplace(qsizetype i,const QJsonValue &value);boolcontains(const QJsonValue &element)const; QJsonValueRef operator[](qsizetype i); QJsonValue operator[](qsizetype i)const;#if QT_CORE_REMOVED_SINCE(6, 8)booloperator==(const QJsonArray &other)const;booloperator!=(const QJsonArray &other)const;#endifvoidswap(QJsonArray &other) noexcept { a.swap(other.a);}class const_iterator;class iterator {public:typedef std::random_access_iterator_tag iterator_category;typedef qsizetype difference_type;typedef QJsonValue value_type;typedef QJsonValueRef reference;typedef QJsonValueRef *pointer;inlineiterator() :item(static_cast<QJsonArray *>(nullptr),0) { }explicit inlineiterator(QJsonArray *array, qsizetype index) :item(array, index) { }constexpriterator(const iterator &other) =default; iterator &operator=(const iterator &other){ item.rebind(other.item);return*this;}inline QJsonValueRef operator*()const{return item; }inlineconst 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)inlinebooloperator==(const iterator &o)const{return item.d == o.item.d && item.index == o.item.index; }inlinebooloperator!=(const iterator &o)const{return!operator==(o); }inlinebooloperator<(const iterator &other)const{Q_ASSERT(item.d == other.item.d);return item.index < other.item.index; }inlinebooloperator<=(const iterator &other)const{Q_ASSERT(item.d == other.item.d);return item.index <= other.item.index; }inlinebooloperator>(const iterator &other)const{return!operator<=(other); }inlinebooloperator>=(const iterator &other)const{return!operator<(other); }inlinebooloperator==(const const_iterator &o)const{return item.d == o.item.d && item.index == o.item.index; }inlinebooloperator!=(const const_iterator &o)const{return!operator==(o); }inlinebooloperator<(const const_iterator &other)const{Q_ASSERT(item.d == other.item.d);return item.index < other.item.index; }inlinebooloperator<=(const const_iterator &other)const{Q_ASSERT(item.d == other.item.d);return item.index <= other.item.index; }inlinebooloperator>(const const_iterator &other)const{return!operator<=(other); }inlinebooloperator>=(const const_iterator &other)const{return!operator<(other); }#endifinline 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{returnoperator+(-j); }inline qsizetype operator-(iterator j)const{return item.index - j.item.index; }private:// Helper functionsstatic boolcomparesEqual_helper(const iterator &lhs,const iterator &rhs) noexcept {return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;}static boolcomparesEqual_helper(const iterator &lhs,const const_iterator &rhs) noexcept {return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;}staticQt::strong_ordering compareThreeWay_helper(const iterator &lhs,const iterator &rhs){Q_ASSERT(lhs.item.d == rhs.item.d);returnQt::compareThreeWay(lhs.item.index, rhs.item.index);}staticQt::strong_ordering compareThreeWay_helper(const iterator &lhs,const const_iterator &rhs){Q_ASSERT(lhs.item.d == rhs.item.d);returnQt::compareThreeWay(lhs.item.index, rhs.item.index);}// Compare friendsfriendboolcomparesEqual(const iterator &lhs,const iterator &rhs) noexcept {returncomparesEqual_helper(lhs, rhs);}friend Qt::strong_ordering compareThreeWay(const iterator &lhs,const iterator &rhs){returncompareThreeWay_helper(lhs, rhs);}Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator)friendboolcomparesEqual(const iterator &lhs,const const_iterator &rhs) noexcept {returncomparesEqual_helper(lhs, rhs);}friend Qt::strong_ordering compareThreeWay(const iterator &lhs,const const_iterator &rhs){returncompareThreeWay_helper(lhs, rhs);}Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator, const_iterator) QJsonValueRef item;friend class QJsonArray;};friend class iterator;class const_iterator {public:typedef std::random_access_iterator_tag iterator_category;typedef qptrdiff difference_type;typedef QJsonValue value_type;typedefconst QJsonValueRef reference;typedefconst QJsonValueRef *pointer;inlineconst_iterator() :item(static_cast<QJsonArray *>(nullptr),0) { }explicit inlineconst_iterator(const QJsonArray *array, qsizetype index):item(const_cast<QJsonArray *>(array), index) { }inlineconst_iterator(const iterator &o) :item(o.item) { }constexprconst_iterator(const const_iterator &other) =default; const_iterator &operator=(const const_iterator &other){ item.rebind(other.item);return*this;}inlineconst QJsonValueConstRef operator*()const{return item; }inlineconst QJsonValueConstRef *operator->()const{return&item; }inline QJsonValueConstRef operator[](qsizetype j)const{return*(*this+ j); }#if QT_CORE_REMOVED_SINCE(6, 8)inlinebooloperator==(const const_iterator &o)const{return item.d == o.item.d && item.index == o.item.index; }inlinebooloperator!=(const const_iterator &o)const{return!operator==(o); }inlinebooloperator<(const const_iterator &other)const{Q_ASSERT(item.d == other.item.d);return item.index < other.item.index; }inlinebooloperator<=(const const_iterator &other)const{Q_ASSERT(item.d == other.item.d);return item.index <= other.item.index; }inlinebooloperator>(const const_iterator &other)const{return!operator<=(other); }inlinebooloperator>=(const const_iterator &other)const{return!operator<(other); }#endifinline 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{returnoperator+(-j); }inline qsizetype operator-(const_iterator j)const{return item.index - j.item.index; }private:// Helper functionsstatic boolcomparesEqual_helper(const const_iterator &lhs,const const_iterator &rhs) noexcept {return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;}staticQt::strong_ordering compareThreeWay_helper(const const_iterator &lhs,const const_iterator &rhs){Q_ASSERT(lhs.item.d == rhs.item.d);returnQt::compareThreeWay(lhs.item.index, rhs.item.index);}// Compare friendsfriendboolcomparesEqual(const const_iterator &lhs,const const_iterator &rhs) noexcept {returncomparesEqual_helper(lhs, rhs);}friend Qt::strong_ordering compareThreeWay(const const_iterator &lhs,const const_iterator &rhs){returncompareThreeWay_helper(lhs, rhs);}Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(const_iterator) QJsonValueConstRef item;friend class QJsonArray;};friend class const_iterator;// stl styleinline iterator begin() {detach();returniterator(this,0); }inline const_iterator begin()const{returnconst_iterator(this,0); }inline const_iterator constBegin()const{returnconst_iterator(this,0); }inline const_iterator cbegin()const{returnconst_iterator(this,0); }inline iterator end() {detach();returniterator(this,size()); }inline const_iterator end()const{returnconst_iterator(this,size()); }inline const_iterator constEnd()const{returnconst_iterator(this,size()); }inline const_iterator cend()const{returnconst_iterator(this,size()); } 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 Qttypedef iterator Iterator;typedef const_iterator ConstIterator;// convenienceinline QJsonArray operator+(const QJsonValue &v)const{ QJsonArray n = *this; n += v;return n; }inline QJsonArray &operator+=(const QJsonValue &v){append(v);return*this; }inline QJsonArray &operator<< (const QJsonValue &v){append(v);return*this; }// stl compatibilityinlinevoidpush_back(const QJsonValue &t) {append(t); }inlinevoidpush_front(const QJsonValue &t) {prepend(t); }inlinevoidpop_front() {removeFirst(); }inlinevoidpop_back() {removeLast(); }inlineboolempty()const{returnisEmpty(); }typedef qsizetype size_type;typedef QJsonValue value_type;typedef value_type *pointer;typedefconst value_type *const_pointer;typedef QJsonValueRef reference;typedef QJsonValue const_reference;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 boolcomparesEqual(const QJsonArray &lhs,const QJsonArray &rhs);friend Q_CORE_EXPORT boolcomparesEqual(const QJsonArray &lhs,const QJsonValue &rhs);Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonArray)Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonArray, QJsonValue)QJsonArray(QCborContainerPrivate *array);booldetach(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_tqHash(const QJsonArray &array,size_t seed =0);#if !defined(QT_NO_DEBUG_STREAM) Q_CORE_EXPORT QDebug operator<<(QDebug,const QJsonArray &);#endif#ifndef QT_NO_DATASTREAM Q_CORE_EXPORT QDataStream &operator<<(QDataStream &,const QJsonArray &); Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonArray &);#endif QT_END_NAMESPACE #endif// QJSONARRAY_H
|