| 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 QCBORVALUE_H |
| 6 | #define QCBORVALUE_H |
| 7 | |
| 8 | #include <QtCore/qbytearray.h> |
| 9 | #include <QtCore/qcborcommon.h> |
| 10 | #include <QtCore/qcompare.h> |
| 11 | #include <QtCore/qdatetime.h> |
| 12 | #if QT_CONFIG(regularexpression) |
| 13 | # include <QtCore/qregularexpression.h> |
| 14 | #endif |
| 15 | #include <QtCore/qstring.h> |
| 16 | #include <QtCore/qstringview.h> |
| 17 | #include <QtCore/qurl.h> |
| 18 | #include <QtCore/quuid.h> |
| 19 | #include <QtCore/qvariant.h> |
| 20 | |
| 21 | /* X11 headers use these values too, but as defines */ |
| 22 | #if defined(False) && defined(True) |
| 23 | # undef True |
| 24 | # undef False |
| 25 | #endif |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | class QCborArray; |
| 30 | class QCborMap; |
| 31 | class QCborStreamReader; |
| 32 | class QCborStreamWriter; |
| 33 | class QDataStream; |
| 34 | |
| 35 | namespace QJsonPrivate { class Value; } |
| 36 | |
| 37 | struct QCborParserError |
| 38 | { |
| 39 | qint64 offset = 0; |
| 40 | QCborError error = { .c: QCborError::NoError }; |
| 41 | |
| 42 | QString errorString() const { return error.toString(); } |
| 43 | }; |
| 44 | |
| 45 | class QCborValueRef; |
| 46 | class QCborContainerPrivate; |
| 47 | class Q_CORE_EXPORT QCborValue |
| 48 | { |
| 49 | Q_GADGET |
| 50 | public: |
| 51 | enum EncodingOption { |
| 52 | SortKeysInMaps = 0x01, |
| 53 | UseFloat = 0x02, |
| 54 | #ifndef QT_BOOTSTRAPPED |
| 55 | UseFloat16 = UseFloat | 0x04, |
| 56 | #endif |
| 57 | UseIntegers = 0x08, |
| 58 | |
| 59 | NoTransformation = 0 |
| 60 | }; |
| 61 | Q_DECLARE_FLAGS(EncodingOptions, EncodingOption) |
| 62 | |
| 63 | enum DiagnosticNotationOption { |
| 64 | Compact = 0x00, |
| 65 | LineWrapped = 0x01, |
| 66 | ExtendedFormat = 0x02 |
| 67 | }; |
| 68 | Q_DECLARE_FLAGS(DiagnosticNotationOptions, DiagnosticNotationOption) |
| 69 | |
| 70 | // different from QCborStreamReader::Type because we have more types |
| 71 | enum Type : int { |
| 72 | Integer = 0x00, |
| 73 | ByteArray = 0x40, |
| 74 | String = 0x60, |
| 75 | Array = 0x80, |
| 76 | Map = 0xa0, |
| 77 | Tag = 0xc0, |
| 78 | |
| 79 | // range 0x100 - 0x1ff for Simple Types |
| 80 | SimpleType = 0x100, |
| 81 | False = SimpleType + int(QCborSimpleType::False), |
| 82 | True = SimpleType + int(QCborSimpleType::True), |
| 83 | Null = SimpleType + int(QCborSimpleType::Null), |
| 84 | Undefined = SimpleType + int(QCborSimpleType::Undefined), |
| 85 | |
| 86 | Double = 0x202, |
| 87 | |
| 88 | // extended (tagged) types |
| 89 | DateTime = 0x10000, |
| 90 | Url = 0x10020, |
| 91 | RegularExpression = 0x10023, |
| 92 | Uuid = 0x10025, |
| 93 | |
| 94 | Invalid = -1 |
| 95 | }; |
| 96 | Q_ENUM(Type) |
| 97 | |
| 98 | QCborValue() {} |
| 99 | QCborValue(Type t_) : t(t_) {} |
| 100 | QCborValue(std::nullptr_t) : t(Null) {} |
| 101 | QCborValue(bool b_) : t(b_ ? True : False) {} |
| 102 | #ifndef Q_QDOC |
| 103 | QCborValue(int i) : QCborValue(qint64(i)) {} |
| 104 | QCborValue(unsigned u) : QCborValue(qint64(u)) {} |
| 105 | #endif |
| 106 | QCborValue(qint64 i) : n(i), t(Integer) {} |
| 107 | QCborValue(double v) : t(Double) { memcpy(dest: &n, src: &v, n: sizeof(n)); } |
| 108 | QCborValue(QCborSimpleType st) : t(type_helper(st)) {} |
| 109 | |
| 110 | QCborValue(const QByteArray &ba); |
| 111 | QCborValue(const QString &s); |
| 112 | QCborValue(QStringView s); |
| 113 | QCborValue(QLatin1StringView s); |
| 114 | #ifndef QT_NO_CAST_FROM_ASCII |
| 115 | QT_ASCII_CAST_WARN QCborValue(const char *s) : QCborValue(QString::fromUtf8(s)) {} |
| 116 | #endif |
| 117 | QCborValue(const QCborArray &a); |
| 118 | QCborValue(QCborArray &&a); |
| 119 | QCborValue(const QCborMap &m); |
| 120 | QCborValue(QCborMap &&m); |
| 121 | QCborValue(QCborTag tag, const QCborValue &taggedValue = QCborValue()); |
| 122 | QCborValue(QCborKnownTags t_, const QCborValue &tv = QCborValue()) |
| 123 | : QCborValue(QCborTag(t_), tv) |
| 124 | {} |
| 125 | |
| 126 | #if QT_CONFIG(datestring) |
| 127 | explicit QCborValue(const QDateTime &dt); |
| 128 | #endif |
| 129 | #ifndef QT_BOOTSTRAPPED |
| 130 | explicit QCborValue(const QUrl &url); |
| 131 | # if QT_CONFIG(regularexpression) |
| 132 | explicit QCborValue(const QRegularExpression &rx); |
| 133 | # endif |
| 134 | explicit QCborValue(const QUuid &uuid); |
| 135 | #endif |
| 136 | |
| 137 | ~QCborValue() { if (container) dispose(); } |
| 138 | |
| 139 | // make sure const char* doesn't go call the bool constructor |
| 140 | QCborValue(const void *) = delete; |
| 141 | |
| 142 | QCborValue(const QCborValue &other) noexcept; |
| 143 | QCborValue(QCborValue &&other) noexcept |
| 144 | : n(other.n), container(std::exchange(obj&: other.container, new_val: nullptr)), t(std::exchange(obj&: other.t, new_val: Undefined)) |
| 145 | { |
| 146 | } |
| 147 | QCborValue &operator=(const QCborValue &other) noexcept; |
| 148 | QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_MOVE_AND_SWAP(QCborValue) |
| 149 | |
| 150 | void swap(QCborValue &other) noexcept |
| 151 | { |
| 152 | std::swap(a&: n, b&: other.n); |
| 153 | qt_ptr_swap(lhs&: container, rhs&: other.container); |
| 154 | std::swap(a&: t, b&: other.t); |
| 155 | } |
| 156 | |
| 157 | Type type() const { return t; } |
| 158 | bool isInteger() const { return type() == Integer; } |
| 159 | bool isByteArray() const { return type() == ByteArray; } |
| 160 | bool isString() const { return type() == String; } |
| 161 | bool isArray() const { return type() == Array; } |
| 162 | bool isMap() const { return type() == Map; } |
| 163 | bool isTag() const { return isTag_helper(tt: type()); } |
| 164 | bool isFalse() const { return type() == False; } |
| 165 | bool isTrue() const { return type() == True; } |
| 166 | bool isBool() const { return isFalse() || isTrue(); } |
| 167 | bool isNull() const { return type() == Null; } |
| 168 | bool isUndefined() const { return type() == Undefined; } |
| 169 | bool isDouble() const { return type() == Double; } |
| 170 | bool isDateTime() const { return type() == DateTime; } |
| 171 | bool isUrl() const { return type() == Url; } |
| 172 | bool isRegularExpression() const { return type() == RegularExpression; } |
| 173 | bool isUuid() const { return type() == Uuid; } |
| 174 | bool isInvalid() const { return type() == Invalid; } |
| 175 | bool isContainer() const { return isMap() || isArray(); } |
| 176 | |
| 177 | bool isSimpleType() const |
| 178 | { |
| 179 | return int(type()) >> 8 == int(SimpleType) >> 8; |
| 180 | } |
| 181 | bool isSimpleType(QCborSimpleType st) const |
| 182 | { |
| 183 | return type() == type_helper(st); |
| 184 | } |
| 185 | QCborSimpleType toSimpleType(QCborSimpleType defaultValue = QCborSimpleType::Undefined) const |
| 186 | { |
| 187 | return isSimpleType() ? QCborSimpleType(type() & 0xff) : defaultValue; |
| 188 | } |
| 189 | |
| 190 | qint64 toInteger(qint64 defaultValue = 0) const |
| 191 | { return isInteger() ? value_helper() : isDouble() ? qint64(fp_helper()) : defaultValue; } |
| 192 | bool toBool(bool defaultValue = false) const |
| 193 | { return isBool() ? isTrue() : defaultValue; } |
| 194 | double toDouble(double defaultValue = 0) const |
| 195 | { return isDouble() ? fp_helper() : isInteger() ? double(value_helper()) : defaultValue; } |
| 196 | |
| 197 | QCborTag tag(QCborTag defaultValue = QCborTag(-1)) const; |
| 198 | QCborValue taggedValue(const QCborValue &defaultValue = QCborValue()) const; |
| 199 | |
| 200 | QByteArray toByteArray(const QByteArray &defaultValue = {}) const; |
| 201 | QString toString(const QString &defaultValue = {}) const; |
| 202 | QAnyStringView toStringView(QAnyStringView defaultValue = {}) const; |
| 203 | #if QT_CONFIG(datestring) |
| 204 | QDateTime toDateTime(const QDateTime &defaultValue = {}) const; |
| 205 | #endif |
| 206 | #ifndef QT_BOOTSTRAPPED |
| 207 | QUrl toUrl(const QUrl &defaultValue = {}) const; |
| 208 | # if QT_CONFIG(regularexpression) |
| 209 | QRegularExpression toRegularExpression(const QRegularExpression &defaultValue = {}) const; |
| 210 | # endif |
| 211 | QUuid toUuid(const QUuid &defaultValue = {}) const; |
| 212 | #endif |
| 213 | |
| 214 | // only forward-declared, need split functions |
| 215 | QCborArray toArray() const; |
| 216 | QCborArray toArray(const QCborArray &defaultValue) const; |
| 217 | QCborMap toMap() const; |
| 218 | QCborMap toMap(const QCborMap &defaultValue) const; |
| 219 | |
| 220 | const QCborValue operator[](const QString &key) const; |
| 221 | const QCborValue operator[](QLatin1StringView key) const; |
| 222 | const QCborValue operator[](qint64 key) const; |
| 223 | QCborValueRef operator[](qint64 key); |
| 224 | QCborValueRef operator[](QLatin1StringView key); |
| 225 | QCborValueRef operator[](const QString & key); |
| 226 | |
| 227 | int compare(const QCborValue &other) const; |
| 228 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 229 | bool operator==(const QCborValue &other) const noexcept |
| 230 | { return compare(other) == 0; } |
| 231 | bool operator!=(const QCborValue &other) const noexcept |
| 232 | { return !operator==(other); } |
| 233 | bool operator<(const QCborValue &other) const |
| 234 | { return compare(other) < 0; } |
| 235 | #endif |
| 236 | |
| 237 | static QCborValue fromVariant(const QVariant &variant); |
| 238 | QVariant toVariant() const; |
| 239 | static QCborValue fromJsonValue(const QJsonValue &v); |
| 240 | QJsonValue toJsonValue() const; |
| 241 | |
| 242 | #if QT_CONFIG(cborstreamreader) |
| 243 | static QCborValue fromCbor(QCborStreamReader &reader); |
| 244 | static QCborValue fromCbor(const QByteArray &ba, QCborParserError *error = nullptr); |
| 245 | static QCborValue fromCbor(const char *data, qsizetype len, QCborParserError *error = nullptr) |
| 246 | { return fromCbor(ba: QByteArray(data, int(len)), error); } |
| 247 | static QCborValue fromCbor(const quint8 *data, qsizetype len, QCborParserError *error = nullptr) |
| 248 | { return fromCbor(ba: QByteArray(reinterpret_cast<const char *>(data), int(len)), error); } |
| 249 | #endif // QT_CONFIG(cborstreamreader) |
| 250 | #if QT_CONFIG(cborstreamwriter) |
| 251 | QByteArray toCbor(EncodingOptions opt = NoTransformation) const; |
| 252 | void toCbor(QCborStreamWriter &writer, EncodingOptions opt = NoTransformation) const; |
| 253 | #endif |
| 254 | |
| 255 | QString toDiagnosticNotation(DiagnosticNotationOptions opts = Compact) const; |
| 256 | |
| 257 | private: |
| 258 | friend Q_CORE_EXPORT Q_DECL_PURE_FUNCTION |
| 259 | bool comparesEqual(const QCborValue &lhs, const QCborValue &rhs) noexcept; |
| 260 | friend Qt::strong_ordering compareThreeWay(const QCborValue &lhs, |
| 261 | const QCborValue &rhs) noexcept |
| 262 | { |
| 263 | int c = lhs.compare(other: rhs); |
| 264 | return Qt::compareThreeWay(lhs: c, rhs: 0); |
| 265 | } |
| 266 | |
| 267 | Q_DECLARE_STRONGLY_ORDERED(QCborValue) |
| 268 | friend class QCborArray; |
| 269 | friend class QCborMap; |
| 270 | friend class QCborValueConstRef; |
| 271 | friend class QCborValueRef; |
| 272 | friend class QCborContainerPrivate; |
| 273 | friend class QJsonPrivate::Value; |
| 274 | |
| 275 | qint64 n = 0; |
| 276 | QCborContainerPrivate *container = nullptr; |
| 277 | Type t = Undefined; |
| 278 | |
| 279 | void dispose(); |
| 280 | qint64 value_helper() const |
| 281 | { |
| 282 | return n; |
| 283 | } |
| 284 | |
| 285 | double fp_helper() const |
| 286 | { |
| 287 | static_assert(sizeof(double) == sizeof(n)); |
| 288 | double d; |
| 289 | memcpy(dest: &d, src: &n, n: sizeof(d)); |
| 290 | return d; |
| 291 | } |
| 292 | |
| 293 | constexpr static Type type_helper(QCborSimpleType st) |
| 294 | { |
| 295 | return Type(quint8(st) | SimpleType); |
| 296 | } |
| 297 | |
| 298 | constexpr static bool isTag_helper(Type tt) |
| 299 | { |
| 300 | return tt == Tag || tt >= 0x10000; |
| 301 | } |
| 302 | }; |
| 303 | Q_DECLARE_SHARED(QCborValue) |
| 304 | |
| 305 | class QCborValueConstRef |
| 306 | { |
| 307 | public: |
| 308 | QCborValueConstRef(const QCborValueConstRef &) = default; |
| 309 | QCborValueConstRef &operator=(const QCborValueConstRef &) = delete; |
| 310 | operator QCborValue() const { return concrete(); } |
| 311 | |
| 312 | QCborValue::Type type() const { return concreteType(that: *this); } |
| 313 | bool isInteger() const { return type() == QCborValue::Integer; } |
| 314 | bool isByteArray() const { return type() == QCborValue::ByteArray; } |
| 315 | bool isString() const { return type() == QCborValue::String; } |
| 316 | bool isArray() const { return type() == QCborValue::Array; } |
| 317 | bool isMap() const { return type() == QCborValue::Map; } |
| 318 | bool isTag() const { return concrete().isTag(); } |
| 319 | bool isFalse() const { return type() == QCborValue::False; } |
| 320 | bool isTrue() const { return type() == QCborValue::True; } |
| 321 | bool isBool() const { return isFalse() || isTrue(); } |
| 322 | bool isNull() const { return type() == QCborValue::Null; } |
| 323 | bool isUndefined() const { return type() == QCborValue::Undefined; } |
| 324 | bool isDouble() const { return type() == QCborValue::Double; } |
| 325 | bool isDateTime() const { return type() == QCborValue::DateTime; } |
| 326 | bool isUrl() const { return type() == QCborValue::Url; } |
| 327 | bool isRegularExpression() const { return type() == QCborValue::RegularExpression; } |
| 328 | bool isUuid() const { return type() == QCborValue::Uuid; } |
| 329 | bool isInvalid() const { return type() == QCborValue::Invalid; } |
| 330 | bool isContainer() const { return isMap() || isArray(); } |
| 331 | bool isSimpleType() const { return concrete().isSimpleType(); } |
| 332 | bool isSimpleType(QCborSimpleType st) const { return concrete().isSimpleType(st); } |
| 333 | |
| 334 | QCborSimpleType toSimpleType(QCborSimpleType defaultValue = QCborSimpleType::Undefined) const |
| 335 | { |
| 336 | return concrete().toSimpleType(defaultValue); |
| 337 | } |
| 338 | |
| 339 | QCborTag tag(QCborTag defaultValue = QCborTag(-1)) const |
| 340 | { return concrete().tag(defaultValue); } |
| 341 | QCborValue taggedValue(const QCborValue &defaultValue = QCborValue()) const |
| 342 | { return concrete().taggedValue(defaultValue); } |
| 343 | |
| 344 | qint64 toInteger(qint64 defaultValue = 0) const |
| 345 | { return concrete().toInteger(defaultValue); } |
| 346 | bool toBool(bool defaultValue = false) const |
| 347 | { return concrete().toBool(defaultValue); } |
| 348 | double toDouble(double defaultValue = 0) const |
| 349 | { return concrete().toDouble(defaultValue); } |
| 350 | |
| 351 | QByteArray toByteArray(const QByteArray &defaultValue = {}) const |
| 352 | { return concrete().toByteArray(defaultValue); } |
| 353 | QString toString(const QString &defaultValue = {}) const |
| 354 | { return concrete().toString(defaultValue); } |
| 355 | QAnyStringView toStringView(QAnyStringView defaultValue = {}) const |
| 356 | { return concreteStringView(that: *this, defaultValue); } |
| 357 | #if QT_CONFIG(datestring) |
| 358 | QDateTime toDateTime(const QDateTime &defaultValue = {}) const |
| 359 | { return concrete().toDateTime(defaultValue); } |
| 360 | #endif |
| 361 | #ifndef QT_BOOTSTRAPPED |
| 362 | QUrl toUrl(const QUrl &defaultValue = {}) const |
| 363 | { return concrete().toUrl(defaultValue); } |
| 364 | # if QT_CONFIG(regularexpression) |
| 365 | QRegularExpression toRegularExpression(const QRegularExpression &defaultValue = {}) const |
| 366 | { return concrete().toRegularExpression(defaultValue); } |
| 367 | # endif |
| 368 | QUuid toUuid(const QUuid &defaultValue = {}) const |
| 369 | { return concrete().toUuid(defaultValue); } |
| 370 | #endif |
| 371 | |
| 372 | // only forward-declared, need split functions. Implemented in qcbor{array,map}.h |
| 373 | inline QCborArray toArray() const; |
| 374 | inline QCborArray toArray(const QCborArray &a) const; |
| 375 | inline QCborMap toMap() const; |
| 376 | inline QCborMap toMap(const QCborMap &m) const; |
| 377 | |
| 378 | Q_CORE_EXPORT const QCborValue operator[](const QString &key) const; |
| 379 | Q_CORE_EXPORT const QCborValue operator[](QLatin1StringView key) const; |
| 380 | Q_CORE_EXPORT const QCborValue operator[](qint64 key) const; |
| 381 | |
| 382 | int compare(const QCborValue &other) const |
| 383 | { return concrete().compare(other); } |
| 384 | |
| 385 | QVariant toVariant() const { return concrete().toVariant(); } |
| 386 | inline QJsonValue toJsonValue() const; // in qjsonvalue.h |
| 387 | |
| 388 | #if QT_CONFIG(cborstreamwriter) |
| 389 | QByteArray toCbor(QCborValue::EncodingOptions opt = QCborValue::NoTransformation) const |
| 390 | { return concrete().toCbor(opt); } |
| 391 | void toCbor(QCborStreamWriter &writer, QCborValue::EncodingOptions opt = QCborValue::NoTransformation) const |
| 392 | { return concrete().toCbor(writer, opt); } |
| 393 | #endif |
| 394 | |
| 395 | QString toDiagnosticNotation(QCborValue::DiagnosticNotationOptions opt = QCborValue::Compact) const |
| 396 | { return concrete().toDiagnosticNotation(opts: opt); } |
| 397 | |
| 398 | protected: |
| 399 | friend class QCborValue; |
| 400 | friend class QCborArray; |
| 401 | friend class QCborMap; |
| 402 | friend class QCborContainerPrivate; |
| 403 | |
| 404 | QCborValue concrete() const noexcept { return concrete(that: *this); } |
| 405 | static Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool |
| 406 | comparesEqual_helper(QCborValueConstRef lhs, QCborValueConstRef rhs) noexcept; |
| 407 | static Q_CORE_EXPORT Q_DECL_PURE_FUNCTION Qt::strong_ordering |
| 408 | compareThreeWay_helper(QCborValueConstRef lhs, QCborValueConstRef rhs) noexcept; |
| 409 | friend bool comparesEqual(const QCborValueConstRef &lhs, |
| 410 | const QCborValueConstRef &rhs) noexcept |
| 411 | { |
| 412 | return comparesEqual_helper(lhs, rhs); |
| 413 | } |
| 414 | friend Qt::strong_ordering compareThreeWay(const QCborValueConstRef &lhs, |
| 415 | const QCborValueConstRef &rhs) noexcept |
| 416 | { |
| 417 | return compareThreeWay_helper(lhs, rhs); |
| 418 | } |
| 419 | Q_DECLARE_STRONGLY_ORDERED(QCborValueConstRef) |
| 420 | |
| 421 | static Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool |
| 422 | comparesEqual_helper(QCborValueConstRef lhs, const QCborValue &rhs) noexcept; |
| 423 | static Q_CORE_EXPORT Q_DECL_PURE_FUNCTION Qt::strong_ordering |
| 424 | compareThreeWay_helper(QCborValueConstRef lhs, const QCborValue &rhs) noexcept; |
| 425 | friend bool comparesEqual(const QCborValueConstRef &lhs, |
| 426 | const QCborValue &rhs) noexcept |
| 427 | { |
| 428 | return comparesEqual_helper(lhs, rhs); |
| 429 | } |
| 430 | friend Qt::strong_ordering compareThreeWay(const QCborValueConstRef &lhs, |
| 431 | const QCborValue &rhs) noexcept |
| 432 | { |
| 433 | return compareThreeWay_helper(lhs, rhs); |
| 434 | } |
| 435 | Q_DECLARE_STRONGLY_ORDERED(QCborValueConstRef, QCborValue) |
| 436 | |
| 437 | static Q_CORE_EXPORT QCborValue concrete(QCborValueConstRef that) noexcept; |
| 438 | static Q_CORE_EXPORT QCborValue::Type concreteType(QCborValueConstRef that) noexcept Q_DECL_PURE_FUNCTION; |
| 439 | static Q_CORE_EXPORT bool |
| 440 | concreteBoolean(QCborValueConstRef that, bool defaultValue) noexcept Q_DECL_PURE_FUNCTION; |
| 441 | static Q_CORE_EXPORT double |
| 442 | concreteDouble(QCborValueConstRef that, double defaultValue) noexcept Q_DECL_PURE_FUNCTION; |
| 443 | static Q_CORE_EXPORT qint64 |
| 444 | concreteIntegral(QCborValueConstRef that, qint64 defaultValue) noexcept Q_DECL_PURE_FUNCTION; |
| 445 | static Q_CORE_EXPORT QByteArray |
| 446 | concreteByteArray(QCborValueConstRef that, const QByteArray &defaultValue); |
| 447 | static Q_CORE_EXPORT QString |
| 448 | concreteString(QCborValueConstRef that, const QString &defaultValue); |
| 449 | static Q_CORE_EXPORT QAnyStringView |
| 450 | concreteStringView(QCborValueConstRef that, QAnyStringView defaultValue); |
| 451 | |
| 452 | constexpr QCborValueConstRef() : d(nullptr), i(0) {} // this will actually be invalid |
| 453 | constexpr QCborValueConstRef(QCborContainerPrivate *dd, qsizetype ii) |
| 454 | : d(dd), i(ii) |
| 455 | {} |
| 456 | QCborContainerPrivate *d; |
| 457 | qsizetype i; |
| 458 | }; |
| 459 | |
| 460 | QT_WARNING_PUSH |
| 461 | QT6_ONLY(QT_WARNING_DISABLE_MSVC(4275)) // non dll-interface class 'QJsonValueConstRef' used as base for dll-interface class 'QJsonValueRef' |
| 462 | class QT6_ONLY(Q_CORE_EXPORT) QCborValueRef : public QCborValueConstRef |
| 463 | { |
| 464 | public: |
| 465 | QCborValueRef(const QCborValueRef &) noexcept = default; |
| 466 | QCborValueRef(QCborValueRef &&) noexcept = default; |
| 467 | QCborValueRef &operator=(const QCborValue &other) |
| 468 | { assign(that: *this, other); return *this; } |
| 469 | QCborValueRef &operator=(QCborValue &&other) |
| 470 | { assign(that: *this, other: std::move(other)); other.container = nullptr; return *this; } |
| 471 | QCborValueRef &operator=(const QCborValueRef &other) |
| 472 | { assign(that: *this, other); return *this; } |
| 473 | |
| 474 | QT7_ONLY(Q_CORE_EXPORT) QCborValueRef operator[](qint64 key); |
| 475 | QT7_ONLY(Q_CORE_EXPORT) QCborValueRef operator[](QLatin1StringView key); |
| 476 | QT7_ONLY(Q_CORE_EXPORT) QCborValueRef operator[](const QString & key); |
| 477 | |
| 478 | #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) |
| 479 | // retained for binary compatibility (due to the Q_CORE_EXPORT) because at |
| 480 | // least one compiler emits and exports all inlines in an exported class |
| 481 | |
| 482 | operator QCborValue() const { return concrete(); } |
| 483 | QCborValue::Type type() const { return concreteType(); } |
| 484 | bool isInteger() const { return type() == QCborValue::Integer; } |
| 485 | bool isByteArray() const { return type() == QCborValue::ByteArray; } |
| 486 | bool isString() const { return type() == QCborValue::String; } |
| 487 | bool isArray() const { return type() == QCborValue::Array; } |
| 488 | bool isMap() const { return type() == QCborValue::Map; } |
| 489 | bool isTag() const { return QCborValue::isTag_helper(tt: type()); } |
| 490 | bool isFalse() const { return type() == QCborValue::False; } |
| 491 | bool isTrue() const { return type() == QCborValue::True; } |
| 492 | bool isBool() const { return isFalse() || isTrue(); } |
| 493 | bool isNull() const { return type() == QCborValue::Null; } |
| 494 | bool isUndefined() const { return type() == QCborValue::Undefined; } |
| 495 | bool isDouble() const { return type() == QCborValue::Double; } |
| 496 | bool isDateTime() const { return type() == QCborValue::DateTime; } |
| 497 | bool isUrl() const { return type() == QCborValue::Url; } |
| 498 | bool isRegularExpression() const { return type() == QCborValue::RegularExpression; } |
| 499 | bool isUuid() const { return type() == QCborValue::Uuid; } |
| 500 | bool isInvalid() const { return type() == QCborValue::Invalid; } |
| 501 | bool isContainer() const { return isMap() || isArray(); } |
| 502 | bool isSimpleType() const |
| 503 | { |
| 504 | return type() >= QCborValue::SimpleType && type() < QCborValue::SimpleType + 0x100; |
| 505 | } |
| 506 | bool isSimpleType(QCborSimpleType st) const |
| 507 | { |
| 508 | return type() == QCborValue::type_helper(st); |
| 509 | } |
| 510 | QCborSimpleType toSimpleType(QCborSimpleType defaultValue = QCborSimpleType::Undefined) const |
| 511 | { |
| 512 | return isSimpleType() ? QCborSimpleType(type() & 0xff) : defaultValue; |
| 513 | } |
| 514 | |
| 515 | QCborTag tag(QCborTag defaultValue = QCborTag(-1)) const |
| 516 | { return concrete().tag(defaultValue); } |
| 517 | QCborValue taggedValue(const QCborValue &defaultValue = QCborValue()) const |
| 518 | { return concrete().taggedValue(defaultValue); } |
| 519 | |
| 520 | qint64 toInteger(qint64 defaultValue = 0) const |
| 521 | { return concreteIntegral(that: *this, defaultValue); } |
| 522 | bool toBool(bool defaultValue = false) const |
| 523 | { return concreteBoolean(that: *this, defaultValue); } |
| 524 | double toDouble(double defaultValue = 0) const |
| 525 | { return concreteDouble(that: *this, defaultValue); } |
| 526 | |
| 527 | QByteArray toByteArray(const QByteArray &defaultValue = {}) const |
| 528 | { return concreteByteArray(that: *this, defaultValue); } |
| 529 | QString toString(const QString &defaultValue = {}) const |
| 530 | { return concreteString(that: *this, defaultValue); } |
| 531 | #if QT_CONFIG(datestring) |
| 532 | QDateTime toDateTime(const QDateTime &defaultValue = {}) const |
| 533 | { return concrete().toDateTime(defaultValue); } |
| 534 | #endif |
| 535 | #ifndef QT_BOOTSTRAPPED |
| 536 | QUrl toUrl(const QUrl &defaultValue = {}) const |
| 537 | { return concrete().toUrl(defaultValue); } |
| 538 | # if QT_CONFIG(regularexpression) |
| 539 | QRegularExpression toRegularExpression(const QRegularExpression &defaultValue = {}) const |
| 540 | { return concrete().toRegularExpression(defaultValue); } |
| 541 | # endif |
| 542 | QUuid toUuid(const QUuid &defaultValue = {}) const |
| 543 | { return concrete().toUuid(defaultValue); } |
| 544 | #endif |
| 545 | |
| 546 | // only forward-declared, need split functions. Implemented in qcbor{array,map}.h |
| 547 | QCborArray toArray() const; |
| 548 | QCborArray toArray(const QCborArray &a) const; |
| 549 | QCborMap toMap() const; |
| 550 | QCborMap toMap(const QCborMap &m) const; |
| 551 | |
| 552 | const QCborValue operator[](const QString &key) const; |
| 553 | const QCborValue operator[](QLatin1StringView key) const; |
| 554 | const QCborValue operator[](qint64 key) const; |
| 555 | |
| 556 | int compare(const QCborValue &other) const |
| 557 | { return concrete().compare(other); } |
| 558 | #if QT_CORE_REMOVED_SINCE(6, 8) |
| 559 | bool operator==(const QCborValue &other) const |
| 560 | { return compare(other) == 0; } |
| 561 | bool operator!=(const QCborValue &other) const |
| 562 | { return !operator==(other); } |
| 563 | bool operator<(const QCborValue &other) const |
| 564 | { return compare(other) < 0; } |
| 565 | #endif |
| 566 | |
| 567 | QVariant toVariant() const { return concrete().toVariant(); } |
| 568 | QJsonValue toJsonValue() const; |
| 569 | |
| 570 | #if QT_CONFIG(cborstreamwriter) |
| 571 | using QCborValueConstRef::toCbor; |
| 572 | QByteArray toCbor(QCborValue::EncodingOptions opt = QCborValue::NoTransformation) |
| 573 | { return std::as_const(t&: *this).toCbor(opt); } |
| 574 | void toCbor(QCborStreamWriter &writer, QCborValue::EncodingOptions opt = QCborValue::NoTransformation); |
| 575 | #endif |
| 576 | |
| 577 | using QCborValueConstRef::toDiagnosticNotation; |
| 578 | QString toDiagnosticNotation(QCborValue::DiagnosticNotationOptions opt = QCborValue::Compact) |
| 579 | { return std::as_const(t&: *this).toDiagnosticNotation(opt); } |
| 580 | |
| 581 | private: |
| 582 | static QCborValue concrete(QCborValueRef that) noexcept; |
| 583 | QCborValue concrete() const noexcept { return concrete(that: *this); } |
| 584 | |
| 585 | static QCborValue::Type concreteType(QCborValueRef self) noexcept Q_DECL_PURE_FUNCTION; |
| 586 | QCborValue::Type concreteType() const noexcept { return concreteType(self: *this); } |
| 587 | |
| 588 | // this will actually be invalid... |
| 589 | constexpr QCborValueRef() : QCborValueConstRef(nullptr, 0) {} |
| 590 | |
| 591 | QCborValueRef(QCborContainerPrivate *dd, qsizetype ii) |
| 592 | : QCborValueConstRef(dd, ii) |
| 593 | {} |
| 594 | #else |
| 595 | private: |
| 596 | using QCborValueConstRef::QCborValueConstRef; |
| 597 | #endif // < Qt 7 |
| 598 | |
| 599 | friend class QCborValue; |
| 600 | friend class QCborArray; |
| 601 | friend class QCborMap; |
| 602 | friend class QCborContainerPrivate; |
| 603 | friend class QCborValueConstRef; |
| 604 | |
| 605 | // static so we can pass this by value |
| 606 | QT7_ONLY(Q_CORE_EXPORT) static void assign(QCborValueRef that, const QCborValue &other); |
| 607 | QT7_ONLY(Q_CORE_EXPORT) static void assign(QCborValueRef that, QCborValue &&other); |
| 608 | QT7_ONLY(Q_CORE_EXPORT) static void assign(QCborValueRef that, const QCborValueRef other); |
| 609 | }; |
| 610 | QT_WARNING_POP |
| 611 | Q_DECLARE_OPERATORS_FOR_FLAGS(QCborValue::EncodingOptions) |
| 612 | Q_DECLARE_OPERATORS_FOR_FLAGS(QCborValue::DiagnosticNotationOptions) |
| 613 | |
| 614 | Q_CORE_EXPORT size_t qHash(const QCborValue &value, size_t seed = 0); |
| 615 | |
| 616 | #if !defined(QT_NO_DEBUG_STREAM) |
| 617 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborValue &v); |
| 618 | #endif |
| 619 | |
| 620 | #ifndef QT_NO_DATASTREAM |
| 621 | #if QT_CONFIG(cborstreamwriter) |
| 622 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborValue &); |
| 623 | #endif |
| 624 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborValue &); |
| 625 | #endif |
| 626 | |
| 627 | QT_END_NAMESPACE |
| 628 | |
| 629 | #endif // QCBORVALUE_H |
| 630 | |