File indexing completed on 2025-09-18 09:26:32
0001
0002
0003
0004 #ifndef QJSONARRAY_H
0005 #define QJSONARRAY_H
0006
0007 #include <QtCore/qjsonvalue.h>
0008 #include <QtCore/qiterator.h>
0009 #include <QtCore/qshareddata.h>
0010 #include <initializer_list>
0011
0012 QT_BEGIN_NAMESPACE
0013
0014 class QDebug;
0015 typedef QList<QVariant> QVariantList;
0016
0017 class Q_CORE_EXPORT QJsonArray
0018 {
0019 public:
0020 QJsonArray();
0021
0022 QJsonArray(std::initializer_list<QJsonValue> args);
0023
0024 ~QJsonArray();
0025
0026 QJsonArray(const QJsonArray &other) noexcept;
0027 QJsonArray &operator =(const QJsonArray &other) noexcept;
0028
0029 QJsonArray(QJsonArray &&other) noexcept;
0030
0031 QJsonArray &operator =(QJsonArray &&other) noexcept
0032 {
0033 swap(other);
0034 return *this;
0035 }
0036
0037 static QJsonArray fromStringList(const QStringList &list);
0038 static QJsonArray fromVariantList(const QVariantList &list);
0039 QVariantList toVariantList() const;
0040
0041 qsizetype size() const;
0042 inline qsizetype count() const { return size(); }
0043
0044 bool isEmpty() const;
0045 QJsonValue at(qsizetype i) const;
0046 QJsonValue first() const;
0047 QJsonValue last() const;
0048
0049 void prepend(const QJsonValue &value);
0050 void append(const QJsonValue &value);
0051 void removeAt(qsizetype i);
0052 QJsonValue takeAt(qsizetype i);
0053 inline void removeFirst() { removeAt(0); }
0054 inline void removeLast() { removeAt(size() - 1); }
0055
0056 void insert(qsizetype i, const QJsonValue &value);
0057 void replace(qsizetype i, const QJsonValue &value);
0058
0059 bool contains(const QJsonValue &element) const;
0060 QJsonValueRef operator[](qsizetype i);
0061 QJsonValue operator[](qsizetype i) const;
0062
0063 #if QT_CORE_REMOVED_SINCE(6, 8)
0064 bool operator==(const QJsonArray &other) const;
0065 bool operator!=(const QJsonArray &other) const;
0066 #endif
0067 void swap(QJsonArray &other) noexcept
0068 {
0069 a.swap(other.a);
0070 }
0071
0072 class const_iterator;
0073
0074 class iterator {
0075 public:
0076 typedef std::random_access_iterator_tag iterator_category;
0077 typedef qsizetype difference_type;
0078 typedef QJsonValue value_type;
0079 typedef QJsonValueRef reference;
0080 typedef QJsonValueRef *pointer;
0081
0082 inline iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { }
0083 explicit inline iterator(QJsonArray *array, qsizetype index) : item(array, index) { }
0084
0085 constexpr iterator(const iterator &other) = default;
0086 iterator &operator=(const iterator &other)
0087 {
0088 item.rebind(other.item);
0089 return *this;
0090 }
0091
0092 inline QJsonValueRef operator*() const { return item; }
0093 inline const QJsonValueConstRef *operator->() const { return &item; }
0094 inline QJsonValueRef *operator->() { return &item; }
0095 inline QJsonValueRef operator[](qsizetype j) const { return *(*this + j); }
0096
0097 #if QT_CORE_REMOVED_SINCE(6, 8)
0098 inline bool operator==(const iterator &o) const
0099 { return item.d == o.item.d && item.index == o.item.index; }
0100 inline bool operator!=(const iterator &o) const { return !operator==(o); }
0101 inline bool operator<(const iterator &other) const
0102 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
0103 inline bool operator<=(const iterator &other) const
0104 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
0105 inline bool operator>(const iterator &other) const { return !operator<=(other); }
0106 inline bool operator>=(const iterator &other) const { return !operator<(other); }
0107 inline bool operator==(const const_iterator &o) const
0108 { return item.d == o.item.d && item.index == o.item.index; }
0109 inline bool operator!=(const const_iterator &o) const { return !operator==(o); }
0110 inline bool operator<(const const_iterator &other) const
0111 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
0112 inline bool operator<=(const const_iterator &other) const
0113 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
0114 inline bool operator>(const const_iterator &other) const { return !operator<=(other); }
0115 inline bool operator>=(const const_iterator &other) const { return !operator<(other); }
0116 #endif
0117 inline iterator &operator++() { ++item.index; return *this; }
0118 inline iterator operator++(int) { iterator n = *this; ++item.index; return n; }
0119 inline iterator &operator--() { item.index--; return *this; }
0120 inline iterator operator--(int) { iterator n = *this; item.index--; return n; }
0121 inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
0122 inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
0123 inline iterator operator+(qsizetype j) const { iterator r = *this; return r += j; }
0124 inline iterator operator-(qsizetype j) const { return operator+(-j); }
0125 inline qsizetype operator-(iterator j) const { return item.index - j.item.index; }
0126
0127 private:
0128
0129 static bool comparesEqual_helper(const iterator &lhs, const iterator &rhs) noexcept
0130 {
0131 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
0132 }
0133
0134 static bool comparesEqual_helper(const iterator &lhs, const const_iterator &rhs) noexcept
0135 {
0136 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
0137 }
0138
0139 static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs,
0140 const iterator &rhs)
0141 {
0142 Q_ASSERT(lhs.item.d == rhs.item.d);
0143 return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
0144 }
0145
0146 static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs,
0147 const const_iterator &rhs)
0148 {
0149 Q_ASSERT(lhs.item.d == rhs.item.d);
0150 return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
0151 }
0152
0153
0154 friend bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept
0155 {
0156 return comparesEqual_helper(lhs, rhs);
0157 }
0158 friend Qt::strong_ordering compareThreeWay(const iterator &lhs,
0159 const iterator &rhs)
0160 {
0161 return compareThreeWay_helper(lhs, rhs);
0162 }
0163 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator)
0164 friend bool comparesEqual(const iterator &lhs, const const_iterator &rhs) noexcept
0165 {
0166 return comparesEqual_helper(lhs, rhs);
0167 }
0168 friend Qt::strong_ordering compareThreeWay(const iterator &lhs,
0169 const const_iterator &rhs)
0170 {
0171 return compareThreeWay_helper(lhs, rhs);
0172 }
0173 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator, const_iterator)
0174
0175 QJsonValueRef item;
0176 friend class QJsonArray;
0177 };
0178 friend class iterator;
0179
0180 class const_iterator {
0181 public:
0182 typedef std::random_access_iterator_tag iterator_category;
0183 typedef qptrdiff difference_type;
0184 typedef QJsonValue value_type;
0185 typedef const QJsonValueRef reference;
0186 typedef const QJsonValueRef *pointer;
0187
0188 inline const_iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { }
0189 explicit inline const_iterator(const QJsonArray *array, qsizetype index)
0190 : item(const_cast<QJsonArray *>(array), index) { }
0191 inline const_iterator(const iterator &o) : item(o.item) { }
0192
0193 constexpr const_iterator(const const_iterator &other) = default;
0194 const_iterator &operator=(const const_iterator &other)
0195 {
0196 item.rebind(other.item);
0197 return *this;
0198 }
0199
0200 inline const QJsonValueConstRef operator*() const { return item; }
0201 inline const QJsonValueConstRef *operator->() const { return &item; }
0202
0203 inline QJsonValueConstRef operator[](qsizetype j) const { return *(*this + j); }
0204 #if QT_CORE_REMOVED_SINCE(6, 8)
0205 inline bool operator==(const const_iterator &o) const
0206 { return item.d == o.item.d && item.index == o.item.index; }
0207 inline bool operator!=(const const_iterator &o) const { return !operator==(o); }
0208 inline bool operator<(const const_iterator &other) const
0209 { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
0210 inline bool operator<=(const const_iterator &other) const
0211 { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
0212 inline bool operator>(const const_iterator &other) const { return !operator<=(other); }
0213 inline bool operator>=(const const_iterator &other) const { return !operator<(other); }
0214 #endif
0215 inline const_iterator &operator++() { ++item.index; return *this; }
0216 inline const_iterator operator++(int) { const_iterator n = *this; ++item.index; return n; }
0217 inline const_iterator &operator--() { item.index--; return *this; }
0218 inline const_iterator operator--(int) { const_iterator n = *this; item.index--; return n; }
0219 inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
0220 inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
0221 inline const_iterator operator+(qsizetype j) const { const_iterator r = *this; return r += j; }
0222 inline const_iterator operator-(qsizetype j) const { return operator+(-j); }
0223 inline qsizetype operator-(const_iterator j) const { return item.index - j.item.index; }
0224
0225 private:
0226
0227 static bool comparesEqual_helper(const const_iterator &lhs,
0228 const const_iterator &rhs) noexcept
0229 {
0230 return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
0231 }
0232 static Qt::strong_ordering compareThreeWay_helper(const const_iterator &lhs,
0233 const const_iterator &rhs)
0234 {
0235 Q_ASSERT(lhs.item.d == rhs.item.d);
0236 return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
0237 }
0238
0239
0240 friend bool comparesEqual(const const_iterator &lhs, const const_iterator &rhs) noexcept
0241 {
0242 return comparesEqual_helper(lhs, rhs);
0243 }
0244 friend Qt::strong_ordering compareThreeWay(const const_iterator &lhs,
0245 const const_iterator &rhs)
0246 {
0247 return compareThreeWay_helper(lhs, rhs);
0248 }
0249 Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(const_iterator)
0250 QJsonValueConstRef item;
0251 friend class QJsonArray;
0252 };
0253 friend class const_iterator;
0254
0255
0256 inline iterator begin() { detach(); return iterator(this, 0); }
0257 inline const_iterator begin() const { return const_iterator(this, 0); }
0258 inline const_iterator constBegin() const { return const_iterator(this, 0); }
0259 inline const_iterator cbegin() const { return const_iterator(this, 0); }
0260 inline iterator end() { detach(); return iterator(this, size()); }
0261 inline const_iterator end() const { return const_iterator(this, size()); }
0262 inline const_iterator constEnd() const { return const_iterator(this, size()); }
0263 inline const_iterator cend() const { return const_iterator(this, size()); }
0264 iterator insert(iterator before, const QJsonValue &value)
0265 { insert(before.item.index, value); return before; }
0266 iterator erase(iterator it)
0267 { removeAt(it.item.index); return it; }
0268
0269
0270 typedef iterator Iterator;
0271 typedef const_iterator ConstIterator;
0272
0273
0274 inline QJsonArray operator+(const QJsonValue &v) const
0275 { QJsonArray n = *this; n += v; return n; }
0276 inline QJsonArray &operator+=(const QJsonValue &v)
0277 { append(v); return *this; }
0278 inline QJsonArray &operator<< (const QJsonValue &v)
0279 { append(v); return *this; }
0280
0281
0282 inline void push_back(const QJsonValue &t) { append(t); }
0283 inline void push_front(const QJsonValue &t) { prepend(t); }
0284 inline void pop_front() { removeFirst(); }
0285 inline void pop_back() { removeLast(); }
0286 inline bool empty() const { return isEmpty(); }
0287 typedef qsizetype size_type;
0288 typedef QJsonValue value_type;
0289 typedef value_type *pointer;
0290 typedef const value_type *const_pointer;
0291 typedef QJsonValueRef reference;
0292 typedef QJsonValue const_reference;
0293 typedef qsizetype difference_type;
0294
0295 private:
0296 friend class QJsonValue;
0297 friend class QJsonValueConstRef;
0298 friend class QJsonValueRef;
0299 friend class QJsonPrivate::Value;
0300 friend class QJsonDocument;
0301 friend class QCborArray;
0302 friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
0303
0304 friend Q_CORE_EXPORT bool comparesEqual(const QJsonArray &lhs,
0305 const QJsonArray &rhs);
0306
0307 friend Q_CORE_EXPORT bool comparesEqual(const QJsonArray &lhs,
0308 const QJsonValue &rhs);
0309 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonArray)
0310 Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonArray, QJsonValue)
0311
0312 QJsonArray(QCborContainerPrivate *array);
0313 bool detach(qsizetype reserve = 0);
0314
0315 QExplicitlySharedDataPointer<QCborContainerPrivate> a;
0316 };
0317
0318 Q_DECLARE_SHARED(QJsonArray)
0319
0320 #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
0321 inline QJsonValueConstRef::QJsonValueConstRef(QJsonArray *a, qsizetype idx)
0322 : d(a ? a->a.data() : nullptr), is_object(false), index(idx)
0323 {}
0324 #endif
0325
0326 Q_CORE_EXPORT size_t qHash(const QJsonArray &array, size_t seed = 0);
0327
0328 #if !defined(QT_NO_DEBUG_STREAM)
0329 Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &);
0330 #endif
0331
0332 #ifndef QT_NO_DATASTREAM
0333 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonArray &);
0334 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonArray &);
0335 #endif
0336
0337 QT_END_NAMESPACE
0338
0339 #endif