Warning, file /include/QtCore/qcbormap.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004 #ifndef QCBORMAP_H
0005 #define QCBORMAP_H
0006
0007 #include <QtCore/qcborvalue.h>
0008 #include <QtCore/qpair.h>
0009
0010 #include <initializer_list>
0011
0012 QT_BEGIN_NAMESPACE
0013
0014 class QJsonObject;
0015 class QDataStream;
0016
0017 namespace QJsonPrivate { class Variant; }
0018
0019 class QCborContainerPrivate;
0020 class Q_CORE_EXPORT QCborMap
0021 {
0022 public:
0023 typedef std::pair<QCborValue, QCborValue> value_type;
0024 typedef QCborValue key_type;
0025 typedef QCborValue mapped_type;
0026 typedef qsizetype size_type;
0027
0028 class ConstIterator;
0029 class Iterator {
0030 QCborValueRef item {};
0031 friend class ConstIterator;
0032 friend class QCborMap;
0033 Iterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {}
0034 public:
0035 typedef std::random_access_iterator_tag iterator_category;
0036 typedef qsizetype difference_type;
0037 typedef std::pair<QCborValueConstRef, QCborValueRef> value_type;
0038 typedef std::pair<QCborValueConstRef, QCborValueRef> reference;
0039 typedef std::pair<QCborValueConstRef, QCborValueRef> pointer;
0040
0041 constexpr Iterator() = default;
0042 constexpr Iterator(const Iterator &) = default;
0043 ~Iterator() = default;
0044 Iterator &operator=(const Iterator &other)
0045 {
0046
0047 item.d = other.item.d;
0048 item.i = other.item.i;
0049 return *this;
0050 }
0051
0052 value_type operator*() const { return { QCborValueRef{item.d, item.i - 1}, item }; }
0053 value_type operator[](qsizetype j) const { return *(*this + j); }
0054 QCborValueRef *operator->() { return &item; }
0055 const QCborValueConstRef *operator->() const { return &item; }
0056 #if QT_VERSION >= QT_VERSION_CHECK(7,0,0)
0057 QCborValueConstRef
0058 #else
0059 QCborValue
0060 #endif
0061 key() const { return QCborValueRef(item.d, item.i - 1); }
0062 QCborValueRef value() const { return item; }
0063
0064 bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
0065 bool operator!=(const Iterator &o) const { return !(*this == o); }
0066 bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
0067 bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
0068 bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
0069 bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
0070 bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
0071 bool operator!=(const ConstIterator &o) const { return !(*this == o); }
0072 bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
0073 bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
0074 bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
0075 bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
0076 Iterator &operator++() { item.i += 2; return *this; }
0077 Iterator operator++(int) { Iterator n = *this; item.i += 2; return n; }
0078 Iterator &operator--() { item.i -= 2; return *this; }
0079 Iterator operator--(int) { Iterator n = *this; item.i -= 2; return n; }
0080 Iterator &operator+=(qsizetype j) { item.i += 2 * j; return *this; }
0081 Iterator &operator-=(qsizetype j) { item.i -= 2 * j; return *this; }
0082 Iterator operator+(qsizetype j) const { return Iterator({ item.d, item.i + 2 * j }); }
0083 Iterator operator-(qsizetype j) const { return Iterator({ item.d, item.i - 2 * j }); }
0084 qsizetype operator-(Iterator j) const { return (item.i - j.item.i) / 2; }
0085 };
0086
0087 class ConstIterator {
0088 QCborValueConstRef item;
0089 friend class Iterator;
0090 friend class QCborMap;
0091 friend class QCborValue;
0092 friend class QCborValueRef;
0093 constexpr ConstIterator(QCborValueConstRef it) : item{it} {}
0094 ConstIterator(QCborContainerPrivate *dd, qsizetype ii) : item(dd, ii) {}
0095 public:
0096 typedef std::random_access_iterator_tag iterator_category;
0097 typedef qsizetype difference_type;
0098 typedef std::pair<QCborValueConstRef, QCborValueConstRef> value_type;
0099 typedef std::pair<QCborValueConstRef, QCborValueConstRef> reference;
0100 typedef std::pair<QCborValueConstRef, QCborValueConstRef> pointer;
0101
0102 constexpr ConstIterator() = default;
0103 constexpr ConstIterator(const ConstIterator &) = default;
0104 ~ConstIterator() = default;
0105 ConstIterator &operator=(const ConstIterator &other)
0106 {
0107
0108 item.d = other.item.d;
0109 item.i = other.item.i;
0110 return *this;
0111 }
0112
0113 value_type operator*() const { return { QCborValueRef(item.d, item.i - 1), item }; }
0114 value_type operator[](qsizetype j) const { return *(*this + j); }
0115 const QCborValueConstRef *operator->() const { return &item; }
0116 #if QT_VERSION >= QT_VERSION_CHECK(7,0,0)
0117 QCborValueConstRef
0118 #else
0119 QCborValue
0120 #endif
0121 key() const { return QCborValueRef(item.d, item.i - 1); }
0122 QCborValueConstRef value() const { return item; }
0123
0124 bool operator==(const Iterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
0125 bool operator!=(const Iterator &o) const { return !(*this == o); }
0126 bool operator<(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
0127 bool operator<=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
0128 bool operator>(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
0129 bool operator>=(const Iterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
0130 bool operator==(const ConstIterator &o) const { return item.d == o.item.d && item.i == o.item.i; }
0131 bool operator!=(const ConstIterator &o) const { return !(*this == o); }
0132 bool operator<(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i < other.item.i; }
0133 bool operator<=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i <= other.item.i; }
0134 bool operator>(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i > other.item.i; }
0135 bool operator>=(const ConstIterator& other) const { Q_ASSERT(item.d == other.item.d); return item.i >= other.item.i; }
0136 ConstIterator &operator++() { item.i += 2; return *this; }
0137 ConstIterator operator++(int) { ConstIterator n = *this; item.i += 2; return n; }
0138 ConstIterator &operator--() { item.i -= 2; return *this; }
0139 ConstIterator operator--(int) { ConstIterator n = *this; item.i -= 2; return n; }
0140 ConstIterator &operator+=(qsizetype j) { item.i += 2 * j; return *this; }
0141 ConstIterator &operator-=(qsizetype j) { item.i -= 2 * j; return *this; }
0142 ConstIterator operator+(qsizetype j) const { return ConstIterator{ item.d, item.i + 2 * j }; }
0143 ConstIterator operator-(qsizetype j) const { return ConstIterator{ item.d, item.i - 2 * j }; }
0144 qsizetype operator-(ConstIterator j) const { return (item.i - j.item.i) / 2; }
0145 };
0146
0147 QCborMap() noexcept;
0148 QCborMap(const QCborMap &other) noexcept;
0149 QCborMap &operator=(const QCborMap &other) noexcept;
0150 QCborMap(std::initializer_list<value_type> args)
0151 : QCborMap()
0152 {
0153 detach(args.size());
0154 for (const auto &pair : args)
0155 insert(pair.first, pair.second);
0156 }
0157 ~QCborMap();
0158
0159 void swap(QCborMap &other) noexcept
0160 {
0161 d.swap(other.d);
0162 }
0163
0164 QCborValue toCborValue() const { return *this; }
0165
0166 qsizetype size() const noexcept Q_DECL_PURE_FUNCTION;
0167 bool isEmpty() const { return size() == 0; }
0168 void clear();
0169 QList<QCborValue> keys() const;
0170
0171 QCborValue value(qint64 key) const
0172 { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
0173 QCborValue value(QLatin1StringView key) const
0174 { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
0175 QCborValue value(const QString & key) const
0176 { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
0177 QCborValue value(const QCborValue &key) const
0178 { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
0179 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0180 template<size_t N> QT_ASCII_CAST_WARN const QCborValue value(const char (&key)[N]) const
0181 { return value(QString::fromUtf8(key, N - 1)); }
0182 #endif
0183 const QCborValue operator[](qint64 key) const
0184 { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
0185 const QCborValue operator[](QLatin1StringView key) const
0186 { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
0187 const QCborValue operator[](const QString & key) const
0188 { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
0189 const QCborValue operator[](const QCborValue &key) const
0190 { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); }
0191 #if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
0192 template<size_t N> QT_ASCII_CAST_WARN const QCborValue operator[](const char (&key)[N]) const
0193 { return operator[](QString::fromUtf8(key, N - 1)); }
0194 #endif
0195 QCborValueRef operator[](qint64 key);
0196 QCborValueRef operator[](QLatin1StringView key);
0197 QCborValueRef operator[](const QString & key);
0198 QCborValueRef operator[](const QCborValue &key);
0199
0200 QCborValue take(qint64 key)
0201 { const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); }
0202 QCborValue take(QLatin1StringView key)
0203 { const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); }
0204 QCborValue take(const QString &key)
0205 { const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); }
0206 QCborValue take(const QCborValue &key)
0207 { const_iterator it = constFind(key); if (it != constEnd()) return extract(it); return QCborValue(); }
0208 void remove(qint64 key)
0209 { const_iterator it = constFind(key); if (it != constEnd()) erase(it); }
0210 void remove(QLatin1StringView key)
0211 { const_iterator it = constFind(key); if (it != constEnd()) erase(it); }
0212 void remove(const QString & key)
0213 { const_iterator it = constFind(key); if (it != constEnd()) erase(it); }
0214 void remove(const QCborValue &key)
0215 { const_iterator it = constFind(key); if (it != constEnd()) erase(it); }
0216 bool contains(qint64 key) const
0217 { const_iterator it = find(key); return it != end(); }
0218 bool contains(QLatin1StringView key) const
0219 { const_iterator it = find(key); return it != end(); }
0220 bool contains(const QString & key) const
0221 { const_iterator it = find(key); return it != end(); }
0222 bool contains(const QCborValue &key) const
0223 { const_iterator it = find(key); return it != end(); }
0224
0225 int compare(const QCborMap &other) const noexcept Q_DECL_PURE_FUNCTION;
0226 #if 0 && __has_include(<compare>)
0227 std::strong_ordering operator<=>(const QCborMap &other) const
0228 {
0229 int c = compare(other);
0230 if (c > 0) return std::strong_ordering::greater;
0231 if (c == 0) return std::strong_ordering::equivalent;
0232 return std::strong_ordering::less;
0233 }
0234 #else
0235 bool operator==(const QCborMap &other) const noexcept
0236 { return compare(other) == 0; }
0237 bool operator!=(const QCborMap &other) const noexcept
0238 { return !(*this == other); }
0239 bool operator<(const QCborMap &other) const
0240 { return compare(other) < 0; }
0241 #endif
0242
0243 typedef Iterator iterator;
0244 typedef ConstIterator const_iterator;
0245 iterator begin() { detach(); return iterator{d.data(), 1}; }
0246 const_iterator constBegin() const { return const_iterator{d.data(), 1}; }
0247 const_iterator begin() const { return constBegin(); }
0248 const_iterator cbegin() const { return constBegin(); }
0249 iterator end() { detach(); return iterator{d.data(), 2 * size() + 1}; }
0250 const_iterator constEnd() const { return const_iterator{d.data(), 2 * size() + 1}; }
0251 const_iterator end() const { return constEnd(); }
0252 const_iterator cend() const { return constEnd(); }
0253 iterator erase(iterator it);
0254 iterator erase(const_iterator it) { return erase(iterator{ it.item.d, it.item.i }); }
0255 QCborValue extract(iterator it);
0256 QCborValue extract(const_iterator it) { return extract(iterator{ it.item.d, it.item.i }); }
0257 bool empty() const { return isEmpty(); }
0258
0259 iterator find(qint64 key);
0260 iterator find(QLatin1StringView key);
0261 iterator find(const QString & key);
0262 iterator find(const QCborValue &key);
0263 const_iterator constFind(qint64 key) const;
0264 const_iterator constFind(QLatin1StringView key) const;
0265 const_iterator constFind(const QString & key) const;
0266 const_iterator constFind(const QCborValue &key) const;
0267 const_iterator find(qint64 key) const { return constFind(key); }
0268 const_iterator find(QLatin1StringView key) const { return constFind(key); }
0269 const_iterator find(const QString & key) const { return constFind(key); }
0270 const_iterator find(const QCborValue &key) const { return constFind(key); }
0271
0272 iterator insert(qint64 key, const QCborValue &value_)
0273 {
0274 QCborValueRef v = operator[](key);
0275 v = value_;
0276 return { d.data(), v.i };
0277 }
0278 iterator insert(QLatin1StringView key, const QCborValue &value_)
0279 {
0280 QCborValueRef v = operator[](key);
0281 v = value_;
0282 return { d.data(), v.i };
0283 }
0284 iterator insert(const QString &key, const QCborValue &value_)
0285 {
0286 QCborValueRef v = operator[](key);
0287 v = value_;
0288 return { d.data(), v.i };
0289 }
0290 iterator insert(const QCborValue &key, const QCborValue &value_)
0291 {
0292 QCborValueRef v = operator[](key);
0293 v = value_;
0294 return { d.data(), v.i };
0295 }
0296 iterator insert(value_type v) { return insert(v.first, v.second); }
0297
0298 static QCborMap fromVariantMap(const QVariantMap &map);
0299 static QCborMap fromVariantHash(const QVariantHash &hash);
0300 static QCborMap fromJsonObject(const QJsonObject &o);
0301 static QCborMap fromJsonObject(QJsonObject &&o) noexcept;
0302 QVariantMap toVariantMap() const;
0303 QVariantHash toVariantHash() const;
0304 QJsonObject toJsonObject() const;
0305
0306 private:
0307 friend class QCborContainerPrivate;
0308 friend class QCborValue;
0309 friend class QCborValueRef;
0310 friend class QJsonPrivate::Variant;
0311 void detach(qsizetype reserve = 0);
0312
0313 explicit QCborMap(QCborContainerPrivate &dd) noexcept;
0314 QExplicitlySharedDataPointer<QCborContainerPrivate> d;
0315 };
0316
0317 Q_DECLARE_SHARED(QCborMap)
0318
0319 inline QCborValue::QCborValue(QCborMap &&m)
0320 : n(-1), container(m.d.take()), t(Map)
0321 {
0322 }
0323
0324 #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED)
0325 inline QCborMap QCborValueRef::toMap() const
0326 {
0327 return concrete().toMap();
0328 }
0329
0330 inline QCborMap QCborValueRef::toMap(const QCborMap &m) const
0331 {
0332 return concrete().toMap(m);
0333 }
0334 #endif
0335
0336 inline QCborMap QCborValueConstRef::toMap() const
0337 {
0338 return concrete().toMap();
0339 }
0340
0341 inline QCborMap QCborValueConstRef::toMap(const QCborMap &m) const
0342 {
0343 return concrete().toMap(m);
0344 }
0345
0346 Q_CORE_EXPORT size_t qHash(const QCborMap &map, size_t seed = 0);
0347
0348 #if !defined(QT_NO_DEBUG_STREAM)
0349 Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborMap &m);
0350 #endif
0351
0352 #ifndef QT_NO_DATASTREAM
0353 #if QT_CONFIG(cborstreamwriter)
0354 Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborMap &);
0355 #endif
0356 Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborMap &);
0357 #endif
0358
0359
0360 QT_END_NAMESPACE
0361
0362 #endif