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