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 && QT_HAS_INCLUDE(<compare>)
63# include <compare>
64#endif
65
66QT_BEGIN_NAMESPACE
67
68class QCborArray;
69class QCborMap;
70class QCborStreamReader;
71class QCborStreamWriter;
72class QDataStream;
73
74struct QCborParserError
75{
76 qint64 offset = 0;
77 QCborError error = { QCborError::NoError };
78
79 QString errorString() const { return error.toString(); }
80};
81
82class QCborValueRef;
83class QCborContainerPrivate;
84class Q_CORE_EXPORT QCborValue
85{
86 Q_GADGET
87public:
88 enum EncodingOption {
89 SortKeysInMaps = 0x01,
90 UseFloat = 0x02,
91 UseFloat16 = UseFloat | 0x04,
92 UseIntegers = 0x08,
93
94 NoTransformation = 0
95 };
96 Q_DECLARE_FLAGS(EncodingOptions, EncodingOption)
97
98 enum DiagnosticNotationOption {
99 Compact = 0x00,
100 LineWrapped = 0x01,
101 ExtendedFormat = 0x02
102 };
103 Q_DECLARE_FLAGS(DiagnosticNotationOptions, DiagnosticNotationOption)
104
105 // different from QCborStreamReader::Type because we have more types
106 enum Type : int {
107 Integer = 0x00,
108 ByteArray = 0x40,
109 String = 0x60,
110 Array = 0x80,
111 Map = 0xa0,
112 Tag = 0xc0,
113
114 // range 0x100 - 0x1ff for Simple Types
115 SimpleType = 0x100,
116 False = SimpleType + int(QCborSimpleType::False),
117 True = SimpleType + int(QCborSimpleType::True),
118 Null = SimpleType + int(QCborSimpleType::Null),
119 Undefined = SimpleType + int(QCborSimpleType::Undefined),
120
121 Double = 0x202,
122
123 // extended (tagged) types
124 DateTime = 0x10000,
125 Url = 0x10020,
126 RegularExpression = 0x10023,
127 Uuid = 0x10025,
128
129 Invalid = -1
130 };
131 Q_ENUM(Type)
132
133 QCborValue() {}
134 QCborValue(Type t_) : t(t_) {}
135 QCborValue(std::nullptr_t) : t(Null) {}
136 QCborValue(bool b_) : t(b_ ? True : False) {}
137#ifndef Q_QDOC
138 QCborValue(int i) : QCborValue(qint64(i)) {}
139 QCborValue(unsigned u) : QCborValue(qint64(u)) {}
140#endif
141 QCborValue(qint64 i) : n(i), t(Integer) {}
142 QCborValue(double v) : t(Double) { memcpy(&n, &v, sizeof(n)); }
143 QCborValue(QCborSimpleType st) : t(type_helper(st)) {}
144
145 QCborValue(const QByteArray &ba);
146 QCborValue(const QString &s);
147 QCborValue(QLatin1String s);
148#ifndef QT_NO_CAST_FROM_ASCII
149 QT_ASCII_CAST_WARN QCborValue(const char *s) : QCborValue(QString::fromUtf8(s)) {}
150#endif
151 QCborValue(const QCborArray &a);
152 QCborValue(QCborArray &&a);
153 QCborValue(const QCborMap &m);
154 QCborValue(QCborMap &&m);
155 QCborValue(QCborTag tag, const QCborValue &taggedValue = QCborValue());
156 QCborValue(QCborKnownTags t_, const QCborValue &tv = QCborValue())
157 : QCborValue(QCborTag(t_), tv)
158 {}
159
160 explicit QCborValue(const QDateTime &dt);
161 explicit QCborValue(const QUrl &url);
162#if QT_CONFIG(regularexpression)
163 explicit QCborValue(const QRegularExpression &rx);
164#endif
165 explicit QCborValue(const QUuid &uuid);
166
167 ~QCborValue() { if (container) dispose(); }
168
169 // make sure const char* doesn't go call the bool constructor
170 QCborValue(const void *) = delete;
171
172 QCborValue(const QCborValue &other);
173 QCborValue(QCborValue &&other) noexcept
174 : n(other.n), container(other.container), t(other.t)
175 {
176 other.t = Undefined;
177 other.container = nullptr;
178 }
179 QCborValue &operator=(const QCborValue &other);
180 QCborValue &operator=(QCborValue &&other) noexcept
181 {
182 QCborValue tmp;
183 qSwap(*this, tmp);
184 qSwap(other, *this);
185 return *this;
186 }
187
188 void swap(QCborValue &other) noexcept
189 {
190 qSwap(n, other.n);
191 qSwap(container, other.container);
192 qSwap(t, other.t);
193 }
194
195 Type type() const { return t; }
196 bool isInteger() const { return type() == Integer; }
197 bool isByteArray() const { return type() == ByteArray; }
198 bool isString() const { return type() == String; }
199 bool isArray() const { return type() == Array; }
200 bool isMap() const { return type() == Map; }
201 bool isTag() const { return isTag_helper(type()); }
202 bool isFalse() const { return type() == False; }
203 bool isTrue() const { return type() == True; }
204 bool isBool() const { return isFalse() || isTrue(); }
205 bool isNull() const { return type() == Null; }
206 bool isUndefined() const { return type() == Undefined; }
207 bool isDouble() const { return type() == Double; }
208 bool isDateTime() const { return type() == DateTime; }
209 bool isUrl() const { return type() == Url; }
210 bool isRegularExpression() const { return type() == RegularExpression; }
211 bool isUuid() const { return type() == Uuid; }
212 bool isInvalid() const { return type() == Invalid; }
213 bool isContainer() const { return isMap() || isArray(); }
214
215 bool isSimpleType() const
216 {
217 return int(type()) >> 8 == int(SimpleType) >> 8;
218 }
219 bool isSimpleType(QCborSimpleType st) const
220 {
221 return type() == type_helper(st);
222 }
223 QCborSimpleType toSimpleType(QCborSimpleType defaultValue = QCborSimpleType::Undefined) const
224 {
225 return isSimpleType() ? QCborSimpleType(type() & 0xff) : defaultValue;
226 }
227
228 qint64 toInteger(qint64 defaultValue = 0) const
229 { return isInteger() ? value_helper() : isDouble() ? qint64(fp_helper()) : defaultValue; }
230 bool toBool(bool defaultValue = false) const
231 { return isBool() ? isTrue() : defaultValue; }
232 double toDouble(double defaultValue = 0) const
233 { return isDouble() ? fp_helper() : isInteger() ? double(value_helper()) : defaultValue; }
234
235 QCborTag tag(QCborTag defaultValue = QCborTag(-1)) const;
236 QCborValue taggedValue(const QCborValue &defaultValue = QCborValue()) const;
237
238 QByteArray toByteArray(const QByteArray &defaultValue = {}) const;
239 QString toString(const QString &defaultValue = {}) const;
240 QDateTime toDateTime(const QDateTime &defaultValue = {}) const;
241 QUrl toUrl(const QUrl &defaultValue = {}) const;
242#if QT_CONFIG(regularexpression)
243 QRegularExpression toRegularExpression(const QRegularExpression &defaultValue = {}) const;
244#endif
245 QUuid toUuid(const QUuid &defaultValue = {}) const;
246
247 // only forward-declared, need split functions
248 QCborArray toArray() const;
249 QCborArray toArray(const QCborArray &defaultValue) const;
250 QCborMap toMap() const;
251 QCborMap toMap(const QCborMap &defaultValue) const;
252
253 const QCborValue operator[](const QString &key) const;
254 const QCborValue operator[](QLatin1String key) const;
255 const QCborValue operator[](qint64 key) const;
256 QCborValueRef operator[](qint64 key);
257 QCborValueRef operator[](QLatin1String key);
258 QCborValueRef operator[](const QString & key);
259
260 int compare(const QCborValue &other) const;
261#if 0 && QT_HAS_INCLUDE(<compare>)
262 std::strong_ordering operator<=>(const QCborValue &other) const
263 {
264 int c = compare(other);
265 if (c > 0) return std::partial_ordering::greater;
266 if (c == 0) return std::partial_ordering::equivalent;
267 return std::partial_ordering::less;
268 }
269#else
270 bool operator==(const QCborValue &other) const noexcept
271 { return compare(other) == 0; }
272 bool operator!=(const QCborValue &other) const noexcept
273 { return !(*this == other); }
274 bool operator<(const QCborValue &other) const
275 { return compare(other) < 0; }
276#endif
277
278 static QCborValue fromVariant(const QVariant &variant);
279 QVariant toVariant() const;
280 static QCborValue fromJsonValue(const QJsonValue &v);
281 QJsonValue toJsonValue() const;
282
283 static QCborValue fromCbor(QCborStreamReader &reader);
284 static QCborValue fromCbor(const QByteArray &ba, QCborParserError *error = nullptr);
285 static QCborValue fromCbor(const char *data, qsizetype len, QCborParserError *error = nullptr)
286 { return fromCbor(QByteArray(data, int(len)), error); }
287 static QCborValue fromCbor(const quint8 *data, qsizetype len, QCborParserError *error = nullptr)
288 { return fromCbor(QByteArray(reinterpret_cast<const char *>(data), int(len)), error); }
289 QByteArray toCbor(EncodingOptions opt = NoTransformation);
290 void toCbor(QCborStreamWriter &writer, EncodingOptions opt = NoTransformation);
291
292 QString toDiagnosticNotation(DiagnosticNotationOptions opts = Compact) const;
293
294private:
295 friend class QCborValueRef;
296 friend class QCborContainerPrivate;
297 qint64 n = 0;
298 QCborContainerPrivate *container = nullptr;
299 Type t = Undefined;
300
301 void dispose();
302 qint64 value_helper() const
303 {
304 return n;
305 }
306
307 double fp_helper() const
308 {
309 Q_STATIC_ASSERT(sizeof(double) == sizeof(n));
310 double d;
311 memcpy(&d, &n, sizeof(d));
312 return d;
313 }
314
315 Q_DECL_CONSTEXPR static Type type_helper(QCborSimpleType st)
316 {
317 return Type(quint8(st) | SimpleType);
318 }
319
320 Q_DECL_CONSTEXPR static bool isTag_helper(Type t)
321 {
322 return t == Tag || t >= 0x10000;
323 }
324};
325Q_DECLARE_SHARED(QCborValue)
326
327class Q_CORE_EXPORT QCborValueRef
328{
329public:
330 operator QCborValue() const { return concrete(); }
331
332 QCborValueRef(const QCborValueRef &) noexcept = default;
333 QCborValueRef(QCborValueRef &&) noexcept = default;
334 QCborValueRef &operator=(const QCborValue &other)
335 { assign(*this, other); return *this; }
336 QCborValueRef &operator=(QCborValue &&other)
337 { assign(*this, std::move(other)); other.container = nullptr; return *this; }
338 QCborValueRef &operator=(const QCborValueRef &other)
339 { assign(*this, other); return *this; }
340
341 QCborValue::Type type() const { return concreteType(); }
342 bool isInteger() const { return type() == QCborValue::Integer; }
343 bool isByteArray() const { return type() == QCborValue::ByteArray; }
344 bool isString() const { return type() == QCborValue::String; }
345 bool isArray() const { return type() == QCborValue::Array; }
346 bool isMap() const { return type() == QCborValue::Map; }
347 bool isTag() const { return QCborValue::isTag_helper(type()); }
348 bool isFalse() const { return type() == QCborValue::False; }
349 bool isTrue() const { return type() == QCborValue::True; }
350 bool isBool() const { return isFalse() || isTrue(); }
351 bool isNull() const { return type() == QCborValue::Null; }
352 bool isUndefined() const { return type() == QCborValue::Undefined; }
353 bool isDouble() const { return type() == QCborValue::Double; }
354 bool isDateTime() const { return type() == QCborValue::DateTime; }
355 bool isUrl() const { return type() == QCborValue::Url; }
356 bool isRegularExpression() const { return type() == QCborValue::RegularExpression; }
357 bool isUuid() const { return type() == QCborValue::Uuid; }
358 bool isInvalid() const { return type() == QCborValue::Invalid; }
359 bool isContainer() const { return isMap() || isArray(); }
360 bool isSimpleType() const
361 {
362 return type() >= QCborValue::SimpleType && type() < QCborValue::SimpleType + 0x100;
363 }
364 bool isSimpleType(QCborSimpleType st) const
365 {
366 return type() == QCborValue::type_helper(st);
367 }
368
369 QCborTag tag(QCborTag defaultValue = QCborTag(-1)) const
370 { return concrete().tag(defaultValue); }
371 QCborValue taggedValue(const QCborValue &defaultValue = QCborValue()) const
372 { return concrete().taggedValue(defaultValue); }
373
374 qint64 toInteger(qint64 defaultValue = 0) const
375 { return concrete().toInteger(defaultValue); }
376 bool toBool(bool defaultValue = false) const
377 { return concrete().toBool(defaultValue); }
378 double toDouble(double defaultValue = 0) const
379 { return concrete().toDouble(defaultValue); }
380
381 QByteArray toByteArray(const QByteArray &defaultValue = {}) const
382 { return concrete().toByteArray(defaultValue); }
383 QString toString(const QString &defaultValue = {}) const
384 { return concrete().toString(defaultValue); }
385 QDateTime toDateTime(const QDateTime &defaultValue = {}) const
386 { return concrete().toDateTime(defaultValue); }
387 QUrl toUrl(const QUrl &defaultValue = {}) const
388 { return concrete().toUrl(defaultValue); }
389#if QT_CONFIG(regularexpression)
390 QRegularExpression toRegularExpression(const QRegularExpression &defaultValue = {}) const
391 { return concrete().toRegularExpression(defaultValue); }
392#endif
393 QUuid toUuid(const QUuid &defaultValue = {}) const
394 { return concrete().toUuid(defaultValue); }
395
396 // only forward-declared, need split functions. Implemented in qcbor{array,map}.h
397 QCborArray toArray() const;
398 QCborArray toArray(const QCborArray &a) const;
399 QCborMap toMap() const;
400 QCborMap toMap(const QCborMap &m) const;
401
402 const QCborValue operator[](const QString &key) const;
403 const QCborValue operator[](QLatin1String key) const;
404 const QCborValue operator[](qint64 key) const;
405 QCborValueRef operator[](qint64 key);
406 QCborValueRef operator[](QLatin1String key);
407 QCborValueRef operator[](const QString & key);
408
409 int compare(const QCborValue &other) const
410 { return concrete().compare(other); }
411#if 0 && QT_HAS_INCLUDE(<compare>)
412 std::strong_ordering operator<=>(const QCborValue &other) const
413 {
414 int c = compare(other);
415 if (c > 0) return std::strong_ordering::greater;
416 if (c == 0) return std::strong_ordering::equivalent;
417 return std::strong_ordering::less;
418 }
419#else
420 bool operator==(const QCborValue &other) const
421 { return compare(other) == 0; }
422 bool operator!=(const QCborValue &other) const
423 { return !(*this == other); }
424 bool operator<(const QCborValue &other) const
425 { return compare(other) < 0; }
426#endif
427
428 QVariant toVariant() const { return concrete().toVariant(); }
429 QJsonValue toJsonValue() const;
430
431 QByteArray toCbor(QCborValue::EncodingOptions opt = QCborValue::NoTransformation)
432 { return concrete().toCbor(opt); }
433 void toCbor(QCborStreamWriter &writer, QCborValue::EncodingOptions opt = QCborValue::NoTransformation);
434
435 QString toDiagnosticNotation(QCborValue::DiagnosticNotationOptions opt = QCborValue::Compact)
436 { return concrete().toDiagnosticNotation(opt); }
437
438private:
439 friend class QCborValue;
440 friend class QCborArray;
441 friend class QCborMap;
442 friend class QCborContainerPrivate;
443 friend class QCborValueRefPtr;
444
445 // static so we can pass this by value
446 static void assign(QCborValueRef that, const QCborValue &other);
447 static void assign(QCborValueRef that, QCborValue &&other);
448 static void assign(QCborValueRef that, const QCborValueRef other);
449 static QCborValue concrete(QCborValueRef that) noexcept;
450 QCborValue concrete() const noexcept { return concrete(*this); }
451
452 static QCborValue::Type concreteType(QCborValueRef self) noexcept Q_DECL_PURE_FUNCTION;
453 QCborValue::Type concreteType() const noexcept { return concreteType(*this); }
454
455 // this will actually be invalid...
456 Q_DECL_CONSTEXPR QCborValueRef() : d(nullptr), i(0) {}
457
458 QCborValueRef(QCborContainerPrivate *dd, qsizetype ii)
459 : d(dd), i(ii)
460 {}
461 QCborContainerPrivate *d;
462 qsizetype i;
463};
464
465Q_CORE_EXPORT uint qHash(const QCborValue &value, uint seed = 0);
466
467#if !defined(QT_NO_DEBUG_STREAM)
468Q_CORE_EXPORT QDebug operator<<(QDebug, const QCborValue &v);
469#endif
470
471#ifndef QT_NO_DATASTREAM
472Q_CORE_EXPORT QDataStream &operator<<(QDataStream &, const QCborValue &);
473Q_CORE_EXPORT QDataStream &operator>>(QDataStream &, QCborValue &);
474#endif
475
476QT_END_NAMESPACE
477
478#if defined(QT_X11_DEFINES_FOUND)
479# define True 1
480# define False 0
481#endif
482
483#endif // QCBORVALUE_H
484