Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/QtCore/qcborarray.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // Copyright (C) 2022 Intel Corporation.
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 QCBORARRAY_H
0005 #define QCBORARRAY_H
0006 
0007 #include <QtCore/qcborvalue.h>
0008 
0009 #include <initializer_list>
0010 
0011 QT_BEGIN_NAMESPACE
0012 
0013 class QJsonArray;
0014 class QDataStream;
0015 
0016 namespace QJsonPrivate { class Variant; }
0017 
0018 class QCborContainerPrivate;
0019 class Q_CORE_EXPORT QCborArray
0020 {
0021 public:
0022     class ConstIterator;
0023     class Iterator {
0024         QCborValueRef item {};
0025         friend class ConstIterator;
0026         friend class QCborArray;
0027         Iterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {}
0028     public:
0029         typedef std::random_access_iterator_tag iterator_category;
0030         typedef qsizetype difference_type;
0031         typedef QCborValue value_type;
0032         typedef QCborValueRef reference;
0033         typedef QCborValueRef *pointer;
0034 
0035         constexpr Iterator() = default;
0036         constexpr Iterator(const Iterator &) = default;
0037         Iterator &operator=(const Iterator &other)
0038         {
0039             // rebind the reference
0040             item.d = other.item.d;
0041             item.i = other.item.i;
0042             return *this;
0043         }
0044 
0045         QCborValueRef operator*() const { return item; }
0046         QCborValueRef *operator->() { return &item; }
0047         const QCborValueConstRef *operator->() const { return &item; }
0048         QCborValueRef operator[](qsizetype j) const { return { item.d, item.i + j }; }
0049 
0050         bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
0051         bool operator!=(const Iterator &o) const { return !(*this == o); }
0052         bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
0053         bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
0054         bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
0055         bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
0056         bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
0057         bool operator!=(const ConstIterator &o) const { return !(*this == o); }
0058         bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
0059         bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
0060         bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
0061         bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
0062         Iterator &operator++() { ++item.i; return *this; }
0063         Iterator operator++(int) { Iterator n = *this; ++item.i; return n; }
0064         Iterator &operator--() { item.i--; return *this; }
0065         Iterator operator--(int) { Iterator n = *this; item.i--; return n; }
0066         Iterator &operator+=(qsizetype j) { item.i += j; return *this; }
0067         Iterator &operator-=(qsizetype j) { item.i -= j; return *this; }
0068         Iterator operator+(qsizetype j) const { return Iterator({ item.d, item.i + j }); }
0069         Iterator operator-(qsizetype j) const { return Iterator({ item.d, item.i - j }); }
0070         qsizetype operator-(Iterator j) const { return item.i - j.item.i; }
0071     };
0072 
0073     class ConstIterator {
0074         QCborValueConstRef item;
0075         friend class Iterator;
0076         friend class QCborArray;
0077         ConstIterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {}
0078     public:
0079         typedef std::random_access_iterator_tag iterator_category;
0080         typedef qsizetype difference_type;
0081         typedef const QCborValue value_type;
0082         typedef const QCborValueRef reference;
0083         typedef const QCborValueRef *pointer;
0084 
0085         constexpr ConstIterator() = default;
0086         constexpr ConstIterator(const ConstIterator &) = default;
0087         ConstIterator &operator=(const ConstIterator &other)
0088         {
0089             // rebind the reference
0090             item.d = other.item.d;
0091             item.i = other.item.i;
0092             return *this;
0093         }
0094 
0095         QCborValueConstRef operator*() const { return item; }
0096         const QCborValueConstRef *operator->() const { return &item; }
0097         QCborValueConstRef operator[](qsizetype j) const { return QCborValueRef{ item.d, item.i + j }; }
0098 
0099         bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
0100         bool operator!=(const Iterator &o) const { return !(*this == o); }
0101         bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
0102         bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
0103         bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
0104         bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
0105         bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
0106         bool operator!=(const ConstIterator &o) const { return !(*this == o); }
0107         bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
0108         bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
0109         bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
0110         bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
0111         ConstIterator &operator++() { ++item.i; return *this; }
0112         ConstIterator operator++(int) { ConstIterator n = *this; ++item.i; return n; }
0113         ConstIterator &operator--() { item.i--; return *this; }
0114         ConstIterator operator--(int) { ConstIterator n = *this; item.i--; return n; }
0115         ConstIterator &operator+=(qsizetype j) { item.i += j; return *this; }
0116         ConstIterator &operator-=(qsizetype j) { item.i -= j; return *this; }
0117         ConstIterator operator+(qsizetype j) const { return ConstIterator({ item.d, item.i + j }); }
0118         ConstIterator operator-(qsizetype j) const { return ConstIterator({ item.d, item.i - j }); }
0119         qsizetype operator-(ConstIterator j) const { return item.i - j.item.i; }
0120     };
0121 
0122     typedef qsizetype size_type;
0123     typedef QCborValue value_type;
0124     typedef value_type *pointer;
0125     typedef const value_type *const_pointer;
0126     typedef QCborValue &reference;
0127     typedef const QCborValue &const_reference;
0128     typedef qsizetype difference_type;
0129 
0130     QCborArray() noexcept;
0131     QCborArray(const QCborArray &other) noexcept;
0132     QCborArray &operator=(const QCborArray &other) noexcept;
0133     QCborArray(std::initializer_list<QCborValue> args)
0134         : QCborArray()
0135     {
0136         detach(qsizetype(args.size()));
0137         for (const QCborValue &v : args)
0138             append(v);
0139     }
0140     ~QCborArray();
0141 
0142     void swap(QCborArray &other) noexcept
0143     {
0144         d.swap(other.d);
0145     }
0146 
0147     QCborValue toCborValue() const { return *this; }
0148 
0149     qsizetype size() const noexcept;
0150     bool isEmpty() const { return size() == 0; }
0151     void clear();
0152 
0153     QCborValue at(qsizetype i) const;
0154     QCborValue first() const { return at(0); }
0155     QCborValue last() const { return at(size() - 1); }
0156     const QCborValue operator[](qsizetype i) const { return at(i); }
0157     QCborValueRef first() { Q_ASSERT(!isEmpty()); return begin()[0]; }
0158     QCborValueRef last() { Q_ASSERT(!isEmpty()); return begin()[size() - 1]; }
0159     QCborValueRef operator[](qsizetype i)
0160     {
0161         if (i >= size())
0162             insert(i, QCborValue());
0163         return begin()[i];
0164     }
0165 
0166     void insert(qsizetype i, const QCborValue &value);
0167     void insert(qsizetype i, QCborValue &&value);
0168     void prepend(const QCborValue &value) { insert(0, value); }
0169     void prepend(QCborValue &&value) { insert(0, std::move(value)); }
0170     void append(const QCborValue &value) { insert(-1, value); }
0171     void append(QCborValue &&value) { insert(-1, std::move(value)); }
0172     QCborValue extract(ConstIterator it) { return extract(Iterator{ it.item.d, it.item.i }); }
0173     QCborValue extract(Iterator it);
0174     void removeAt(qsizetype i);
0175     QCborValue takeAt(qsizetype i) { Q_ASSERT(i < size()); return extract(begin() + i); }
0176     void removeFirst() { removeAt(0); }
0177     void removeLast() { removeAt(size() - 1); }
0178     QCborValue takeFirst() { return takeAt(0); }
0179     QCborValue takeLast() { return takeAt(size() - 1); }
0180 
0181     bool contains(const QCborValue &value) const;
0182 
0183     int compare(const QCborArray &other) const noexcept Q_DECL_PURE_FUNCTION;
0184 #if 0 && __has_include(<compare>)
0185     std::strong_ordering operator<=>(const QCborArray &other) const
0186     {
0187         int c = compare(other);
0188         if (c > 0) return std::strong_ordering::greater;
0189         if (c == 0) return std::strong_ordering::equivalent;
0190         return std::strong_ordering::less;
0191     }
0192 #else
0193     bool operator==(const QCborArray &other) const noexcept
0194     { return compare(other) == 0; }
0195     bool operator!=(const QCborArray &other) const noexcept
0196     { return !(*this == other); }
0197     bool operator<(const QCborArray &other) const
0198     { return compare(other) < 0; }
0199 #endif
0200 
0201     typedef Iterator iterator;
0202     typedef ConstIterator const_iterator;
0203     iterator begin() { detach(); return iterator{d.data(), 0}; }
0204     const_iterator constBegin() const { return const_iterator{d.data(), 0}; }
0205     const_iterator begin() const { return constBegin(); }
0206     const_iterator cbegin() const { return constBegin(); }
0207     iterator end() { detach(); return iterator{d.data(), size()}; }
0208     const_iterator constEnd() const { return const_iterator{d.data(), size()}; }
0209     const_iterator end() const { return constEnd(); }
0210     const_iterator cend() const { return constEnd(); }
0211     iterator insert(iterator before, const QCborValue &value)
0212     { insert(before.item.i, value); return iterator{d.data(), before.item.i}; }
0213     iterator insert(const_iterator before, const QCborValue &value)
0214     { insert(before.item.i, value); return iterator{d.data(), before.item.i}; }
0215     iterator erase(iterator it) { removeAt(it.item.i); return iterator{d.data(), it.item.i}; }
0216     iterator erase(const_iterator it) { removeAt(it.item.i); return iterator{d.data(), it.item.i}; }
0217 
0218     void push_back(const QCborValue &t) { append(t); }
0219     void push_front(const QCborValue &t) { prepend(t); }
0220     void pop_front() { removeFirst(); }
0221     void pop_back() { removeLast(); }
0222     bool empty() const { return isEmpty(); }
0223 
0224     // convenience
0225     QCborArray operator+(const QCborValue &v) const
0226     { QCborArray n = *this; n += v; return n; }
0227     QCborArray &operator+=(const QCborValue &v)
0228     { append(v); return *this; }
0229     QCborArray &operator<<(const QCborValue &v)
0230     { append(v); return *this; }
0231 
0232     static QCborArray fromStringList(const QStringList &list);
0233     static QCborArray fromVariantList(const QVariantList &list);
0234     static QCborArray fromJsonArray(const QJsonArray &array);
0235     static QCborArray fromJsonArray(QJsonArray &&array) noexcept;
0236     QVariantList toVariantList() const;
0237     QJsonArray toJsonArray() const;
0238 
0239 private:
0240     void detach(qsizetype reserve = 0);
0241 
0242     friend QCborValue;
0243     friend QCborValueRef;
0244     friend class QJsonPrivate::Variant;
0245     explicit QCborArray(QCborContainerPrivate &dd) noexcept;
0246     QExplicitlySharedDataPointer<QCborContainerPrivate> d;
0247 };
0248 
0249 Q_DECLARE_SHARED(QCborArray)
0250 
0251 inline QCborValue::QCborValue(QCborArray &&a)
0252     : n(-1), container(a.d.take()), t(Array)
0253 {
0254 }
0255 
0256 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
0257 inline QCborArray QCborValueRef::toArray() const
0258 {
0259     return concrete().toArray();
0260 }
0261 
0262 inline QCborArray QCborValueRef::toArray(const QCborArray &a) const
0263 {
0264     return concrete().toArray(a);
0265 }
0266 #endif
0267 
0268 inline QCborArray QCborValueConstRef::toArray() const
0269 {
0270     return concrete().toArray();
0271 }
0272 
0273 inline QCborArray QCborValueConstRef::toArray(const QCborArray &a) const
0274 {
0275     return concrete().toArray(a);
0276 }
0277 
0278 Q_CORE_EXPORT size_t qHash(const QCborArray &array, size_t seed = 0);
0279 
0280 #if !defined(QT_NO_DEBUG_STREAM)
0281 Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborArray &a);
0282 #endif
0283 
0284 #ifndef QT_NO_DATASTREAM
0285 #if QT_CONFIG(cborstreamwriter)
0286 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborArray &);
0287 #endif
0288 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborArray &);
0289 #endif
0290 
0291 QT_END_NAMESPACE
0292 
0293 #endif // QCBORARRAY_H