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

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of qtbase/src/corelib/serialization/qcborvalue.h