Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-18 08:32:28

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