| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtCore module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QJSONARRAY_H |
| 41 | #define QJSONARRAY_H |
| 42 | |
| 43 | #include <QtCore/qjsonvalue.h> |
| 44 | #include <QtCore/qiterator.h> |
| 45 | #include <QtCore/qshareddata.h> |
| 46 | #include <initializer_list> |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE |
| 49 | |
| 50 | class QDebug; |
| 51 | typedef QList<QVariant> QVariantList; |
| 52 | |
| 53 | class Q_CORE_EXPORT QJsonArray |
| 54 | { |
| 55 | public: |
| 56 | QJsonArray(); |
| 57 | |
| 58 | QJsonArray(std::initializer_list<QJsonValue> args); |
| 59 | |
| 60 | ~QJsonArray(); |
| 61 | |
| 62 | QJsonArray(const QJsonArray &other); |
| 63 | QJsonArray &operator =(const QJsonArray &other); |
| 64 | |
| 65 | QJsonArray(QJsonArray &&other) noexcept; |
| 66 | |
| 67 | QJsonArray &operator =(QJsonArray &&other) noexcept |
| 68 | { |
| 69 | swap(other); |
| 70 | return *this; |
| 71 | } |
| 72 | |
| 73 | static QJsonArray fromStringList(const QStringList &list); |
| 74 | static QJsonArray fromVariantList(const QVariantList &list); |
| 75 | QVariantList toVariantList() const; |
| 76 | |
| 77 | qsizetype size() const; |
| 78 | inline qsizetype count() const { return size(); } |
| 79 | |
| 80 | bool isEmpty() const; |
| 81 | QJsonValue at(qsizetype i) const; |
| 82 | QJsonValue first() const; |
| 83 | QJsonValue last() const; |
| 84 | |
| 85 | void prepend(const QJsonValue &value); |
| 86 | void append(const QJsonValue &value); |
| 87 | void removeAt(qsizetype i); |
| 88 | QJsonValue takeAt(qsizetype i); |
| 89 | inline void removeFirst() { removeAt(0); } |
| 90 | inline void removeLast() { removeAt(size() - 1); } |
| 91 | |
| 92 | void insert(qsizetype i, const QJsonValue &value); |
| 93 | void replace(qsizetype i, const QJsonValue &value); |
| 94 | |
| 95 | bool contains(const QJsonValue &element) const; |
| 96 | QJsonValueRef operator[](qsizetype i); |
| 97 | QJsonValue operator[](qsizetype i) const; |
| 98 | |
| 99 | bool operator==(const QJsonArray &other) const; |
| 100 | bool operator!=(const QJsonArray &other) const; |
| 101 | |
| 102 | void swap(QJsonArray &other) noexcept |
| 103 | { |
| 104 | qSwap(a, other.a); |
| 105 | } |
| 106 | |
| 107 | class const_iterator; |
| 108 | |
| 109 | class iterator { |
| 110 | public: |
| 111 | typedef std::random_access_iterator_tag iterator_category; |
| 112 | typedef qsizetype difference_type; |
| 113 | typedef QJsonValue value_type; |
| 114 | typedef QJsonValueRef reference; |
| 115 | typedef QJsonValueRef *pointer; |
| 116 | |
| 117 | inline iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { } |
| 118 | explicit inline iterator(QJsonArray *array, qsizetype index) : item(array, index) { } |
| 119 | |
| 120 | constexpr iterator(const iterator &other) = default; |
| 121 | iterator &operator=(const iterator &other) |
| 122 | { |
| 123 | item.a = other.item.a; |
| 124 | item.index = other.item.index; |
| 125 | return *this; |
| 126 | } |
| 127 | |
| 128 | inline QJsonValueRef operator*() const { return item; } |
| 129 | inline QJsonValueRef *operator->() const { return &item; } |
| 130 | inline QJsonValueRef operator[](qsizetype j) const |
| 131 | { return { item.a, qsizetype(item.index) + j }; } |
| 132 | |
| 133 | inline bool operator==(const iterator &o) const |
| 134 | { return item.a == o.item.a && item.index == o.item.index; } |
| 135 | inline bool operator!=(const iterator &o) const { return !(*this == o); } |
| 136 | inline bool operator<(const iterator &other) const |
| 137 | { Q_ASSERT(item.a == other.item.a); return item.index < other.item.index; } |
| 138 | inline bool operator<=(const iterator &other) const |
| 139 | { Q_ASSERT(item.a == other.item.a); return item.index <= other.item.index; } |
| 140 | inline bool operator>(const iterator &other) const { return !(*this <= other); } |
| 141 | inline bool operator>=(const iterator &other) const { return !(*this < other); } |
| 142 | inline bool operator==(const const_iterator &o) const |
| 143 | { return item.a == o.item.a && item.index == o.item.index; } |
| 144 | inline bool operator!=(const const_iterator &o) const { return !(*this == o); } |
| 145 | inline bool operator<(const const_iterator &other) const |
| 146 | { Q_ASSERT(item.a == other.item.a); return item.index < other.item.index; } |
| 147 | inline bool operator<=(const const_iterator &other) const |
| 148 | { Q_ASSERT(item.a == other.item.a); return item.index <= other.item.index; } |
| 149 | inline bool operator>(const const_iterator &other) const { return !(*this <= other); } |
| 150 | inline bool operator>=(const const_iterator &other) const { return !(*this < other); } |
| 151 | inline iterator &operator++() { ++item.index; return *this; } |
| 152 | inline iterator operator++(int) { iterator n = *this; ++item.index; return n; } |
| 153 | inline iterator &operator--() { item.index--; return *this; } |
| 154 | inline iterator operator--(int) { iterator n = *this; item.index--; return n; } |
| 155 | inline iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; } |
| 156 | inline iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; } |
| 157 | inline iterator operator+(qsizetype j) const |
| 158 | { return iterator(item.a, qsizetype(item.index) + j); } |
| 159 | inline iterator operator-(qsizetype j) const |
| 160 | { return iterator(item.a, qsizetype(item.index) - j); } |
| 161 | inline qsizetype operator-(iterator j) const { return item.index - j.item.index; } |
| 162 | |
| 163 | private: |
| 164 | mutable QJsonValueRef item; |
| 165 | friend class QJsonArray; |
| 166 | }; |
| 167 | friend class iterator; |
| 168 | |
| 169 | class const_iterator { |
| 170 | public: |
| 171 | typedef std::random_access_iterator_tag iterator_category; |
| 172 | typedef qptrdiff difference_type; |
| 173 | typedef QJsonValue value_type; |
| 174 | typedef const QJsonValueRef reference; |
| 175 | typedef const QJsonValueRef *pointer; |
| 176 | |
| 177 | inline const_iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { } |
| 178 | explicit inline const_iterator(const QJsonArray *array, qsizetype index) |
| 179 | : item(const_cast<QJsonArray *>(array), index) { } |
| 180 | inline const_iterator(const iterator &o) : item(o.item) { } |
| 181 | |
| 182 | constexpr const_iterator(const const_iterator &other) = default; |
| 183 | const_iterator &operator=(const const_iterator &other) |
| 184 | { |
| 185 | item.a = other.item.a; |
| 186 | item.index = other.item.index; |
| 187 | return *this; |
| 188 | } |
| 189 | |
| 190 | inline const QJsonValueRef operator*() const { return item; } |
| 191 | inline const QJsonValueRef *operator->() const { return &item; } |
| 192 | |
| 193 | inline QJsonValueRef operator[](qsizetype j) const |
| 194 | { return { item.a, qsizetype(item.index) + j }; } |
| 195 | inline bool operator==(const const_iterator &o) const |
| 196 | { return item.a == o.item.a && item.index == o.item.index; } |
| 197 | inline bool operator!=(const const_iterator &o) const { return !(*this == o); } |
| 198 | inline bool operator<(const const_iterator &other) const |
| 199 | { Q_ASSERT(item.a == other.item.a); return item.index < other.item.index; } |
| 200 | inline bool operator<=(const const_iterator &other) const |
| 201 | { Q_ASSERT(item.a == other.item.a); return item.index <= other.item.index; } |
| 202 | inline bool operator>(const const_iterator &other) const { return !(*this <= other); } |
| 203 | inline bool operator>=(const const_iterator &other) const { return !(*this < other); } |
| 204 | inline const_iterator &operator++() { ++item.index; return *this; } |
| 205 | inline const_iterator operator++(int) { const_iterator n = *this; ++item.index; return n; } |
| 206 | inline const_iterator &operator--() { item.index--; return *this; } |
| 207 | inline const_iterator operator--(int) { const_iterator n = *this; item.index--; return n; } |
| 208 | inline const_iterator &operator+=(qsizetype j) { item.index += quint64(j); return *this; } |
| 209 | inline const_iterator &operator-=(qsizetype j) { item.index -= quint64(j); return *this; } |
| 210 | inline const_iterator operator+(qsizetype j) const |
| 211 | { return const_iterator(item.a, qsizetype(item.index) + j); } |
| 212 | inline const_iterator operator-(qsizetype j) const |
| 213 | { return const_iterator(item.a, qsizetype(item.index) - j); } |
| 214 | inline qsizetype operator-(const_iterator j) const { return item.index - j.item.index; } |
| 215 | |
| 216 | private: |
| 217 | QJsonValueRef item; |
| 218 | friend class QJsonArray; |
| 219 | }; |
| 220 | friend class const_iterator; |
| 221 | |
| 222 | // stl style |
| 223 | inline iterator begin() { detach(); return iterator(this, 0); } |
| 224 | inline const_iterator begin() const { return const_iterator(this, 0); } |
| 225 | inline const_iterator constBegin() const { return const_iterator(this, 0); } |
| 226 | inline const_iterator cbegin() const { return const_iterator(this, 0); } |
| 227 | inline iterator end() { detach(); return iterator(this, size()); } |
| 228 | inline const_iterator end() const { return const_iterator(this, size()); } |
| 229 | inline const_iterator constEnd() const { return const_iterator(this, size()); } |
| 230 | inline const_iterator cend() const { return const_iterator(this, size()); } |
| 231 | iterator insert(iterator before, const QJsonValue &value) |
| 232 | { insert(before.item.index, value); return before; } |
| 233 | iterator erase(iterator it) |
| 234 | { removeAt(it.item.index); return it; } |
| 235 | |
| 236 | // more Qt |
| 237 | typedef iterator Iterator; |
| 238 | typedef const_iterator ConstIterator; |
| 239 | |
| 240 | // convenience |
| 241 | inline QJsonArray operator+(const QJsonValue &v) const |
| 242 | { QJsonArray n = *this; n += v; return n; } |
| 243 | inline QJsonArray &operator+=(const QJsonValue &v) |
| 244 | { append(v); return *this; } |
| 245 | inline QJsonArray &operator<< (const QJsonValue &v) |
| 246 | { append(v); return *this; } |
| 247 | |
| 248 | // stl compatibility |
| 249 | inline void push_back(const QJsonValue &t) { append(t); } |
| 250 | inline void push_front(const QJsonValue &t) { prepend(t); } |
| 251 | inline void pop_front() { removeFirst(); } |
| 252 | inline void pop_back() { removeLast(); } |
| 253 | inline bool empty() const { return isEmpty(); } |
| 254 | typedef qsizetype size_type; |
| 255 | typedef QJsonValue value_type; |
| 256 | typedef value_type *pointer; |
| 257 | typedef const value_type *const_pointer; |
| 258 | typedef QJsonValueRef reference; |
| 259 | typedef QJsonValue const_reference; |
| 260 | typedef qsizetype difference_type; |
| 261 | |
| 262 | private: |
| 263 | friend class QJsonValue; |
| 264 | friend class QJsonDocument; |
| 265 | friend class QCborArray; |
| 266 | friend Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &); |
| 267 | |
| 268 | QJsonArray(QCborContainerPrivate *array); |
| 269 | bool detach(qsizetype reserve = 0); |
| 270 | |
| 271 | QExplicitlySharedDataPointer<QCborContainerPrivate> a; |
| 272 | }; |
| 273 | |
| 274 | Q_DECLARE_SHARED(QJsonArray) |
| 275 | |
| 276 | Q_CORE_EXPORT size_t qHash(const QJsonArray &array, size_t seed = 0); |
| 277 | |
| 278 | #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) |
| 279 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonArray &); |
| 280 | #endif |
| 281 | |
| 282 | #ifndef QT_NO_DATASTREAM |
| 283 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QJsonArray &); |
| 284 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QJsonArray &); |
| 285 | #endif |
| 286 | |
| 287 | QT_END_NAMESPACE |
| 288 | |
| 289 | #endif // QJSONARRAY_H |
| 290 | |