1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2018 Intel Corporation. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtCore module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #ifndef QCBORVALUE_H |
41 | #define QCBORVALUE_H |
42 | |
43 | #include <QtCore/qbytearray.h> |
44 | #include <QtCore/qdatetime.h> |
45 | #include <QtCore/qcborcommon.h> |
46 | #if QT_CONFIG(regularexpression) |
47 | # include <QtCore/qregularexpression.h> |
48 | #endif |
49 | #include <QtCore/qstring.h> |
50 | #include <QtCore/qstringview.h> |
51 | #include <QtCore/qurl.h> |
52 | #include <QtCore/quuid.h> |
53 | #include <QtCore/qvariant.h> |
54 | #include <QtCore/qvector.h> |
55 | |
56 | // See qcborcommon.h for why we check |
57 | #if defined(QT_X11_DEFINES_FOUND) |
58 | # undef True |
59 | # undef False |
60 | #endif |
61 | |
62 | #if 0 && __has_include(<compare>) |
63 | # include <compare> |
64 | #endif |
65 | |
66 | QT_BEGIN_NAMESPACE |
67 | |
68 | class QCborArray; |
69 | class QCborMap; |
70 | class QCborStreamReader; |
71 | class QCborStreamWriter; |
72 | class QDataStream; |
73 | |
74 | namespace QJsonPrivate { class Value; } |
75 | |
76 | struct QCborParserError |
77 | { |
78 | qint64 offset = 0; |
79 | QCborError error = { .c: QCborError::NoError }; |
80 | |
81 | QString errorString() const { return error.toString(); } |
82 | }; |
83 | |
84 | class QCborValueRef; |
85 | class QCborContainerPrivate; |
86 | class Q_CORE_EXPORT QCborValue |
87 | { |
88 | Q_GADGET |
89 | public: |
90 | enum EncodingOption { |
91 | SortKeysInMaps = 0x01, |
92 | UseFloat = 0x02, |
93 | #ifndef QT_BOOTSTRAPPED |
94 | UseFloat16 = UseFloat | 0x04, |
95 | #endif |
96 | UseIntegers = 0x08, |
97 | |
98 | NoTransformation = 0 |
99 | }; |
100 | Q_DECLARE_FLAGS(EncodingOptions, EncodingOption) |
101 | |
102 | enum DiagnosticNotationOption { |
103 | Compact = 0x00, |
104 | LineWrapped = 0x01, |
105 | ExtendedFormat = 0x02 |
106 | }; |
107 | Q_DECLARE_FLAGS(DiagnosticNotationOptions, DiagnosticNotationOption) |
108 | |
109 | // different from QCborStreamReader::Type because we have more types |
110 | enum Type : int { |
111 | Integer = 0x00, |
112 | ByteArray = 0x40, |
113 | String = 0x60, |
114 | Array = 0x80, |
115 | Map = 0xa0, |
116 | Tag = 0xc0, |
117 | |
118 | // range 0x100 - 0x1ff for Simple Types |
119 | SimpleType = 0x100, |
120 | False = SimpleType + int(QCborSimpleType::False), |
121 | True = SimpleType + int(QCborSimpleType::True), |
122 | Null = SimpleType + int(QCborSimpleType::Null), |
123 | Undefined = SimpleType + int(QCborSimpleType::Undefined), |
124 | |
125 | Double = 0x202, |
126 | |
127 | // extended (tagged) types |
128 | DateTime = 0x10000, |
129 | Url = 0x10020, |
130 | RegularExpression = 0x10023, |
131 | Uuid = 0x10025, |
132 | |
133 | Invalid = -1 |
134 | }; |
135 | Q_ENUM(Type) |
136 | |
137 | QCborValue() {} |
138 | QCborValue(Type t_) : t(t_) {} |
139 | QCborValue(std::nullptr_t) : t(Null) {} |
140 | QCborValue(bool b_) : t(b_ ? True : False) {} |
141 | #ifndef Q_QDOC |
142 | QCborValue(int i) : QCborValue(qint64(i)) {} |
143 | QCborValue(unsigned u) : QCborValue(qint64(u)) {} |
144 | #endif |
145 | QCborValue(qint64 i) : n(i), t(Integer) {} |
146 | QCborValue(double v) : t(Double) { memcpy(dest: &n, src: &v, n: sizeof(n)); } |
147 | QCborValue(QCborSimpleType st) : t(type_helper(st)) {} |
148 | |
149 | QCborValue(const QByteArray &ba); |
150 | #if QT_STRINGVIEW_LEVEL < 2 |
151 | QCborValue(const QString &s); |
152 | #endif |
153 | QCborValue(QStringView s); |
154 | QCborValue(QLatin1String s); |
155 | #ifndef QT_NO_CAST_FROM_ASCII |
156 | QT_ASCII_CAST_WARN QCborValue(const char *s) : QCborValue(QString::fromUtf8(str: s)) {} |
157 | #endif |
158 | QCborValue(const QCborArray &a); |
159 | QCborValue(QCborArray &&a); |
160 | QCborValue(const QCborMap &m); |
161 | QCborValue(QCborMap &&m); |
162 | QCborValue(QCborTag tag, const QCborValue &taggedValue = QCborValue()); |
163 | QCborValue(QCborKnownTags t_, const QCborValue &tv = QCborValue()) |
164 | : QCborValue(QCborTag(t_), tv) |
165 | {} |
166 | |
167 | explicit QCborValue(const QDateTime &dt); |
168 | #ifndef QT_BOOTSTRAPPED |
169 | explicit QCborValue(const QUrl &url); |
170 | #endif |
171 | #if QT_CONFIG(regularexpression) |
172 | explicit QCborValue(const QRegularExpression &rx); |
173 | #endif |
174 | explicit QCborValue(const QUuid &uuid); |
175 | |
176 | ~QCborValue() { if (container) dispose(); } |
177 | |
178 | // make sure const char* doesn't go call the bool constructor |
179 | QCborValue(const void *) = delete; |
180 | |
181 | QCborValue(const QCborValue &other); |
182 | QCborValue(QCborValue &&other) noexcept |
183 | : n(other.n), container(other.container), t(other.t) |
184 | { |
185 | other.t = Undefined; |
186 | other.container = nullptr; |
187 | } |
188 | QCborValue &operator=(const QCborValue &other); |
189 | QCborValue &operator=(QCborValue &&other) noexcept |
190 | { |
191 | QCborValue tmp(std::move(other)); |
192 | swap(other&: tmp); |
193 | return *this; |
194 | } |
195 | |
196 | void swap(QCborValue &other) noexcept |
197 | { |
198 | qSwap(value1&: n, value2&: other.n); |
199 | qSwap(value1&: container, value2&: other.container); |
200 | qSwap(value1&: t, value2&: other.t); |
201 | } |
202 | |
203 | Type type() const { return t; } |
204 | bool isInteger() const { return type() == Integer; } |
205 | bool isByteArray() const { return type() == ByteArray; } |
206 | bool isString() const { return type() == String; } |
207 | bool isArray() const { return type() == Array; } |
208 | bool isMap() const { return type() == Map; } |
209 | bool isTag() const { return isTag_helper(tt: type()); } |
210 | bool isFalse() const { return type() == False; } |
211 | bool isTrue() const { return type() == True; } |
212 | bool isBool() const { return isFalse() || isTrue(); } |
213 | bool isNull() const { return type() == Null; } |
214 | bool isUndefined() const { return type() == Undefined; } |
215 | bool isDouble() const { return type() == Double; } |
216 | bool isDateTime() const { return type() == DateTime; } |
217 | bool isUrl() const { return type() == Url; } |
218 | bool isRegularExpression() const { return type() == RegularExpression; } |
219 | bool isUuid() const { return type() == Uuid; } |
220 | bool isInvalid() const { return type() == Invalid; } |
221 | bool isContainer() const { return isMap() || isArray(); } |
222 | |
223 | bool isSimpleType() const |
224 | { |
225 | return int(type()) >> 8 == int(SimpleType) >> 8; |
226 | } |
227 | bool isSimpleType(QCborSimpleType st) const |
228 | { |
229 | return type() == type_helper(st); |
230 | } |
231 | QCborSimpleType toSimpleType(QCborSimpleType defaultValue = QCborSimpleType::Undefined) const |
232 | { |
233 | return isSimpleType() ? QCborSimpleType(type() & 0xff) : defaultValue; |
234 | } |
235 | |
236 | qint64 toInteger(qint64 defaultValue = 0) const |
237 | { return isInteger() ? value_helper() : isDouble() ? qint64(fp_helper()) : defaultValue; } |
238 | bool toBool(bool defaultValue = false) const |
239 | { return isBool() ? isTrue() : defaultValue; } |
240 | double toDouble(double defaultValue = 0) const |
241 | { return isDouble() ? fp_helper() : isInteger() ? double(value_helper()) : defaultValue; } |
242 | |
243 | QCborTag tag(QCborTag defaultValue = QCborTag(-1)) const; |
244 | QCborValue taggedValue(const QCborValue &defaultValue = QCborValue()) const; |
245 | |
246 | QByteArray toByteArray(const QByteArray &defaultValue = {}) const; |
247 | QString toString(const QString &defaultValue = {}) const; |
248 | QDateTime toDateTime(const QDateTime &defaultValue = {}) const; |
249 | QUrl toUrl(const QUrl &defaultValue = {}) const; |
250 | #if QT_CONFIG(regularexpression) |
251 | QRegularExpression toRegularExpression(const QRegularExpression &defaultValue = {}) const; |
252 | #endif |
253 | QUuid toUuid(const QUuid &defaultValue = {}) const; |
254 | |
255 | // only forward-declared, need split functions |
256 | QCborArray toArray() const; |
257 | QCborArray toArray(const QCborArray &defaultValue) const; |
258 | QCborMap toMap() const; |
259 | QCborMap toMap(const QCborMap &defaultValue) const; |
260 | |
261 | const QCborValue operator[](const QString &key) const; |
262 | const QCborValue operator[](QLatin1String key) const; |
263 | const QCborValue operator[](qint64 key) const; |
264 | QCborValueRef operator[](qint64 key); |
265 | QCborValueRef operator[](QLatin1String key); |
266 | QCborValueRef operator[](const QString & key); |
267 | |
268 | int compare(const QCborValue &other) const; |
269 | #if 0 && __has_include(<compare>) |
270 | std::strong_ordering operator<=>(const QCborValue &other) const |
271 | { |
272 | int c = compare(other); |
273 | if (c > 0) return std::partial_ordering::greater; |
274 | if (c == 0) return std::partial_ordering::equivalent; |
275 | return std::partial_ordering::less; |
276 | } |
277 | #else |
278 | bool operator==(const QCborValue &other) const noexcept |
279 | { return compare(other) == 0; } |
280 | bool operator!=(const QCborValue &other) const noexcept |
281 | { return !(*this == other); } |
282 | bool operator<(const QCborValue &other) const |
283 | { return compare(other) < 0; } |
284 | #endif |
285 | |
286 | static QCborValue fromVariant(const QVariant &variant); |
287 | QVariant toVariant() const; |
288 | static QCborValue fromJsonValue(const QJsonValue &v); |
289 | QJsonValue toJsonValue() const; |
290 | |
291 | #if QT_CONFIG(cborstreamreader) |
292 | static QCborValue fromCbor(QCborStreamReader &reader); |
293 | static QCborValue fromCbor(const QByteArray &ba, QCborParserError *error = nullptr); |
294 | static QCborValue fromCbor(const char *data, qsizetype len, QCborParserError *error = nullptr) |
295 | { return fromCbor(ba: QByteArray(data, int(len)), error); } |
296 | static QCborValue fromCbor(const quint8 *data, qsizetype len, QCborParserError *error = nullptr) |
297 | { return fromCbor(ba: QByteArray(reinterpret_cast<const char *>(data), int(len)), error); } |
298 | #endif // QT_CONFIG(cborstreamreader) |
299 | #if QT_CONFIG(cborstreamwriter) |
300 | QByteArray toCbor(EncodingOptions opt = NoTransformation); |
301 | void toCbor(QCborStreamWriter &writer, EncodingOptions opt = NoTransformation); |
302 | #endif |
303 | |
304 | QString toDiagnosticNotation(DiagnosticNotationOptions opts = Compact) const; |
305 | |
306 | private: |
307 | friend class QCborValueRef; |
308 | friend class QCborContainerPrivate; |
309 | friend class QJsonPrivate::Value; |
310 | |
311 | qint64 n = 0; |
312 | QCborContainerPrivate *container = nullptr; |
313 | Type t = Undefined; |
314 | |
315 | void dispose(); |
316 | qint64 value_helper() const |
317 | { |
318 | return n; |
319 | } |
320 | |
321 | double fp_helper() const |
322 | { |
323 | Q_STATIC_ASSERT(sizeof(double) == sizeof(n)); |
324 | double d; |
325 | memcpy(dest: &d, src: &n, n: sizeof(d)); |
326 | return d; |
327 | } |
328 | |
329 | Q_DECL_CONSTEXPR static Type type_helper(QCborSimpleType st) |
330 | { |
331 | return Type(quint8(st) | SimpleType); |
332 | } |
333 | |
334 | Q_DECL_CONSTEXPR static bool isTag_helper(Type tt) |
335 | { |
336 | return tt == Tag || tt >= 0x10000; |
337 | } |
338 | }; |
339 | Q_DECLARE_SHARED(QCborValue) |
340 | |
341 | class Q_CORE_EXPORT QCborValueRef |
342 | { |
343 | public: |
344 | operator QCborValue() const { return concrete(); } |
345 | |
346 | QCborValueRef(const QCborValueRef &) noexcept = default; |
347 | QCborValueRef(QCborValueRef &&) noexcept = default; |
348 | QCborValueRef &operator=(const QCborValue &other) |
349 | { assign(that: *this, other); return *this; } |
350 | QCborValueRef &operator=(QCborValue &&other) |
351 | { assign(that: *this, other: std::move(other)); other.container = nullptr; return *this; } |
352 | QCborValueRef &operator=(const QCborValueRef &other) |
353 | { assign(that: *this, other); return *this; } |
354 | |
355 | QCborValue::Type type() const { return concreteType(); } |
356 | bool isInteger() const { return type() == QCborValue::Integer; } |
357 | bool isByteArray() const { return type() == QCborValue::ByteArray; } |
358 | bool isString() const { return type() == QCborValue::String; } |
359 | bool isArray() const { return type() == QCborValue::Array; } |
360 | bool isMap() const { return type() == QCborValue::Map; } |
361 | bool isTag() const { return QCborValue::isTag_helper(tt: type()); } |
362 | bool isFalse() const { return type() == QCborValue::False; } |
363 | bool isTrue() const { return type() == QCborValue::True; } |
364 | bool isBool() const { return isFalse() || isTrue(); } |
365 | bool isNull() const { return type() == QCborValue::Null; } |
366 | bool isUndefined() const { return type() == QCborValue::Undefined; } |
367 | bool isDouble() const { return type() == QCborValue::Double; } |
368 | bool isDateTime() const { return type() == QCborValue::DateTime; } |
369 | bool isUrl() const { return type() == QCborValue::Url; } |
370 | bool isRegularExpression() const { return type() == QCborValue::RegularExpression; } |
371 | bool isUuid() const { return type() == QCborValue::Uuid; } |
372 | bool isInvalid() const { return type() == QCborValue::Invalid; } |
373 | bool isContainer() const { return isMap() || isArray(); } |
374 | bool isSimpleType() const |
375 | { |
376 | return type() >= QCborValue::SimpleType && type() < QCborValue::SimpleType + 0x100; |
377 | } |
378 | bool isSimpleType(QCborSimpleType st) const |
379 | { |
380 | return type() == QCborValue::type_helper(st); |
381 | } |
382 | |
383 | QCborTag tag(QCborTag defaultValue = QCborTag(-1)) const |
384 | { return concrete().tag(defaultValue); } |
385 | QCborValue taggedValue(const QCborValue &defaultValue = QCborValue()) const |
386 | { return concrete().taggedValue(defaultValue); } |
387 | |
388 | qint64 toInteger(qint64 defaultValue = 0) const |
389 | { return concrete().toInteger(defaultValue); } |
390 | bool toBool(bool defaultValue = false) const |
391 | { return concrete().toBool(defaultValue); } |
392 | double toDouble(double defaultValue = 0) const |
393 | { return concrete().toDouble(defaultValue); } |
394 | |
395 | QByteArray toByteArray(const QByteArray &defaultValue = {}) const |
396 | { return concrete().toByteArray(defaultValue); } |
397 | QString toString(const QString &defaultValue = {}) const |
398 | { return concrete().toString(defaultValue); } |
399 | QDateTime toDateTime(const QDateTime &defaultValue = {}) const |
400 | { return concrete().toDateTime(defaultValue); } |
401 | #ifndef QT_BOOTSTRAPPED |
402 | QUrl toUrl(const QUrl &defaultValue = {}) const |
403 | { return concrete().toUrl(defaultValue); } |
404 | #endif |
405 | #if QT_CONFIG(regularexpression) |
406 | QRegularExpression toRegularExpression(const QRegularExpression &defaultValue = {}) const |
407 | { return concrete().toRegularExpression(defaultValue); } |
408 | #endif |
409 | QUuid toUuid(const QUuid &defaultValue = {}) const |
410 | { return concrete().toUuid(defaultValue); } |
411 | |
412 | // only forward-declared, need split functions. Implemented in qcbor{array,map}.h |
413 | QCborArray toArray() const; |
414 | QCborArray toArray(const QCborArray &a) const; |
415 | QCborMap toMap() const; |
416 | QCborMap toMap(const QCborMap &m) const; |
417 | |
418 | const QCborValue operator[](const QString &key) const; |
419 | const QCborValue operator[](QLatin1String key) const; |
420 | const QCborValue operator[](qint64 key) const; |
421 | QCborValueRef operator[](qint64 key); |
422 | QCborValueRef operator[](QLatin1String key); |
423 | QCborValueRef operator[](const QString & key); |
424 | |
425 | int compare(const QCborValue &other) const |
426 | { return concrete().compare(other); } |
427 | #if 0 && __has_include(<compare>) |
428 | std::strong_ordering operator<=>(const QCborValue &other) const |
429 | { |
430 | int c = compare(other); |
431 | if (c > 0) return std::strong_ordering::greater; |
432 | if (c == 0) return std::strong_ordering::equivalent; |
433 | return std::strong_ordering::less; |
434 | } |
435 | #else |
436 | bool operator==(const QCborValue &other) const |
437 | { return compare(other) == 0; } |
438 | bool operator!=(const QCborValue &other) const |
439 | { return !(*this == other); } |
440 | bool operator<(const QCborValue &other) const |
441 | { return compare(other) < 0; } |
442 | #endif |
443 | |
444 | QVariant toVariant() const { return concrete().toVariant(); } |
445 | QJsonValue toJsonValue() const; |
446 | |
447 | #if QT_CONFIG(cborstreamwriter) |
448 | QByteArray toCbor(QCborValue::EncodingOptions opt = QCborValue::NoTransformation) |
449 | { return concrete().toCbor(opt); } |
450 | void toCbor(QCborStreamWriter &writer, QCborValue::EncodingOptions opt = QCborValue::NoTransformation); |
451 | #endif |
452 | |
453 | QString toDiagnosticNotation(QCborValue::DiagnosticNotationOptions opt = QCborValue::Compact) |
454 | { return concrete().toDiagnosticNotation(opts: opt); } |
455 | |
456 | private: |
457 | friend class QCborValue; |
458 | friend class QCborArray; |
459 | friend class QCborMap; |
460 | friend class QCborContainerPrivate; |
461 | friend class QCborValueRefPtr; |
462 | |
463 | // static so we can pass this by value |
464 | static void assign(QCborValueRef that, const QCborValue &other); |
465 | static void assign(QCborValueRef that, QCborValue &&other); |
466 | static void assign(QCborValueRef that, const QCborValueRef other); |
467 | static QCborValue concrete(QCborValueRef that) noexcept; |
468 | QCborValue concrete() const noexcept { return concrete(that: *this); } |
469 | |
470 | static QCborValue::Type concreteType(QCborValueRef self) noexcept Q_DECL_PURE_FUNCTION; |
471 | QCborValue::Type concreteType() const noexcept { return concreteType(self: *this); } |
472 | |
473 | // this will actually be invalid... |
474 | Q_DECL_CONSTEXPR QCborValueRef() : d(nullptr), i(0) {} |
475 | |
476 | QCborValueRef(QCborContainerPrivate *dd, qsizetype ii) |
477 | : d(dd), i(ii) |
478 | {} |
479 | QCborContainerPrivate *d; |
480 | qsizetype i; |
481 | }; |
482 | |
483 | Q_CORE_EXPORT uint qHash(const QCborValue &value, uint seed = 0); |
484 | |
485 | #if !defined(QT_NO_DEBUG_STREAM) |
486 | Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborValue &v); |
487 | #endif |
488 | |
489 | #ifndef QT_NO_DATASTREAM |
490 | Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborValue &); |
491 | Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborValue &); |
492 | #endif |
493 | |
494 | QT_END_NAMESPACE |
495 | |
496 | #if defined(QT_X11_DEFINES_FOUND) |
497 | # define True 1 |
498 | # define False 0 |
499 | #endif |
500 | |
501 | #endif // QCBORVALUE_H |
502 | |