| 1 | // Copyright (C) 2022 Intel Corporation. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
| 3 | // Qt-Security score:critical reason:data-parser |
| 4 | |
| 5 | #ifndef QCBORMAP_H |
| 6 | #define QCBORMAP_H |
| 7 | |
| 8 | #include <QtCore/qcborvalue.h> |
| 9 | #include <QtCore/qpair.h> |
| 10 | |
| 11 | #include <initializer_list> |
| 12 | |
| 13 | QT_BEGIN_NAMESPACE |
| 14 | |
| 15 | class QJsonObject; |
| 16 | class QDataStream; |
| 17 | |
| 18 | namespace QJsonPrivate { class Variant; } |
| 19 | |
| 20 | namespace QtPrivate { |
| 21 | |
| 22 | template <typename T, typename Iterator> |
| 23 | struct QCborMapKeyValues |
| 24 | { |
| 25 | static QCborValueConstRef key(const Iterator &it) { return it.keyRef(); } |
| 26 | static QCborValueConstRef key(Iterator &it) { return it.keyRef(); } |
| 27 | static T value(const Iterator &it) { return it.value(); } |
| 28 | static T value(Iterator &it) { return it.value(); } |
| 29 | }; |
| 30 | |
| 31 | } // namespace QtPrivate |
| 32 | |
| 33 | class QCborContainerPrivate; |
| 34 | class Q_CORE_EXPORT QCborMap |
| 35 | { |
| 36 | public: |
| 37 | typedef std::pair<QCborValue, QCborValue> value_type; |
| 38 | typedef QCborValue key_type; |
| 39 | typedef QCborValue mapped_type; |
| 40 | typedef qsizetype size_type; |
| 41 | |
| 42 | class ConstIterator; |
| 43 | class Iterator { |
| 44 | QCborValueRef item {}; // points to the value |
| 45 | friend class ConstIterator; |
| 46 | friend class QCborMap; |
| 47 | Iterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {} |
| 48 | public: |
| 49 | typedef std::random_access_iterator_tag iterator_category; |
| 50 | typedef qsizetype difference_type; |
| 51 | typedef std::pair<QCborValueConstRef, QCborValueRef> value_type; |
| 52 | typedef std::pair<QCborValueConstRef, QCborValueRef> reference; |
| 53 | typedef std::pair<QCborValueConstRef, QCborValueRef> pointer; |
| 54 | |
| 55 | constexpr Iterator() = default; |
| 56 | constexpr Iterator(const Iterator &) = default; |
| 57 | ~Iterator() = default; |
| 58 | Iterator &operator=(const Iterator &other) |
| 59 | { |
| 60 | // rebind the reference |
| 61 | item.d = other.item.d; |
| 62 | item.i = other.item.i; |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | value_type operator*() const { return { QCborValueRef{item.d, item.i - 1}, item }; } |
| 67 | value_type operator[](qsizetype j) const { return *(*this + j); } |
| 68 | QCborValueRef *operator->() { return &item; } |
| 69 | const QCborValueConstRef *operator->() const { return &item; } |
| 70 | #if QT_VERSION >= QT_VERSION_CHECK(7,0,0) |
| 71 | QCborValueConstRef |
| 72 | #else |
| 73 | QCborValue |
| 74 | #endif |
| 75 | key() const { return QCborValueRef(item.d, item.i - 1); } |
| 76 | QCborValueConstRef keyRef() const { return QCborValueRef(item.d, item.i - 1); } |
| 77 | QCborValueRef value() const { return item; } |
| 78 | |
| 79 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 80 | bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 81 | bool operator!=(const Iterator &o) const { return !operator==(o); } |
| 82 | bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 83 | bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 84 | bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 85 | bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 86 | bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 87 | bool operator!=(const ConstIterator &o) const { return !operator==(o); } |
| 88 | bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 89 | bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 90 | bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 91 | bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 92 | #endif |
| 93 | Iterator &operator++() { item.i += 2; return *this; } |
| 94 | Iterator operator++(int) { Iterator n = *this; item.i += 2; return n; } |
| 95 | Iterator &operator--() { item.i -= 2; return *this; } |
| 96 | Iterator operator--(int) { Iterator n = *this; item.i -= 2; return n; } |
| 97 | Iterator &operator+=(qsizetype j) { item.i += 2 * j; return *this; } |
| 98 | Iterator &operator-=(qsizetype j) { item.i -= 2 * j; return *this; } |
| 99 | Iterator operator+(qsizetype j) const { return Iterator({ item.d, item.i + 2 * j }); } |
| 100 | Iterator operator-(qsizetype j) const { return Iterator({ item.d, item.i - 2 * j }); } |
| 101 | qsizetype operator-(Iterator j) const { return (item.i - j.item.i) / 2; } |
| 102 | |
| 103 | private: |
| 104 | // Helper functions |
| 105 | static bool comparesEqual_helper(const Iterator &lhs, const Iterator &rhs) noexcept |
| 106 | { |
| 107 | return lhs.item.d == rhs.item.d && lhs.item.i == rhs.item.i; |
| 108 | } |
| 109 | |
| 110 | static bool comparesEqual_helper(const Iterator &lhs, const ConstIterator &rhs) noexcept |
| 111 | { |
| 112 | return lhs.item.d == rhs.item.d && lhs.item.i == rhs.item.i; |
| 113 | } |
| 114 | |
| 115 | static Qt::strong_ordering compareThreeWay_helper(const Iterator &lhs, |
| 116 | const Iterator &rhs) |
| 117 | { |
| 118 | Q_ASSERT(lhs.item.d == rhs.item.d); |
| 119 | return Qt::compareThreeWay(lhs: lhs.item.i, rhs: rhs.item.i); |
| 120 | } |
| 121 | |
| 122 | static Qt::strong_ordering compareThreeWay_helper(const Iterator &lhs, |
| 123 | const ConstIterator &rhs) |
| 124 | { |
| 125 | Q_ASSERT(lhs.item.d == rhs.item.d); |
| 126 | return Qt::compareThreeWay(lhs: lhs.item.i, rhs: rhs.item.i); |
| 127 | } |
| 128 | |
| 129 | // Compare friends |
| 130 | friend bool comparesEqual(const Iterator &lhs, const Iterator &rhs) noexcept |
| 131 | { |
| 132 | return comparesEqual_helper(lhs, rhs); |
| 133 | } |
| 134 | friend Qt::strong_ordering compareThreeWay(const Iterator &lhs, |
| 135 | const Iterator &rhs) |
| 136 | { |
| 137 | return compareThreeWay_helper(lhs, rhs); |
| 138 | } |
| 139 | Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(Iterator) |
| 140 | friend bool comparesEqual(const Iterator &lhs, const ConstIterator &rhs) noexcept |
| 141 | { |
| 142 | return comparesEqual_helper(lhs, rhs); |
| 143 | } |
| 144 | friend Qt::strong_ordering compareThreeWay(const Iterator &lhs, |
| 145 | const ConstIterator &rhs) |
| 146 | { |
| 147 | return compareThreeWay_helper(lhs, rhs); |
| 148 | } |
| 149 | Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(Iterator, ConstIterator) |
| 150 | }; |
| 151 | |
| 152 | class ConstIterator { |
| 153 | QCborValueConstRef item; // points to the value |
| 154 | friend class Iterator; |
| 155 | friend class QCborMap; |
| 156 | friend class QCborValue; |
| 157 | friend class QCborValueRef; |
| 158 | constexpr ConstIterator(QCborValueConstRef it) : item{it} {} |
| 159 | ConstIterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {} |
| 160 | public: |
| 161 | typedef std::random_access_iterator_tag iterator_category; |
| 162 | typedef qsizetype difference_type; |
| 163 | typedef std::pair<QCborValueConstRef, QCborValueConstRef> value_type; |
| 164 | typedef std::pair<QCborValueConstRef, QCborValueConstRef> reference; |
| 165 | typedef std::pair<QCborValueConstRef, QCborValueConstRef> pointer; |
| 166 | |
| 167 | constexpr ConstIterator() = default; |
| 168 | constexpr ConstIterator(const ConstIterator &) = default; |
| 169 | ~ConstIterator() = default; |
| 170 | ConstIterator &operator=(const ConstIterator &other) |
| 171 | { |
| 172 | // rebind the reference |
| 173 | item.d = other.item.d; |
| 174 | item.i = other.item.i; |
| 175 | return *this; |
| 176 | } |
| 177 | |
| 178 | value_type operator*() const { return { QCborValueRef(item.d, item.i - 1), item }; } |
| 179 | value_type operator[](qsizetype j) const { return *(*this + j); } |
| 180 | const QCborValueConstRef *operator->() const { return &item; } |
| 181 | #if QT_VERSION >= QT_VERSION_CHECK(7,0,0) |
| 182 | QCborValueConstRef |
| 183 | #else |
| 184 | QCborValue |
| 185 | #endif |
| 186 | key() const { return QCborValueRef(item.d, item.i - 1); } |
| 187 | QCborValueConstRef keyRef() const { return QCborValueRef(item.d, item.i - 1); } |
| 188 | QCborValueConstRef value() const { return item; } |
| 189 | |
| 190 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 191 | bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 192 | bool operator!=(const Iterator &o) const { return !operator==(o); } |
| 193 | bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 194 | bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 195 | bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 196 | bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 197 | bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 198 | bool operator!=(const ConstIterator &o) const { return !operator==(o); } |
| 199 | bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 200 | bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 201 | bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 202 | bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 203 | #endif |
| 204 | ConstIterator &operator++() { item.i += 2; return *this; } |
| 205 | ConstIterator operator++(int) { ConstIterator n = *this; item.i += 2; return n; } |
| 206 | ConstIterator &operator--() { item.i -= 2; return *this; } |
| 207 | ConstIterator operator--(int) { ConstIterator n = *this; item.i -= 2; return n; } |
| 208 | ConstIterator &operator+=(qsizetype j) { item.i += 2 * j; return *this; } |
| 209 | ConstIterator &operator-=(qsizetype j) { item.i -= 2 * j; return *this; } |
| 210 | ConstIterator operator+(qsizetype j) const { return ConstIterator{ item.d, item.i + 2 * j }; } |
| 211 | ConstIterator operator-(qsizetype j) const { return ConstIterator{ item.d, item.i - 2 * j }; } |
| 212 | qsizetype operator-(ConstIterator j) const { return (item.i - j.item.i) / 2; } |
| 213 | private: |
| 214 | // Helper functions |
| 215 | static bool comparesEqual_helper(const ConstIterator &lhs, |
| 216 | const ConstIterator &rhs) noexcept |
| 217 | { |
| 218 | return lhs.item.d == rhs.item.d && lhs.item.i == rhs.item.i; |
| 219 | } |
| 220 | static Qt::strong_ordering compareThreeWay_helper(const ConstIterator &lhs, |
| 221 | const ConstIterator &rhs) |
| 222 | { |
| 223 | Q_ASSERT(lhs.item.d == rhs.item.d); |
| 224 | return Qt::compareThreeWay(lhs: lhs.item.i, rhs: rhs.item.i); |
| 225 | } |
| 226 | |
| 227 | // Compare friends |
| 228 | friend bool comparesEqual(const ConstIterator &lhs, const ConstIterator &rhs) noexcept |
| 229 | { |
| 230 | return comparesEqual_helper(lhs, rhs); |
| 231 | } |
| 232 | friend Qt::strong_ordering compareThreeWay(const ConstIterator &lhs, |
| 233 | const ConstIterator &rhs) |
| 234 | { |
| 235 | return compareThreeWay_helper(lhs, rhs); |
| 236 | } |
| 237 | Q_DECLARE_STRONGLY_ORDERED_NON_NOEXCEPT(ConstIterator) |
| 238 | }; |
| 239 | |
| 240 | QCborMap() noexcept; |
| 241 | QCborMap(const QCborMap &other) noexcept; |
| 242 | QCborMap(QCborMap &&other) noexcept = default; |
| 243 | QCborMap &operator=(const QCborMap &other) noexcept; |
| 244 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QCborMap) |
| 245 | QCborMap(std::initializer_list<value_type> args) |
| 246 | : QCborMap() |
| 247 | { |
| 248 | detach(reserve: args.size()); |
| 249 | for (const auto &pair : args) |
| 250 | insert(key: pair.first, value_: pair.second); |
| 251 | } |
| 252 | ~QCborMap(); |
| 253 | |
| 254 | void swap(QCborMap &other) noexcept |
| 255 | { |
| 256 | d.swap(other&: other.d); |
| 257 | } |
| 258 | |
| 259 | QCborValue toCborValue() const { return *this; } |
| 260 | |
| 261 | qsizetype size() const noexcept Q_DECL_PURE_FUNCTION; |
| 262 | bool isEmpty() const { return size() == 0; } |
| 263 | void clear(); |
| 264 | QList<QCborValue> keys() const; |
| 265 | |
| 266 | QCborValue value(qint64 key) const |
| 267 | { const_iterator it = find(key); return comparesEqual(lhs: it, rhs: end()) ? QCborValue() : it.value(); } |
| 268 | QCborValue value(QLatin1StringView key) const |
| 269 | { const_iterator it = find(key); return comparesEqual(lhs: it, rhs: end()) ? QCborValue() : it.value(); } |
| 270 | QCborValue value(const QString & key) const |
| 271 | { const_iterator it = find(key); return comparesEqual(lhs: it, rhs: end()) ? QCborValue() : it.value(); } |
| 272 | QCborValue value(const QCborValue &key) const |
| 273 | { const_iterator it = find(key); return comparesEqual(lhs: it, rhs: end()) ? QCborValue() : it.value(); } |
| 274 | #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) |
| 275 | template<size_t N> QT_ASCII_CAST_WARN const QCborValue value(const char (&key)[N]) const |
| 276 | { return value(QString::fromUtf8(key, N - 1)); } |
| 277 | #endif |
| 278 | const QCborValue operator[](qint64 key) const |
| 279 | { const_iterator it = find(key); return comparesEqual(lhs: it, rhs: end()) ? QCborValue() : it.value(); } |
| 280 | const QCborValue operator[](QLatin1StringView key) const |
| 281 | { const_iterator it = find(key); return comparesEqual(lhs: it, rhs: end()) ? QCborValue() : it.value(); } |
| 282 | const QCborValue operator[](const QString & key) const |
| 283 | { const_iterator it = find(key); return comparesEqual(lhs: it, rhs: end()) ? QCborValue() : it.value(); } |
| 284 | const QCborValue operator[](const QCborValue &key) const |
| 285 | { const_iterator it = find(key); return comparesEqual(lhs: it, rhs: end()) ? QCborValue() : it.value(); } |
| 286 | #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) |
| 287 | template<size_t N> QT_ASCII_CAST_WARN const QCborValue operator[](const char (&key)[N]) const |
| 288 | { return operator[](QString::fromUtf8(key, N - 1)); } |
| 289 | #endif |
| 290 | QCborValueRef operator[](qint64 key); |
| 291 | QCborValueRef operator[](QLatin1StringView key); |
| 292 | QCborValueRef operator[](const QString & key); |
| 293 | QCborValueRef operator[](const QCborValue &key); |
| 294 | |
| 295 | QCborValue take(qint64 key) |
| 296 | { const_iterator it = constFind(key); if (!comparesEqual(lhs: it, rhs: constEnd())) return extract(it); return QCborValue(); } |
| 297 | QCborValue take(QLatin1StringView key) |
| 298 | { const_iterator it = constFind(key); if (!comparesEqual(lhs: it, rhs: constEnd())) return extract(it); return QCborValue(); } |
| 299 | QCborValue take(const QString &key) |
| 300 | { const_iterator it = constFind(key); if (!comparesEqual(lhs: it, rhs: constEnd())) return extract(it); return QCborValue(); } |
| 301 | QCborValue take(const QCborValue &key) |
| 302 | { const_iterator it = constFind(key); if (!comparesEqual(lhs: it, rhs: constEnd())) return extract(it); return QCborValue(); } |
| 303 | void remove(qint64 key) |
| 304 | { const_iterator it = constFind(key); if (!comparesEqual(lhs: it, rhs: constEnd())) erase(it); } |
| 305 | void remove(QLatin1StringView key) |
| 306 | { const_iterator it = constFind(key); if (!comparesEqual(lhs: it, rhs: constEnd())) erase(it); } |
| 307 | void remove(const QString & key) |
| 308 | { const_iterator it = constFind(key); if (!comparesEqual(lhs: it, rhs: constEnd())) erase(it); } |
| 309 | void remove(const QCborValue &key) |
| 310 | { const_iterator it = constFind(key); if (!comparesEqual(lhs: it, rhs: constEnd())) erase(it); } |
| 311 | bool contains(qint64 key) const |
| 312 | { const_iterator it = find(key); return !comparesEqual(lhs: it, rhs: end()); } |
| 313 | bool contains(QLatin1StringView key) const |
| 314 | { const_iterator it = find(key); return !comparesEqual(lhs: it, rhs: end()); } |
| 315 | bool contains(const QString & key) const |
| 316 | { const_iterator it = find(key); return !comparesEqual(lhs: it, rhs: end()); } |
| 317 | bool contains(const QCborValue &key) const |
| 318 | { const_iterator it = find(key); return !comparesEqual(lhs: it, rhs: end()); } |
| 319 | |
| 320 | int compare(const QCborMap &other) const noexcept Q_DECL_PURE_FUNCTION; |
| 321 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 322 | bool operator==(const QCborMap &other) const noexcept |
| 323 | { return compare(other) == 0; } |
| 324 | bool operator!=(const QCborMap &other) const noexcept |
| 325 | { return !operator==(other); } |
| 326 | bool operator<(const QCborMap &other) const |
| 327 | { return compare(other) < 0; } |
| 328 | #endif |
| 329 | |
| 330 | typedef Iterator iterator; |
| 331 | typedef ConstIterator const_iterator; |
| 332 | iterator begin() { detach(); return iterator{d.data(), 1}; } |
| 333 | const_iterator constBegin() const { return const_iterator{d.data(), 1}; } |
| 334 | const_iterator begin() const { return constBegin(); } |
| 335 | const_iterator cbegin() const { return constBegin(); } |
| 336 | iterator end() { detach(); return iterator{d.data(), 2 * size() + 1}; } |
| 337 | const_iterator constEnd() const { return const_iterator{d.data(), 2 * size() + 1}; } |
| 338 | const_iterator end() const { return constEnd(); } |
| 339 | const_iterator cend() const { return constEnd(); } |
| 340 | iterator erase(iterator it); |
| 341 | iterator erase(const_iterator it) { return erase(it: iterator{ it.item.d, it.item.i }); } |
| 342 | QCborValue (iterator it); |
| 343 | QCborValue (const_iterator it) { return extract(it: iterator{ it.item.d, it.item.i }); } |
| 344 | bool empty() const { return isEmpty(); } |
| 345 | |
| 346 | typedef QKeyValueIterator<QCborValueConstRef, QCborValueConstRef, const_iterator, |
| 347 | QtPrivate::QCborMapKeyValues<QCborValueConstRef, ConstIterator>> |
| 348 | const_key_value_iterator; |
| 349 | typedef QKeyValueIterator<QCborValueConstRef, QCborValueRef, iterator, |
| 350 | QtPrivate::QCborMapKeyValues<QCborValueRef, Iterator>> |
| 351 | key_value_iterator; |
| 352 | |
| 353 | key_value_iterator keyValueBegin() { return key_value_iterator(begin()); } |
| 354 | key_value_iterator keyValueEnd() { return key_value_iterator(end()); } |
| 355 | const_key_value_iterator keyValueBegin() const { return const_key_value_iterator(begin()); } |
| 356 | const_key_value_iterator constKeyValueBegin() const |
| 357 | { |
| 358 | return const_key_value_iterator(begin()); |
| 359 | } |
| 360 | const_key_value_iterator keyValueEnd() const { return const_key_value_iterator(end()); } |
| 361 | const_key_value_iterator constKeyValueEnd() const { return const_key_value_iterator(end()); } |
| 362 | |
| 363 | auto asKeyValueRange() & { return QtPrivate::QKeyValueRange<QCborMap &>(*this); } |
| 364 | auto asKeyValueRange() const & { return QtPrivate::QKeyValueRange<const QCborMap &>(*this); } |
| 365 | auto asKeyValueRange() && { return QtPrivate::QKeyValueRange<QCborMap>(std::move(*this)); } |
| 366 | auto asKeyValueRange() const && |
| 367 | { |
| 368 | return QtPrivate::QKeyValueRange<QCborMap>(std::move(*this)); |
| 369 | } |
| 370 | |
| 371 | iterator find(qint64 key); |
| 372 | iterator find(QLatin1StringView key); |
| 373 | iterator find(const QString & key); |
| 374 | iterator find(const QCborValue &key); |
| 375 | const_iterator constFind(qint64 key) const; |
| 376 | const_iterator constFind(QLatin1StringView key) const; |
| 377 | const_iterator constFind(const QString & key) const; |
| 378 | const_iterator constFind(const QCborValue &key) const; |
| 379 | const_iterator find(qint64 key) const { return constFind(key); } |
| 380 | const_iterator find(QLatin1StringView key) const { return constFind(key); } |
| 381 | const_iterator find(const QString & key) const { return constFind(key); } |
| 382 | const_iterator find(const QCborValue &key) const { return constFind(key); } |
| 383 | |
| 384 | iterator insert(qint64 key, const QCborValue &value_) |
| 385 | { |
| 386 | QCborValueRef v = operator[](key); // detaches |
| 387 | v = value_; |
| 388 | return { d.data(), v.i }; |
| 389 | } |
| 390 | iterator insert(QLatin1StringView key, const QCborValue &value_) |
| 391 | { |
| 392 | QCborValueRef v = operator[](key); // detaches |
| 393 | v = value_; |
| 394 | return { d.data(), v.i }; |
| 395 | } |
| 396 | iterator insert(const QString &key, const QCborValue &value_) |
| 397 | { |
| 398 | QCborValueRef v = operator[](key); // detaches |
| 399 | v = value_; |
| 400 | return { d.data(), v.i }; |
| 401 | } |
| 402 | iterator insert(const QCborValue &key, const QCborValue &value_) |
| 403 | { |
| 404 | QCborValueRef v = operator[](key); // detaches |
| 405 | v = value_; |
| 406 | return { d.data(), v.i }; |
| 407 | } |
| 408 | iterator insert(value_type v) { return insert(key: v.first, value_: v.second); } |
| 409 | |
| 410 | static QCborMap fromVariantMap(const QVariantMap &map); |
| 411 | static QCborMap fromVariantHash(const QVariantHash &hash); |
| 412 | static QCborMap fromJsonObject(const QJsonObject &o); |
| 413 | static QCborMap fromJsonObject(QJsonObject &&o) noexcept; |
| 414 | QVariantMap toVariantMap() const; |
| 415 | QVariantHash toVariantHash() const; |
| 416 | QJsonObject toJsonObject() const; |
| 417 | |
| 418 | private: |
| 419 | friend class QCborContainerPrivate; |
| 420 | friend class QCborValue; |
| 421 | friend class QCborValueRef; |
| 422 | friend class QJsonPrivate::Variant; |
| 423 | void detach(qsizetype reserve = 0); |
| 424 | |
| 425 | friend Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool |
| 426 | comparesEqual(const QCborMap &lhs, const QCborMap &rhs) noexcept; |
| 427 | friend Qt::strong_ordering compareThreeWay(const QCborMap &lhs, |
| 428 | const QCborMap &rhs) noexcept |
| 429 | { |
| 430 | int c = lhs.compare(other: rhs); |
| 431 | return Qt::compareThreeWay(lhs: c, rhs: 0); |
| 432 | } |
| 433 | Q_DECLARE_STRONGLY_ORDERED(QCborMap) |
| 434 | |
| 435 | static Q_DECL_PURE_FUNCTION bool |
| 436 | comparesEqual_helper(const QCborMap &lhs, const QCborValue &rhs) noexcept; |
| 437 | static Q_DECL_PURE_FUNCTION Qt::strong_ordering |
| 438 | compareThreeWay_helper(const QCborMap &lhs, const QCborValue &rhs) noexcept; |
| 439 | friend bool comparesEqual(const QCborMap &lhs, |
| 440 | const QCborValue &rhs) noexcept |
| 441 | { |
| 442 | return comparesEqual_helper(lhs, rhs); |
| 443 | } |
| 444 | friend Qt::strong_ordering compareThreeWay(const QCborMap &lhs, |
| 445 | const QCborValue &rhs) noexcept |
| 446 | { |
| 447 | return compareThreeWay_helper(lhs, rhs); |
| 448 | } |
| 449 | Q_DECLARE_STRONGLY_ORDERED(QCborMap, QCborValue) |
| 450 | |
| 451 | static Q_DECL_PURE_FUNCTION bool |
| 452 | comparesEqual_helper(const QCborMap &lhs, QCborValueConstRef rhs) noexcept; |
| 453 | static Q_DECL_PURE_FUNCTION Qt::strong_ordering |
| 454 | compareThreeWay_helper(const QCborMap &lhs, QCborValueConstRef rhs) noexcept; |
| 455 | friend bool comparesEqual(const QCborMap &lhs, |
| 456 | const QCborValueConstRef &rhs) noexcept |
| 457 | { |
| 458 | return comparesEqual_helper(lhs, rhs); |
| 459 | } |
| 460 | friend Qt::strong_ordering compareThreeWay(const QCborMap &lhs, |
| 461 | const QCborValueConstRef &rhs) noexcept |
| 462 | { |
| 463 | return compareThreeWay_helper(lhs, rhs); |
| 464 | } |
| 465 | Q_DECLARE_STRONGLY_ORDERED(QCborMap, QCborValueConstRef) |
| 466 | |
| 467 | explicit QCborMap(QCborContainerPrivate &dd) noexcept; |
| 468 | QExplicitlySharedDataPointer<QCborContainerPrivate> d; |
| 469 | }; |
| 470 | |
| 471 | Q_DECLARE_SHARED(QCborMap) |
| 472 | |
| 473 | inline QCborValue::QCborValue(QCborMap &&m) |
| 474 | : n(-1), container(m.d.take()), t(Map) |
| 475 | { |
| 476 | } |
| 477 | |
| 478 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) |
| 479 | inline QCborMap QCborValueRef::toMap() const |
| 480 | { |
| 481 | return concrete().toMap(); |
| 482 | } |
| 483 | |
| 484 | inline QCborMap QCborValueRef::toMap(const QCborMap &m) const |
| 485 | { |
| 486 | return concrete().toMap(defaultValue: m); |
| 487 | } |
| 488 | #endif |
| 489 | |
| 490 | inline QCborMap QCborValueConstRef::toMap() const |
| 491 | { |
| 492 | return concrete().toMap(); |
| 493 | } |
| 494 | |
| 495 | inline QCborMap QCborValueConstRef::toMap(const QCborMap &m) const |
| 496 | { |
| 497 | return concrete().toMap(defaultValue: m); |
| 498 | } |
| 499 | |
| 500 | Q_CORE_EXPORT size_t qHash(const QCborMap &map, size_t seed = 0); |
| 501 | |
| 502 | #if !defined(QT_NO_DEBUG_STREAM) |
| 503 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborMap &m); |
| 504 | #endif |
| 505 | |
| 506 | #ifndef QT_NO_DATASTREAM |
| 507 | #if QT_CONFIG(cborstreamwriter) |
| 508 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborMap &); |
| 509 | #endif |
| 510 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborMap &); |
| 511 | #endif |
| 512 | |
| 513 | |
| 514 | QT_END_NAMESPACE |
| 515 | |
| 516 | #endif // QCBORMAP_H |
| 517 | |