| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2018 Intel Corporation. |
| 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 QCBORARRAY_H |
| 41 | #define QCBORARRAY_H |
| 42 | |
| 43 | #include <QtCore/qcborvalue.h> |
| 44 | |
| 45 | #include <initializer_list> |
| 46 | |
| 47 | QT_BEGIN_NAMESPACE |
| 48 | |
| 49 | class QJsonArray; |
| 50 | class QDataStream; |
| 51 | |
| 52 | namespace QJsonPrivate { class Variant; } |
| 53 | |
| 54 | class QCborContainerPrivate; |
| 55 | class Q_CORE_EXPORT QCborArray |
| 56 | { |
| 57 | public: |
| 58 | class ConstIterator; |
| 59 | class Iterator { |
| 60 | mutable QCborValueRef item; |
| 61 | friend class ConstIterator; |
| 62 | friend class QCborArray; |
| 63 | Iterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {} |
| 64 | public: |
| 65 | typedef std::random_access_iterator_tag iterator_category; |
| 66 | typedef qsizetype difference_type; |
| 67 | typedef QCborValue value_type; |
| 68 | typedef QCborValueRef reference; |
| 69 | typedef QCborValueRef *pointer; |
| 70 | |
| 71 | Q_DECL_CONSTEXPR Iterator() = default; |
| 72 | Q_DECL_CONSTEXPR Iterator(const Iterator &) = default; |
| 73 | Iterator &operator=(const Iterator &other) |
| 74 | { |
| 75 | // rebind the reference |
| 76 | item.d = other.item.d; |
| 77 | item.i = other.item.i; |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | QCborValueRef operator*() const { return item; } |
| 82 | QCborValueRef *operator->() const { return &item; } |
| 83 | QCborValueRef operator[](qsizetype j) { return { item.d, item.i + j }; } |
| 84 | |
| 85 | bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 86 | bool operator!=(const Iterator &o) const { return !(*this == o); } |
| 87 | bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 88 | bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 89 | bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 90 | bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 91 | bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 92 | bool operator!=(const ConstIterator &o) const { return !(*this == o); } |
| 93 | bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 94 | bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 95 | bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 96 | bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 97 | Iterator &operator++() { ++item.i; return *this; } |
| 98 | Iterator operator++(int) { Iterator n = *this; ++item.i; return n; } |
| 99 | Iterator &operator--() { item.i--; return *this; } |
| 100 | Iterator operator--(int) { Iterator n = *this; item.i--; return n; } |
| 101 | Iterator &operator+=(qsizetype j) { item.i += j; return *this; } |
| 102 | Iterator &operator-=(qsizetype j) { item.i -= j; return *this; } |
| 103 | Iterator operator+(qsizetype j) const { return Iterator({ item.d, item.i + j }); } |
| 104 | Iterator operator-(qsizetype j) const { return Iterator({ item.d, item.i - j }); } |
| 105 | qsizetype operator-(Iterator j) const { return item.i - j.item.i; } |
| 106 | }; |
| 107 | |
| 108 | class ConstIterator { |
| 109 | QCborValueRef item; |
| 110 | friend class Iterator; |
| 111 | friend class QCborArray; |
| 112 | ConstIterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {} |
| 113 | public: |
| 114 | typedef std::random_access_iterator_tag iterator_category; |
| 115 | typedef qsizetype difference_type; |
| 116 | typedef const QCborValue value_type; |
| 117 | typedef const QCborValueRef reference; |
| 118 | typedef const QCborValueRef *pointer; |
| 119 | |
| 120 | Q_DECL_CONSTEXPR ConstIterator() = default; |
| 121 | Q_DECL_CONSTEXPR ConstIterator(const ConstIterator &) = default; |
| 122 | ConstIterator &operator=(const ConstIterator &other) |
| 123 | { |
| 124 | // rebind the reference |
| 125 | item.d = other.item.d; |
| 126 | item.i = other.item.i; |
| 127 | return *this; |
| 128 | } |
| 129 | |
| 130 | const QCborValueRef operator*() const { return item; } |
| 131 | const QCborValueRef *operator->() const { return &item; } |
| 132 | const QCborValueRef operator[](qsizetype j) { return { item.d, item.i + j }; } |
| 133 | |
| 134 | bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 135 | bool operator!=(const Iterator &o) const { return !(*this == o); } |
| 136 | bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 137 | bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 138 | bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 139 | bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 140 | bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 141 | bool operator!=(const ConstIterator &o) const { return !(*this == o); } |
| 142 | bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 143 | bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 144 | bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 145 | bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 146 | ConstIterator &operator++() { ++item.i; return *this; } |
| 147 | ConstIterator operator++(int) { ConstIterator n = *this; ++item.i; return n; } |
| 148 | ConstIterator &operator--() { item.i--; return *this; } |
| 149 | ConstIterator operator--(int) { ConstIterator n = *this; item.i--; return n; } |
| 150 | ConstIterator &operator+=(qsizetype j) { item.i += j; return *this; } |
| 151 | ConstIterator &operator-=(qsizetype j) { item.i -= j; return *this; } |
| 152 | ConstIterator operator+(qsizetype j) const { return ConstIterator({ item.d, item.i + j }); } |
| 153 | ConstIterator operator-(qsizetype j) const { return ConstIterator({ item.d, item.i - j }); } |
| 154 | qsizetype operator-(ConstIterator j) const { return item.i - j.item.i; } |
| 155 | }; |
| 156 | |
| 157 | typedef qsizetype size_type; |
| 158 | typedef QCborValue value_type; |
| 159 | typedef value_type *pointer; |
| 160 | typedef const value_type *const_pointer; |
| 161 | typedef QCborValue &reference; |
| 162 | typedef const QCborValue &const_reference; |
| 163 | typedef qsizetype difference_type; |
| 164 | |
| 165 | QCborArray() noexcept; |
| 166 | QCborArray(const QCborArray &other) noexcept; |
| 167 | QCborArray &operator=(const QCborArray &other) noexcept; |
| 168 | QCborArray(std::initializer_list<QCborValue> args) |
| 169 | : QCborArray() |
| 170 | { |
| 171 | detach(qsizetype(args.size())); |
| 172 | for (const QCborValue &v : args) |
| 173 | append(v); |
| 174 | } |
| 175 | ~QCborArray(); |
| 176 | |
| 177 | void swap(QCborArray &other) noexcept |
| 178 | { |
| 179 | qSwap(d, other.d); |
| 180 | } |
| 181 | |
| 182 | QCborValue toCborValue() const { return *this; } |
| 183 | |
| 184 | qsizetype size() const noexcept; |
| 185 | bool isEmpty() const { return size() == 0; } |
| 186 | void clear(); |
| 187 | |
| 188 | QCborValue at(qsizetype i) const; |
| 189 | QCborValue first() const { return at(0); } |
| 190 | QCborValue last() const { return at(size() - 1); } |
| 191 | const QCborValue operator[](qsizetype i) const { return at(i); } |
| 192 | QCborValueRef first() { Q_ASSERT(!isEmpty()); return begin()[0]; } |
| 193 | QCborValueRef last() { Q_ASSERT(!isEmpty()); return begin()[size() - 1]; } |
| 194 | QCborValueRef operator[](qsizetype i) |
| 195 | { |
| 196 | if (i >= size()) |
| 197 | insert(i, QCborValue()); |
| 198 | return begin()[i]; |
| 199 | } |
| 200 | |
| 201 | void insert(qsizetype i, const QCborValue &value); |
| 202 | void insert(qsizetype i, QCborValue &&value); |
| 203 | void prepend(const QCborValue &value) { insert(0, value); } |
| 204 | void prepend(QCborValue &&value) { insert(0, std::move(value)); } |
| 205 | void append(const QCborValue &value) { insert(-1, value); } |
| 206 | void append(QCborValue &&value) { insert(-1, std::move(value)); } |
| 207 | QCborValue (ConstIterator it) { return extract(Iterator{ it.item.d, it.item.i }); } |
| 208 | QCborValue (Iterator it); |
| 209 | void removeAt(qsizetype i); |
| 210 | QCborValue takeAt(qsizetype i) { Q_ASSERT(i < size()); return extract(begin() + i); } |
| 211 | void removeFirst() { removeAt(0); } |
| 212 | void removeLast() { removeAt(size() - 1); } |
| 213 | QCborValue takeFirst() { return takeAt(0); } |
| 214 | QCborValue takeLast() { return takeAt(size() - 1); } |
| 215 | |
| 216 | bool contains(const QCborValue &value) const; |
| 217 | |
| 218 | int compare(const QCborArray &other) const noexcept Q_DECL_PURE_FUNCTION; |
| 219 | #if 0 && __has_include(<compare>) |
| 220 | std::strong_ordering operator<=>(const QCborArray &other) const |
| 221 | { |
| 222 | int c = compare(other); |
| 223 | if (c > 0) return std::strong_ordering::greater; |
| 224 | if (c == 0) return std::strong_ordering::equivalent; |
| 225 | return std::strong_ordering::less; |
| 226 | } |
| 227 | #else |
| 228 | bool operator==(const QCborArray &other) const noexcept |
| 229 | { return compare(other) == 0; } |
| 230 | bool operator!=(const QCborArray &other) const noexcept |
| 231 | { return !(*this == other); } |
| 232 | bool operator<(const QCborArray &other) const |
| 233 | { return compare(other) < 0; } |
| 234 | #endif |
| 235 | |
| 236 | typedef Iterator iterator; |
| 237 | typedef ConstIterator const_iterator; |
| 238 | iterator begin() { detach(); return iterator{d.data(), 0}; } |
| 239 | const_iterator constBegin() const { return const_iterator{d.data(), 0}; } |
| 240 | const_iterator begin() const { return constBegin(); } |
| 241 | const_iterator cbegin() const { return constBegin(); } |
| 242 | iterator end() { detach(); return iterator{d.data(), size()}; } |
| 243 | const_iterator constEnd() const { return const_iterator{d.data(), size()}; } |
| 244 | const_iterator end() const { return constEnd(); } |
| 245 | const_iterator cend() const { return constEnd(); } |
| 246 | iterator insert(iterator before, const QCborValue &value) |
| 247 | { insert(before.item.i, value); return iterator{d.data(), before.item.i}; } |
| 248 | iterator insert(const_iterator before, const QCborValue &value) |
| 249 | { insert(before.item.i, value); return iterator{d.data(), before.item.i}; } |
| 250 | iterator erase(iterator it) { removeAt(it.item.i); return iterator{d.data(), it.item.i}; } |
| 251 | iterator erase(const_iterator it) { removeAt(it.item.i); return iterator{d.data(), it.item.i}; } |
| 252 | |
| 253 | void push_back(const QCborValue &t) { append(t); } |
| 254 | void push_front(const QCborValue &t) { prepend(t); } |
| 255 | void pop_front() { removeFirst(); } |
| 256 | void pop_back() { removeLast(); } |
| 257 | bool empty() const { return isEmpty(); } |
| 258 | |
| 259 | // convenience |
| 260 | QCborArray operator+(const QCborValue &v) const |
| 261 | { QCborArray n = *this; n += v; return n; } |
| 262 | QCborArray &operator+=(const QCborValue &v) |
| 263 | { append(v); return *this; } |
| 264 | QCborArray &operator<<(const QCborValue &v) |
| 265 | { append(v); return *this; } |
| 266 | |
| 267 | static QCborArray fromStringList(const QStringList &list); |
| 268 | static QCborArray fromVariantList(const QVariantList &list); |
| 269 | static QCborArray fromJsonArray(const QJsonArray &array); |
| 270 | QVariantList toVariantList() const; |
| 271 | QJsonArray toJsonArray() const; |
| 272 | |
| 273 | private: |
| 274 | void detach(qsizetype reserve = 0); |
| 275 | |
| 276 | friend QCborValue; |
| 277 | friend QCborValueRef; |
| 278 | friend class QJsonPrivate::Variant; |
| 279 | friend class QCborContainerPrivate; |
| 280 | explicit QCborArray(QCborContainerPrivate &dd) noexcept; |
| 281 | QExplicitlySharedDataPointer<QCborContainerPrivate> d; |
| 282 | }; |
| 283 | |
| 284 | Q_DECLARE_SHARED(QCborArray) |
| 285 | |
| 286 | inline QCborValue::QCborValue(QCborArray &&a) |
| 287 | : n(-1), container(a.d.take()), t(Array) |
| 288 | { |
| 289 | } |
| 290 | |
| 291 | inline QCborArray QCborValueRef::toArray() const |
| 292 | { |
| 293 | return concrete().toArray(); |
| 294 | } |
| 295 | |
| 296 | inline QCborArray QCborValueRef::toArray(const QCborArray &a) const |
| 297 | { |
| 298 | return concrete().toArray(a); |
| 299 | } |
| 300 | |
| 301 | Q_CORE_EXPORT uint qHash(const QCborArray &array, uint seed = 0); |
| 302 | |
| 303 | #if !defined(QT_NO_DEBUG_STREAM) |
| 304 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborArray &a); |
| 305 | #endif |
| 306 | |
| 307 | #ifndef QT_NO_DATASTREAM |
| 308 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborArray &); |
| 309 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborArray &); |
| 310 | #endif |
| 311 | |
| 312 | QT_END_NAMESPACE |
| 313 | |
| 314 | #endif // QCBORARRAY_H |
| 315 | |