| 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 QCBORMAP_H |
| 41 | #define QCBORMAP_H |
| 42 | |
| 43 | #include <QtCore/qcborvalue.h> |
| 44 | #include <QtCore/qpair.h> |
| 45 | |
| 46 | #include <initializer_list> |
| 47 | |
| 48 | QT_BEGIN_NAMESPACE |
| 49 | |
| 50 | template <class Key, class T> class QMap; |
| 51 | typedef QMap<QString, QVariant> QVariantMap; |
| 52 | template <class Key, class T> class QHash; |
| 53 | typedef QHash<QString, QVariant> QVariantHash; |
| 54 | class QJsonObject; |
| 55 | class QDataStream; |
| 56 | |
| 57 | namespace QJsonPrivate { class Variant; } |
| 58 | |
| 59 | class QCborContainerPrivate; |
| 60 | class Q_CORE_EXPORT QCborMap |
| 61 | { |
| 62 | public: |
| 63 | typedef QPair<QCborValue, QCborValue> value_type; |
| 64 | typedef QCborValue key_type; |
| 65 | typedef QCborValue mapped_type; |
| 66 | typedef qsizetype size_type; |
| 67 | |
| 68 | class ConstIterator; |
| 69 | class Iterator { |
| 70 | mutable QCborValueRef item; // points to the value |
| 71 | friend class ConstIterator; |
| 72 | friend class QCborMap; |
| 73 | Iterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {} |
| 74 | public: |
| 75 | typedef std::random_access_iterator_tag iterator_category; |
| 76 | typedef qsizetype difference_type; |
| 77 | typedef QPair<const QCborValueRef, QCborValueRef> value_type; |
| 78 | typedef QPair<const QCborValueRef, QCborValueRef> reference; |
| 79 | typedef QPair<const QCborValueRef, QCborValueRef> pointer; |
| 80 | |
| 81 | Q_DECL_CONSTEXPR Iterator() = default; |
| 82 | Q_DECL_CONSTEXPR Iterator(const Iterator &) = default; |
| 83 | Iterator &operator=(const Iterator &other) |
| 84 | { |
| 85 | // rebind the reference |
| 86 | item.d = other.item.d; |
| 87 | item.i = other.item.i; |
| 88 | return *this; |
| 89 | } |
| 90 | |
| 91 | value_type operator*() const { return { {item.d, item.i - 1}, item }; } |
| 92 | QCborValueRef *operator->() const { return &item; } |
| 93 | QCborValue key() const { return QCborValueRef(item.d, item.i - 1); } |
| 94 | QCborValueRef value() const { return item; } |
| 95 | |
| 96 | bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 97 | bool operator!=(const Iterator &o) const { return !(*this == o); } |
| 98 | bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 99 | bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 100 | bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 101 | bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 102 | bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 103 | bool operator!=(const ConstIterator &o) const { return !(*this == o); } |
| 104 | bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 105 | bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 106 | bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 107 | bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 108 | Iterator &operator++() { item.i += 2; return *this; } |
| 109 | Iterator operator++(int) { Iterator n = *this; item.i += 2; return n; } |
| 110 | Iterator &operator--() { item.i -= 2; return *this; } |
| 111 | Iterator operator--(int) { Iterator n = *this; item.i -= 2; return n; } |
| 112 | Iterator &operator+=(qsizetype j) { item.i += 2 * j; return *this; } |
| 113 | Iterator &operator-=(qsizetype j) { item.i -= 2 * j; return *this; } |
| 114 | Iterator operator+(qsizetype j) const { return Iterator({ item.d, item.i + 2 * j }); } |
| 115 | Iterator operator-(qsizetype j) const { return Iterator({ item.d, item.i - 2 * j }); } |
| 116 | qsizetype operator-(Iterator j) const { return (item.i - j.item.i) / 2; } |
| 117 | }; |
| 118 | |
| 119 | class ConstIterator { |
| 120 | QCborValueRef item; // points to the value |
| 121 | friend class Iterator; |
| 122 | friend class QCborMap; |
| 123 | friend class QCborValue; |
| 124 | friend class QCborValueRef; |
| 125 | ConstIterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {} |
| 126 | public: |
| 127 | typedef std::random_access_iterator_tag iterator_category; |
| 128 | typedef qsizetype difference_type; |
| 129 | typedef QPair<const QCborValueRef, const QCborValueRef> value_type; |
| 130 | typedef QPair<const QCborValueRef, const QCborValueRef> reference; |
| 131 | typedef QPair<const QCborValueRef, const QCborValueRef> pointer; |
| 132 | |
| 133 | Q_DECL_CONSTEXPR ConstIterator() = default; |
| 134 | Q_DECL_CONSTEXPR ConstIterator(const ConstIterator &) = default; |
| 135 | ConstIterator &operator=(const ConstIterator &other) |
| 136 | { |
| 137 | // rebind the reference |
| 138 | item.d = other.item.d; |
| 139 | item.i = other.item.i; |
| 140 | return *this; |
| 141 | } |
| 142 | |
| 143 | value_type operator*() const { return { {item.d, item.i - 1}, item }; } |
| 144 | const QCborValueRef *operator->() const { return &item; } |
| 145 | QCborValue key() const { return QCborValueRef(item.d, item.i - 1); } |
| 146 | QCborValueRef value() const { return item; } |
| 147 | |
| 148 | bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 149 | bool operator!=(const Iterator &o) const { return !(*this == o); } |
| 150 | bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 151 | bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 152 | bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 153 | bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 154 | bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; } |
| 155 | bool operator!=(const ConstIterator &o) const { return !(*this == o); } |
| 156 | bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; } |
| 157 | bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; } |
| 158 | bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; } |
| 159 | bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; } |
| 160 | ConstIterator &operator++() { item.i += 2; return *this; } |
| 161 | ConstIterator operator++(int) { ConstIterator n = *this; item.i += 2; return n; } |
| 162 | ConstIterator &operator--() { item.i -= 2; return *this; } |
| 163 | ConstIterator operator--(int) { ConstIterator n = *this; item.i -= 2; return n; } |
| 164 | ConstIterator &operator+=(qsizetype j) { item.i += 2 * j; return *this; } |
| 165 | ConstIterator &operator-=(qsizetype j) { item.i -= 2 * j; return *this; } |
| 166 | ConstIterator operator+(qsizetype j) const { return ConstIterator({ item.d, item.i + 2 * j }); } |
| 167 | ConstIterator operator-(qsizetype j) const { return ConstIterator({ item.d, item.i - 2 * j }); } |
| 168 | qsizetype operator-(ConstIterator j) const { return (item.i - j.item.i) / 2; } |
| 169 | }; |
| 170 | |
| 171 | QCborMap() noexcept; |
| 172 | QCborMap(const QCborMap &other) noexcept; |
| 173 | QCborMap &operator=(const QCborMap &other) noexcept; |
| 174 | QCborMap(std::initializer_list<value_type> args) |
| 175 | : QCborMap() |
| 176 | { |
| 177 | detach(reserve: args.size()); |
| 178 | for (const auto &pair : args) |
| 179 | insert(key: pair.first, value_: pair.second); |
| 180 | } |
| 181 | ~QCborMap(); |
| 182 | |
| 183 | void swap(QCborMap &other) noexcept |
| 184 | { |
| 185 | qSwap(value1&: d, value2&: other.d); |
| 186 | } |
| 187 | |
| 188 | QCborValue toCborValue() const { return *this; } |
| 189 | |
| 190 | qsizetype size() const noexcept Q_DECL_PURE_FUNCTION; |
| 191 | bool isEmpty() const { return size() == 0; } |
| 192 | void clear(); |
| 193 | QVector<QCborValue> keys() const; |
| 194 | |
| 195 | QCborValue value(qint64 key) const |
| 196 | { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } |
| 197 | QCborValue value(QLatin1String key) const |
| 198 | { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } |
| 199 | QCborValue value(const QString & key) const |
| 200 | { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } |
| 201 | QCborValue value(const QCborValue &key) const |
| 202 | { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } |
| 203 | #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) |
| 204 | template<size_t N> QT_ASCII_CAST_WARN const QCborValue value(const char (&key)[N]) const |
| 205 | { return value(QString::fromUtf8(key, N - 1)); } |
| 206 | #endif |
| 207 | const QCborValue operator[](qint64 key) const |
| 208 | { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } |
| 209 | const QCborValue operator[](QLatin1String key) const |
| 210 | { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } |
| 211 | const QCborValue operator[](const QString & key) const |
| 212 | { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } |
| 213 | const QCborValue operator[](const QCborValue &key) const |
| 214 | { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } |
| 215 | #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) |
| 216 | template<size_t N> QT_ASCII_CAST_WARN const QCborValue operator[](const char (&key)[N]) const |
| 217 | { return operator[](QString::fromUtf8(key, N - 1)); } |
| 218 | #endif |
| 219 | QCborValueRef operator[](qint64 key); |
| 220 | QCborValueRef operator[](QLatin1String key); |
| 221 | QCborValueRef operator[](const QString & key); |
| 222 | QCborValueRef operator[](const QCborValue &key); |
| 223 | |
| 224 | QCborValue take(qint64 key) |
| 225 | { const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); } |
| 226 | QCborValue take(QLatin1String key) |
| 227 | { const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); } |
| 228 | QCborValue take(const QString &key) |
| 229 | { const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); } |
| 230 | QCborValue take(const QCborValue &key) |
| 231 | { const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); } |
| 232 | void remove(qint64 key) |
| 233 | { const_iterator it = constFind(key); if (it != constEnd()) erase(it); } |
| 234 | void remove(QLatin1String key) |
| 235 | { const_iterator it = constFind(key); if (it != constEnd()) erase(it); } |
| 236 | void remove(const QString & key) |
| 237 | { const_iterator it = constFind(key); if (it != constEnd()) erase(it); } |
| 238 | void remove(const QCborValue &key) |
| 239 | { const_iterator it = constFind(key); if (it != constEnd()) erase(it); } |
| 240 | bool contains(qint64 key) const |
| 241 | { const_iterator it = find(key); return it != end(); } |
| 242 | bool contains(QLatin1String key) const |
| 243 | { const_iterator it = find(key); return it != end(); } |
| 244 | bool contains(const QString & key) const |
| 245 | { const_iterator it = find(key); return it != end(); } |
| 246 | bool contains(const QCborValue &key) const |
| 247 | { const_iterator it = find(key); return it != end(); } |
| 248 | |
| 249 | int compare(const QCborMap &other) const noexcept Q_DECL_PURE_FUNCTION; |
| 250 | #if 0 && __has_include(<compare>) |
| 251 | std::strong_ordering operator<=>(const QCborMap &other) const |
| 252 | { |
| 253 | int c = compare(other); |
| 254 | if (c > 0) return std::strong_ordering::greater; |
| 255 | if (c == 0) return std::strong_ordering::equivalent; |
| 256 | return std::strong_ordering::less; |
| 257 | } |
| 258 | #else |
| 259 | bool operator==(const QCborMap &other) const noexcept |
| 260 | { return compare(other) == 0; } |
| 261 | bool operator!=(const QCborMap &other) const noexcept |
| 262 | { return !(*this == other); } |
| 263 | bool operator<(const QCborMap &other) const |
| 264 | { return compare(other) < 0; } |
| 265 | #endif |
| 266 | |
| 267 | typedef Iterator iterator; |
| 268 | typedef ConstIterator const_iterator; |
| 269 | iterator begin() { detach(); return iterator{d.data(), 1}; } |
| 270 | const_iterator constBegin() const { return const_iterator{d.data(), 1}; } |
| 271 | const_iterator begin() const { return constBegin(); } |
| 272 | const_iterator cbegin() const { return constBegin(); } |
| 273 | iterator end() { detach(); return iterator{d.data(), 2 * size() + 1}; } |
| 274 | const_iterator constEnd() const { return const_iterator{d.data(), 2 * size() + 1}; } |
| 275 | const_iterator end() const { return constEnd(); } |
| 276 | const_iterator cend() const { return constEnd(); } |
| 277 | iterator erase(iterator it); |
| 278 | iterator erase(const_iterator it) { return erase(it: iterator{ it.item.d, it.item.i }); } |
| 279 | QCborValue (iterator it); |
| 280 | QCborValue (const_iterator it) { return extract(it: iterator{ it.item.d, it.item.i }); } |
| 281 | bool empty() const { return isEmpty(); } |
| 282 | |
| 283 | iterator find(qint64 key); |
| 284 | iterator find(QLatin1String key); |
| 285 | iterator find(const QString & key); |
| 286 | iterator find(const QCborValue &key); |
| 287 | const_iterator constFind(qint64 key) const; |
| 288 | const_iterator constFind(QLatin1String key) const; |
| 289 | const_iterator constFind(const QString & key) const; |
| 290 | const_iterator constFind(const QCborValue &key) const; |
| 291 | const_iterator find(qint64 key) const { return constFind(key); } |
| 292 | const_iterator find(QLatin1String key) const { return constFind(key); } |
| 293 | const_iterator find(const QString & key) const { return constFind(key); } |
| 294 | const_iterator find(const QCborValue &key) const { return constFind(key); } |
| 295 | |
| 296 | iterator insert(qint64 key, const QCborValue &value_) |
| 297 | { |
| 298 | QCborValueRef v = operator[](key); // detaches |
| 299 | v = value_; |
| 300 | return { d.data(), v.i }; |
| 301 | } |
| 302 | iterator insert(QLatin1String key, const QCborValue &value_) |
| 303 | { |
| 304 | QCborValueRef v = operator[](key); // detaches |
| 305 | v = value_; |
| 306 | return { d.data(), v.i }; |
| 307 | } |
| 308 | iterator insert(const QString &key, const QCborValue &value_) |
| 309 | { |
| 310 | QCborValueRef v = operator[](key); // detaches |
| 311 | v = value_; |
| 312 | return { d.data(), v.i }; |
| 313 | } |
| 314 | iterator insert(const QCborValue &key, const QCborValue &value_) |
| 315 | { |
| 316 | QCborValueRef v = operator[](key); // detaches |
| 317 | v = value_; |
| 318 | return { d.data(), v.i }; |
| 319 | } |
| 320 | iterator insert(value_type v) { return insert(key: v.first, value_: v.second); } |
| 321 | |
| 322 | static QCborMap fromVariantMap(const QVariantMap &map); |
| 323 | static QCborMap fromVariantHash(const QVariantHash &hash); |
| 324 | static QCborMap fromJsonObject(const QJsonObject &o); |
| 325 | QVariantMap toVariantMap() const; |
| 326 | QVariantHash toVariantHash() const; |
| 327 | QJsonObject toJsonObject() const; |
| 328 | |
| 329 | private: |
| 330 | friend class QCborValue; |
| 331 | friend class QCborValueRef; |
| 332 | friend class QJsonPrivate::Variant; |
| 333 | friend class QCborContainerPrivate; |
| 334 | void detach(qsizetype reserve = 0); |
| 335 | |
| 336 | explicit QCborMap(QCborContainerPrivate &dd) noexcept; |
| 337 | QExplicitlySharedDataPointer<QCborContainerPrivate> d; |
| 338 | }; |
| 339 | |
| 340 | Q_DECLARE_SHARED(QCborMap) |
| 341 | |
| 342 | inline QCborValue::QCborValue(QCborMap &&m) |
| 343 | : n(-1), container(m.d.take()), t(Map) |
| 344 | { |
| 345 | } |
| 346 | |
| 347 | inline QCborMap QCborValueRef::toMap() const |
| 348 | { |
| 349 | return concrete().toMap(); |
| 350 | } |
| 351 | |
| 352 | inline QCborMap QCborValueRef::toMap(const QCborMap &m) const |
| 353 | { |
| 354 | return concrete().toMap(defaultValue: m); |
| 355 | } |
| 356 | |
| 357 | Q_CORE_EXPORT uint qHash(const QCborMap &map, uint seed = 0); |
| 358 | |
| 359 | #if !defined(QT_NO_DEBUG_STREAM) |
| 360 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborMap &m); |
| 361 | #endif |
| 362 | |
| 363 | #ifndef QT_NO_DATASTREAM |
| 364 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborMap &); |
| 365 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborMap &); |
| 366 | #endif |
| 367 | |
| 368 | |
| 369 | QT_END_NAMESPACE |
| 370 | |
| 371 | #endif // QCBORMAP_H |
| 372 | |