Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-17 09:09:20

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 
0004 #ifndef QJSONOBJECT_H
0005 #define QJSONOBJECT_H
0006 
0007 #include <QtCore/qjsonvalue.h>
0008 #include <QtCore/qiterator.h>
0009 #include <QtCore/qpair.h>
0010 #include <QtCore/qshareddata.h>
0011 #include <initializer_list>
0012 
0013 QT_BEGIN_NAMESPACE
0014 
0015 class QDebug;
0016 
0017 class QCborContainerPrivate;
0018 
0019 class Q_CORE_EXPORT QJsonObject
0020 {
0021 public:
0022     QJsonObject();
0023 
0024     QJsonObject(std::initializer_list<std::pair<QString, QJsonValue> > args);
0025 
0026     ~QJsonObject();
0027 
0028     QJsonObject(const QJsonObject &other) noexcept;
0029     QJsonObject &operator =(const QJsonObject &other) noexcept;
0030 
0031     QJsonObject(QJsonObject &&other) noexcept;
0032 
0033     QJsonObject &operator =(QJsonObject &&other) noexcept
0034     {
0035         swap(other);
0036         return *this;
0037     }
0038 
0039     void swap(QJsonObject &other) noexcept
0040     {
0041         o.swap(other.o);
0042     }
0043 
0044     static QJsonObject fromVariantMap(const QVariantMap &map);
0045     QVariantMap toVariantMap() const;
0046     static QJsonObject fromVariantHash(const QVariantHash &map);
0047     QVariantHash toVariantHash() const;
0048 
0049     QStringList keys() const;
0050     qsizetype size() const;
0051     inline qsizetype count() const { return size(); }
0052     inline qsizetype length() const { return size(); }
0053     bool isEmpty() const;
0054 
0055     QJsonValue value(const QString &key) const;
0056     QJsonValue operator[] (const QString &key) const;
0057     QJsonValueRef operator[] (const QString &key);
0058     QJsonValue value(QStringView key) const;
0059     QJsonValue value(QLatin1StringView key) const;
0060     QJsonValue operator[] (QStringView key) const { return value(key); }
0061     QJsonValue operator[] (QLatin1StringView key) const { return value(key); }
0062     QJsonValueRef operator[] (QStringView key);
0063     QJsonValueRef operator[] (QLatin1StringView key);
0064 
0065     void remove(const QString &key);
0066     QJsonValue take(const QString &key);
0067     bool contains(const QString &key) const;
0068     void remove(QStringView key);
0069     void remove(QLatin1StringView key);
0070     QJsonValue take(QStringView key);
0071     QJsonValue take(QLatin1StringView key);
0072     bool contains(QStringView key) const;
0073     bool contains(QLatin1StringView key) const;
0074 
0075 #if QT_CORE_REMOVED_SINCE(6, 8)
0076     bool operator==(const QJsonObject &other) const;
0077     bool operator!=(const QJsonObject &other) const;
0078 #endif
0079     class const_iterator;
0080 
0081     class iterator
0082     {
0083         friend class const_iterator;
0084         friend class QJsonObject;
0085         QJsonValueRef item;
0086 
0087     public:
0088         typedef std::random_access_iterator_tag iterator_category;
0089         typedef qsizetype difference_type;
0090         typedef QJsonValue value_type;
0091         typedef QJsonValueRef reference;
0092         typedef QJsonValueRef *pointer;
0093 
0094         inline iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
0095         inline iterator(QJsonObject *obj, qsizetype index) : item(obj, index) { }
0096 
0097         constexpr iterator(const iterator &other) = default;
0098         iterator &operator=(const iterator &other)
0099         {
0100             item.rebind(other.item);
0101             return *this;
0102         }
0103 
0104         inline QString key() const { return item.objectKey(); }
0105         inline QJsonValueRef value() const { return item; }
0106         inline QJsonValueRef operator*() const { return item; }
0107         inline const QJsonValueConstRef *operator->() const { return &item; }
0108         inline QJsonValueRef *operator->() { return &item; }
0109         inline QJsonValueRef operator[](qsizetype j) const { return *(*this + j); }
0110 #if QT_CORE_REMOVED_SINCE(6, 8)
0111         inline bool operator==(const iterator &other) const
0112         { return item.d == other.item.d && item.index == other.item.index; }
0113         inline bool operator!=(const iterator &other) const { return !operator==(other); }
0114         bool operator<(const iterator& other) const
0115         { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
0116         bool operator<=(const iterator& other) const
0117         { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
0118         bool operator>(const iterator& other) const { return !operator<=(other); }
0119         bool operator>=(const iterator& other) const { return !operator<(other); }
0120 #endif
0121         inline iterator &operator++() { ++item.index; return *this; }
0122         inline iterator operator++(int) { iterator r = *this; ++item.index; return r; }
0123         inline iterator &operator--() { --item.index; return *this; }
0124         inline iterator operator--(int) { iterator r = *this; --item.index; return r; }
0125         inline iterator operator+(qsizetype j) const { iterator r = *this; return r += j; }
0126         inline iterator operator-(qsizetype j) const { return operator+(-j); }
0127         inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
0128         inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
0129         qsizetype operator-(iterator j) const { return item.index - j.item.index; }
0130 
0131     public:
0132 #if QT_CORE_REMOVED_SINCE(6, 8)
0133         inline bool operator==(const const_iterator &other) const
0134         { return item.d == other.item.d && item.index == other.item.index; }
0135         inline bool operator!=(const const_iterator &other) const { return !operator==(other); }
0136         bool operator<(const const_iterator& other) const
0137         { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
0138         bool operator<=(const const_iterator& other) const
0139         { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
0140         bool operator>(const const_iterator& other) const { return operator<=(other); }
0141         bool operator>=(const const_iterator& other) const { return operator<(other); }
0142 #endif
0143     private:
0144         // Helper functions
0145         static bool comparesEqual_helper(const iterator &lhs, const iterator &rhs) noexcept
0146         {
0147             return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
0148         }
0149         static bool comparesEqual_helper(const iterator &lhs, const const_iterator &rhs) noexcept
0150         {
0151             return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
0152         }
0153 
0154         static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs,
0155                                                           const iterator &rhs)
0156         {
0157             Q_ASSERT(lhs.item.d == rhs.item.d);
0158             return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
0159         }
0160         static Qt::strong_ordering compareThreeWay_helper(const iterator &lhs,
0161                                                           const const_iterator &rhs)
0162         {
0163             Q_ASSERT(lhs.item.d == rhs.item.d);
0164             return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
0165         }
0166 
0167         // Compare friends
0168         friend bool comparesEqual(const iterator &lhs, const iterator &rhs) noexcept
0169         {
0170             return comparesEqual_helper(lhs, rhs);
0171         }
0172         friend Qt::strong_ordering compareThreeWay(const iterator &lhs,
0173                                                    const iterator &rhs)
0174         {
0175             return compareThreeWay_helper(lhs, rhs);
0176         }
0177         Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator)
0178 
0179         friend bool comparesEqual(const iterator &lhs, const const_iterator &rhs) noexcept
0180         {
0181             return comparesEqual_helper(lhs, rhs);
0182         }
0183         friend Qt::strong_ordering compareThreeWay(const iterator &lhs,
0184                                                    const const_iterator &rhs)
0185         {
0186             return compareThreeWay_helper(lhs, rhs);
0187         }
0188         Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(iterator, const_iterator)
0189     };
0190     friend class iterator;
0191 
0192     class const_iterator
0193     {
0194         friend class iterator;
0195         QJsonValueConstRef item;
0196 
0197     public:
0198         typedef std::random_access_iterator_tag iterator_category;
0199         typedef qsizetype difference_type;
0200         typedef QJsonValue value_type;
0201         typedef const QJsonValueConstRef reference;
0202         typedef const QJsonValueConstRef *pointer;
0203 
0204         inline const_iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
0205         inline const_iterator(const QJsonObject *obj, qsizetype index)
0206             : item(const_cast<QJsonObject*>(obj), index) { }
0207         inline const_iterator(const iterator &other)
0208             : item(other.item) { }
0209 
0210         constexpr const_iterator(const const_iterator &other) = default;
0211         const_iterator &operator=(const const_iterator &other)
0212         {
0213             item.rebind(other.item);
0214             return *this;
0215         }
0216 
0217         inline QString key() const { return item.objectKey(); }
0218         inline QJsonValueConstRef value() const { return item; }
0219         inline const QJsonValueConstRef operator*() const { return item; }
0220         inline const QJsonValueConstRef *operator->() const { return &item; }
0221         inline QJsonValueConstRef operator[](qsizetype j) const { return *(*this + j); }
0222 #if QT_CORE_REMOVED_SINCE(6, 8)
0223         inline bool operator==(const const_iterator &other) const
0224         { return item.d == other.item.d && item.index == other.item.index; }
0225         inline bool operator!=(const const_iterator &other) const { return !operator==(other); }
0226         bool operator<(const const_iterator& other) const
0227         { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
0228         bool operator<=(const const_iterator& other) const
0229         { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
0230         bool operator>(const const_iterator& other) const { return !operator<=(other); }
0231         bool operator>=(const const_iterator& other) const { return !operator<(other); }
0232 #endif
0233         inline const_iterator &operator++() { ++item.index; return *this; }
0234         inline const_iterator operator++(int) { const_iterator r = *this; ++item.index; return r; }
0235         inline const_iterator &operator--() { --item.index; return *this; }
0236         inline const_iterator operator--(int) { const_iterator r = *this; --item.index; return r; }
0237         inline const_iterator operator+(qsizetype j) const { const_iterator r = *this; return r += j; }
0238         inline const_iterator operator-(qsizetype j) const { return operator+(-j); }
0239         inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; }
0240         inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; }
0241         qsizetype operator-(const_iterator j) const { return item.index - j.item.index; }
0242 #if QT_CORE_REMOVED_SINCE(6, 8)
0243         inline bool operator==(const iterator &other) const
0244         { return item.d == other.item.d && item.index == other.item.index; }
0245         inline bool operator!=(const iterator &other) const { return !operator==(other); }
0246         bool operator<(const iterator& other) const
0247         { Q_ASSERT(item.d == other.item.d); return item.index < other.item.index; }
0248         bool operator<=(const iterator& other) const
0249         { Q_ASSERT(item.d == other.item.d); return item.index <= other.item.index; }
0250         bool operator>(const iterator& other) const { return !operator<=(other); }
0251         bool operator>=(const iterator& other) const { return !operator<(other); }
0252 #endif
0253 
0254     private:
0255         // Helper functions
0256         static bool comparesEqual_helper(const const_iterator &lhs,
0257                                          const const_iterator &rhs) noexcept
0258         {
0259             return lhs.item.d == rhs.item.d && lhs.item.index == rhs.item.index;
0260         }
0261         static Qt::strong_ordering compareThreeWay_helper(const const_iterator &lhs,
0262                                                           const const_iterator &rhs)
0263         {
0264             Q_ASSERT(lhs.item.d == rhs.item.d);
0265             return Qt::compareThreeWay(lhs.item.index, rhs.item.index);
0266         }
0267 
0268         // Compare friends
0269         friend bool comparesEqual(const const_iterator &lhs, const const_iterator &rhs) noexcept
0270         {
0271             return comparesEqual_helper(lhs, rhs);
0272         }
0273         friend Qt::strong_ordering compareThreeWay(const const_iterator &lhs,
0274                                                    const const_iterator &rhs)
0275         {
0276             return compareThreeWay_helper(lhs, rhs);
0277         }
0278         Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(const_iterator)
0279     };
0280     friend class const_iterator;
0281 
0282     // STL style
0283     inline iterator begin() { detach(); return iterator(this, 0); }
0284     inline const_iterator begin() const { return const_iterator(this, 0); }
0285     inline const_iterator constBegin() const { return const_iterator(this, 0); }
0286     inline iterator end() { detach(); return iterator(this, size()); }
0287     inline const_iterator end() const { return const_iterator(this, size()); }
0288     inline const_iterator constEnd() const { return const_iterator(this, size()); }
0289     iterator erase(iterator it);
0290 
0291     // more Qt
0292     typedef iterator Iterator;
0293     typedef const_iterator ConstIterator;
0294     iterator find(const QString &key);
0295     const_iterator find(const QString &key) const { return constFind(key); }
0296     const_iterator constFind(const QString &key) const;
0297     iterator insert(const QString &key, const QJsonValue &value);
0298     iterator find(QStringView key);
0299     iterator find(QLatin1StringView key);
0300     const_iterator find(QStringView key) const { return constFind(key); }
0301     const_iterator find(QLatin1StringView key) const { return constFind(key); }
0302     const_iterator constFind(QStringView key) const;
0303     const_iterator constFind(QLatin1StringView key) const;
0304     iterator insert(QStringView key, const QJsonValue &value);
0305     iterator insert(QLatin1StringView key, const QJsonValue &value);
0306 
0307     // STL compatibility
0308     typedef QJsonValue mapped_type;
0309     typedef QString key_type;
0310     typedef qsizetype size_type;
0311 
0312     inline bool empty() const { return isEmpty(); }
0313 
0314 private:
0315     friend Q_CORE_EXPORT bool comparesEqual(const QJsonObject &lhs,
0316                                             const QJsonObject &rhs);
0317     friend bool comparesEqual(const QJsonObject &lhs, const QJsonValue &rhs)
0318     {
0319         return comparesEqual(lhs, rhs.toObject());
0320     }
0321     friend bool comparesEqual(const QJsonObject &lhs, const QJsonValueConstRef &rhs)
0322     {
0323         return comparesEqual(lhs, rhs.toObject());
0324     }
0325     Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject)
0326     Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject, QJsonValue)
0327     Q_DECLARE_EQUALITY_COMPARABLE_NON_NOEXCEPT(QJsonObject, QJsonValueConstRef)
0328     friend class QJsonValue;
0329     friend class QJsonDocument;
0330     friend class QJsonPrivate::Value;
0331     friend class QJsonValueConstRef;
0332     friend class QJsonValueRef;
0333     friend class QCborMap;
0334     friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
0335 
0336     QJsonObject(QCborContainerPrivate *object);
0337     bool detach(qsizetype reserve = 0);
0338 
0339     template <typename T> QJsonValue valueImpl(T key) const;
0340     template <typename T> QJsonValueRef atImpl(T key);
0341     template <typename T> void removeImpl(T key);
0342     template <typename T> QJsonValue takeImpl(T key);
0343     template <typename T> bool containsImpl(T key) const;
0344     template <typename T> iterator findImpl(T key);
0345     template <typename T> const_iterator constFindImpl(T key) const;
0346     template <typename T> iterator insertImpl(T key, const QJsonValue &value);
0347 
0348 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
0349     QString keyAt(qsizetype i) const;
0350     QJsonValue valueAt(qsizetype i) const;
0351     void setValueAt(qsizetype i, const QJsonValue &val);
0352 #endif
0353     void removeAt(qsizetype i);
0354     template <typename T> iterator insertAt(qsizetype i, T key, const QJsonValue &val, bool exists);
0355 
0356     QExplicitlySharedDataPointer<QCborContainerPrivate> o;
0357 };
0358 
0359 Q_DECLARE_SHARED(QJsonObject)
0360 
0361 #if QT_VERSION >= QT_VERSION_CHECK(7, 0, 0) || defined(QT_BOOTSTRAPPED)
0362 inline QJsonValueConstRef::QJsonValueConstRef(QJsonObject *o, qsizetype idx)
0363     : d(o ? o->o.data() : nullptr), is_object(true), index(idx)
0364 {}
0365 #endif
0366 
0367 Q_CORE_EXPORT size_t qHash(const QJsonObject &object, size_t seed = 0);
0368 
0369 #if !defined(QT_NO_DEBUG_STREAM)
0370 Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonObject &);
0371 #endif
0372 
0373 #ifndef QT_NO_DATASTREAM
0374 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonObject &);
0375 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonObject &);
0376 #endif
0377 
0378 QT_END_NAMESPACE
0379 
0380 #endif // QJSONOBJECT_H