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