Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qcborvalue_p.h
Go to the documentation of this file.
1// Copyright (C) 2020 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_P_H
5#define QCBORVALUE_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API.
12// This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include "qcborvalue.h"
19
20#if QT_CONFIG(cborstreamreader)
21# include "qcborstreamreader.h"
22#endif
23
24#include <private/qglobal_p.h>
25#include <private/qstringconverter_p.h>
26
27#include <math.h>
28
30
31namespace QtCbor {
32enum class Comparison {
35};
36
37struct Undefined {};
38struct Element
39{
41 IsContainer = 0x0001,
42 HasByteData = 0x0002,
43 StringIsUtf16 = 0x0004,
44 StringIsAscii = 0x0008
45 };
46 Q_DECLARE_FLAGS(ValueFlags, ValueFlag)
47
48 union {
51 };
53 ValueFlags flags = {};
54
56 : value(v), type(t), flags(f)
57 {}
58
62
63 double fpvalue() const
64 {
65 double d;
66 memcpy(&d, &value, sizeof(d));
67 return d;
68 }
69};
70Q_DECLARE_OPERATORS_FOR_FLAGS(Element::ValueFlags)
71static_assert(sizeof(Element) == 16);
72
74{
76
77 const char *byte() const { return reinterpret_cast<const char *>(this + 1); }
78 char *byte() { return reinterpret_cast<char *>(this + 1); }
79 const QChar *utf16() const { return reinterpret_cast<const QChar *>(this + 1); }
80 QChar *utf16() { return reinterpret_cast<QChar *>(this + 1); }
81
82 QByteArray toByteArray() const { return QByteArray(byte(), len); }
83 QString toString() const { return QString(utf16(), len / 2); }
84 QString toUtf8String() const { return QString::fromUtf8(byte(), len); }
85
87 QLatin1StringView asLatin1() const { return {byte(), len}; }
89 QStringView asStringView() const{ return QStringView(utf16(), len / 2); }
91};
92static_assert(std::is_trivial<ByteData>::value);
93static_assert(std::is_standard_layout<ByteData>::value);
94} // namespace QtCbor
95
97
99{
102
103public:
105
108 QList<QtCbor::Element> elements;
109
110 void deref() { if (!ref.deref()) delete this; }
111 void compact();
115
117 const char *block, qsizetype len)
118 {
119 // This function does not do overflow checking, since the len parameter
120 // is expected to be trusted. There's another version of this function
121 // in decodeStringFromCbor(), which checks.
122
123 qptrdiff offset = target.size();
124
125 // align offset
126 offset += alignof(QtCbor::ByteData) - 1;
127 offset &= ~(alignof(QtCbor::ByteData) - 1);
128
130
131 targetUsed += increment;
132 target.resize(offset + increment);
133
134 char *ptr = target.begin() + offset;
135 auto b = new (ptr) QtCbor::ByteData;
136 b->len = len;
137 if (block)
138 memcpy(b->byte(), block, len);
139
140 return offset;
141 }
142
143 qptrdiff addByteData(const char *block, qsizetype len)
144 {
145 return addByteDataImpl(data, usedData, block, len);
146 }
147
149 {
150 if ((e.flags & QtCbor::Element::HasByteData) == 0)
151 return nullptr;
152
153 size_t offset = size_t(e.value);
154 Q_ASSERT((offset % alignof(QtCbor::ByteData)) == 0);
155 Q_ASSERT(offset + sizeof(QtCbor::ByteData) <= size_t(data.size()));
156
157 auto b = reinterpret_cast<const QtCbor::ByteData *>(data.constData() + offset);
158 Q_ASSERT(offset + sizeof(*b) + size_t(b->len) <= size_t(data.size()));
159 return b;
160 }
162 {
163 return byteData(elements.at(idx));
164 }
165
167 {
168 const QtCbor::Element &e = elements.at(idx);
169 if (e.type != type || (e.flags & QtCbor::Element::IsContainer) == 0)
170 return nullptr;
171 return e.container;
172 }
173
176 {
177 if (value.container)
178 return replaceAt_complex(e, value, disp);
179
180 e = { value.value_helper(), value.type() };
181 if (value.isContainer())
182 e.container = nullptr;
183 }
185 {
186 QtCbor::Element &e = elements[idx];
187 if (e.flags & QtCbor::Element::IsContainer) {
188 e.container->deref();
189 e.container = nullptr;
190 e.flags = {};
191 } else if (auto b = byteData(e)) {
192 usedData -= b->len + sizeof(QtCbor::ByteData);
193 }
194 replaceAt_internal(e, value, disp);
195 }
197 {
198 replaceAt_internal(*elements.insert(idx, {}), value, disp);
199 }
200
214 QtCbor::Element::ValueFlags extraFlags = {})
215 {
217 QtCbor::Element::HasByteData | extraFlags));
218 }
221 {
222 appendByteData(str, len, QCborValue::String, QtCbor::Element::StringIsAscii);
223 }
229 {
230 if (!QtPrivate::isAscii(s))
232
233 // US-ASCII is a subset of UTF-8, so we can keep in 8-bit
234 appendByteData(s.latin1(), s.size(), QCborValue::String,
235 QtCbor::Element::StringIsAscii);
236 }
239
240 void append(const QString &s)
241 {
243 }
244
246 {
249 else
251 }
252 void append(const QCborValue &v)
253 {
255 }
256
258 {
259 const auto &e = elements.at(idx);
260 const auto data = byteData(e);
261 if (!data)
262 return QByteArray();
263 return data->toByteArray();
264 }
266 {
267 const auto &e = elements.at(idx);
268 const auto data = byteData(e);
269 if (!data)
270 return QString();
271 if (e.flags & QtCbor::Element::StringIsUtf16)
272 return data->toString();
273 if (e.flags & QtCbor::Element::StringIsAscii)
274 return data->asLatin1();
275 return data->toUtf8String();
276 }
277
278 static void resetValue(QCborValue &v)
279 {
280 v.container = nullptr;
281 }
282
285 {
287 result.n = n;
288 result.container = d;
289 if (d && disp == CopyContainer)
290 d->ref.ref();
291 return result;
292 }
293
295 {
296 const auto &e = elements.at(idx);
297
298 if (e.flags & QtCbor::Element::IsContainer) {
299 if (e.type == QCborValue::Tag && e.container->elements.size() != 2) {
300 // invalid tags can be created due to incomplete parsing
301 return makeValue(QCborValue::Invalid, 0, nullptr);
302 }
303 return makeValue(e.type, -1, e.container);
304 } else if (e.flags & QtCbor::Element::HasByteData) {
305 return makeValue(e.type, idx, const_cast<QCborContainerPrivate *>(this));
306 }
307 return makeValue(e.type, e.value);
308 }
311 {
313 qSwap(e, elements[idx]);
314
315 if (e.flags & QtCbor::Element::IsContainer) {
316 if (e.type == QCborValue::Tag && e.container->elements.size() != 2) {
317 // invalid tags can be created due to incomplete parsing
318 e.container->deref();
319 return makeValue(QCborValue::Invalid, 0, nullptr);
320 }
321 return makeValue(e.type, -1, e.container, MoveContainer);
322 } else if (e.flags & QtCbor::Element::HasByteData) {
323 return extractAt_complex(e);
324 }
325 return makeValue(e.type, e.value);
326 }
327
329 {
330 if (value.n >= 0 && value.container)
331 return value.container->elements.at(value.n);
332
334 e.value = value.n;
335 e.type = value.t;
336 if (value.container) {
337 e.container = value.container;
338 e.flags = QtCbor::Element::IsContainer;
339 }
340 return e;
341 }
342
344 {
345 return QUtf8::compareUtf8(QByteArrayView(b->byte(), b->len), s);
346 }
347
349 {
350 return QUtf8::compareUtf8(QByteArrayView(b->byte(), b->len), s);
351 }
352
353 template<typename String>
355 {
356 if (e.type != QCborValue::String)
357 return int(e.type) - int(QCborValue::String);
358
359 const QtCbor::ByteData *b = byteData(e);
360 if (!b)
361 return s.isEmpty() ? 0 : -1;
362
363 if (e.flags & QtCbor::Element::StringIsUtf16) {
365 return QtPrivate::equalStrings(b->asStringView(), s) ? 0 : 1;
366 return QtPrivate::compareStrings(b->asStringView(), s);
367 }
368 return compareUtf8(b, s);
369 }
370
371 template<typename String>
372 bool stringEqualsElement(const QtCbor::Element &e, String s) const
373 {
375 }
376
377 template<typename String>
378 bool stringEqualsElement(qsizetype idx, String s) const
379 {
380 return stringEqualsElement(elements.at(idx), s);
381 }
382
385 QtCbor::Comparison mode) noexcept;
387 {
388 auto &e1 = elements.at(idx);
389 auto e2 = elementFromValue(value);
390 return compareElement_helper(this, e1, value.container, e2, mode);
391 }
392
394 {
395 replaceAt(idx, {});
396 elements.remove(idx);
397 }
398
399 // doesn't apply to JSON
400 template <typename KeyType> QCborValueConstRef findCborMapKey(KeyType key)
401 {
402 qsizetype i = 0;
403 for ( ; i < elements.size(); i += 2) {
404 const auto &e = elements.at(i);
405 bool equals;
406 if constexpr (std::is_same_v<std::decay_t<KeyType>, QCborValue>) {
408 } else if constexpr (std::is_integral_v<KeyType>) {
409 equals = (e.type == QCborValue::Integer && e.value == key);
410 } else {
411 // assume it's a string
412 equals = stringEqualsElement(i, key);
413 }
414 if (equals)
415 break;
416 }
417 return { this, i + 1 };
418 }
419 template <typename KeyType> static QCborValue findCborMapKey(const QCborValue &self, KeyType key)
420 {
421 if (self.isMap() && self.container) {
422 qsizetype idx = self.container->findCborMapKey(key).i;
423 if (idx < self.container->elements.size())
424 return self.container->valueAt(idx);
425 }
426 return QCborValue();
427 }
428 template <typename KeyType> static QCborValueRef
430 {
431 qsizetype size = 0;
432 qsizetype index = size + 1;
433 if (container) {
434 size = container->elements.size();
435 index = container->findCborMapKey<KeyType>(key).i; // returns size + 1 if not found
436 }
437 Q_ASSERT(index & 1);
438 Q_ASSERT((size & 1) == 0);
439
440 container = detach(container, qMax(index + 1, size));
441 Q_ASSERT(container);
442 Q_ASSERT((container->elements.size() & 1) == 0);
443
444 if (index >= size) {
445 container->append(key);
446 container->append(QCborValue());
447 }
448 Q_ASSERT(index < container->elements.size());
449 return { container, index };
450 }
451 template <typename KeyType> static QCborValueRef findOrAddMapKey(QCborMap &map, KeyType key);
452 template <typename KeyType> static QCborValueRef findOrAddMapKey(QCborValue &self, KeyType key);
453 template <typename KeyType> static QCborValueRef findOrAddMapKey(QCborValueRef self, KeyType key);
454
455#if QT_CONFIG(cborstreamreader)
456 void decodeValueFromCbor(QCborStreamReader &reader, int remainingStackDepth);
457 void decodeStringFromCbor(QCborStreamReader &reader);
458 static inline void setErrorInReader(QCborStreamReader &reader, QCborError error);
459#endif
460};
461
463
464#endif // QCBORVALUE_P_H
\inmodule QtCore
Definition qbytearray.h:57
qsizetype size_type
Definition qbytearray.h:459
static QByteArray fromRawData(const char *data, qsizetype size)
Constructs a QByteArray that uses the first size bytes of the data array.
Definition qbytearray.h:409
QString stringAt(qsizetype idx) const
void insertAt(qsizetype idx, const QCborValue &value, ContainerDisposition disp=CopyContainer)
QByteArray::size_type usedData
const QtCbor::ByteData * byteData(QtCbor::Element e) const
static int compareUtf8(const QtCbor::ByteData *b, QStringView s)
void replaceAt_internal(QtCbor::Element &e, const QCborValue &value, ContainerDisposition disp)
void append(QStringView s)
void appendUtf8String(const char *str, qsizetype len)
void append(const QString &s)
void append(const QCborValue &v)
const QtCbor::ByteData * byteData(qsizetype idx) const
void appendByteData(const char *data, qsizetype len, QCborValue::Type type, QtCbor::Element::ValueFlags extraFlags={})
static QCborContainerPrivate * grow(QCborContainerPrivate *d, qsizetype index)
Prepare for an insertion at position index.
int stringCompareElement(const QtCbor::Element &e, String s, QtCbor::Comparison mode) const
static int compareElement_helper(const QCborContainerPrivate *c1, QtCbor::Element e1, const QCborContainerPrivate *c2, QtCbor::Element e2, QtCbor::Comparison mode) noexcept
static QCborContainerPrivate * detach(QCborContainerPrivate *d, qsizetype reserved)
bool stringEqualsElement(const QtCbor::Element &e, String s) const
void removeAt(qsizetype idx)
static int compareUtf8(const QtCbor::ByteData *b, QLatin1StringView s)
void append(QLatin1StringView s)
QCborValueConstRef findCborMapKey(KeyType key)
void replaceAt(qsizetype idx, const QCborValue &value, ContainerDisposition disp=CopyContainer)
QList< QtCbor::Element > elements
void append(QCborTag tag)
void appendAsciiString(const char *str, qsizetype len)
QCborValue extractAt(qsizetype idx)
void appendNonAsciiString(QStringView s)
void append(QtCbor::Undefined)
static qptrdiff addByteDataImpl(QByteArray &target, QByteArray::size_type &targetUsed, const char *block, qsizetype len)
QByteArray byteArrayAt(qsizetype idx) const
QCborContainerPrivate * containerAt(qsizetype idx, QCborValue::Type type) const
static QCborValue makeValue(QCborValue::Type type, qint64 n, QCborContainerPrivate *d=nullptr, ContainerDisposition disp=CopyContainer)
int compareElement(qsizetype idx, const QCborValue &value, QtCbor::Comparison mode) const
static QCborValueRef findOrAddMapKey(QCborContainerPrivate *container, KeyType key)
QCborValue extractAt_complex(QtCbor::Element e)
void appendAsciiString(const QString &s)
qptrdiff addByteData(const char *block, qsizetype len)
static QtCbor::Element elementFromValue(const QCborValue &value)
void replaceAt_complex(QtCbor::Element &e, const QCborValue &value, ContainerDisposition disp)
static void resetValue(QCborValue &v)
static QCborValue findCborMapKey(const QCborValue &self, KeyType key)
bool stringEqualsElement(qsizetype idx, String s) const
QCborValue valueAt(qsizetype idx) const
void append(qint64 value)
static QCborContainerPrivate * clone(QCborContainerPrivate *d, qsizetype reserved=-1)
\inmodule QtCore\reentrant
Definition qcbormap.h:21
\inmodule QtCore\reentrant
\inmodule QtCore\reentrant
Definition qcborvalue.h:47
Type
This enum represents the QCborValue type.
Definition qcborvalue.h:70
\inmodule QtCore
qsizetype size() const noexcept
Definition qlist.h:397
iterator insert(qsizetype i, parameter_type t)
Definition qlist.h:488
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
void remove(qsizetype i, qsizetype n=1)
Definition qlist.h:794
void append(parameter_type t)
Definition qlist.h:458
\inmodule QtCore
Definition qshareddata.h:19
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:6018
static QString fromRawData(const QChar *, qsizetype size)
Constructs a QString that uses the first size Unicode characters in the array unicode.
Definition qstring.cpp:9482
QString str
[2]
QMap< QString, QString > map
[6]
Combined button and popup list for selecting options.
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool equalStrings(QStringView lhs, QStringView rhs) noexcept
Definition qstring.cpp:1393
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION int compareStrings(QStringView lhs, QStringView rhs, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
Q_CORE_EXPORT Q_DECL_PURE_FUNCTION bool isAscii(QLatin1StringView s) noexcept
Definition qstring.cpp:850
QCborTag
Definition qcborcommon.h:30
AudioChannelLayoutTag tag
DBusConnection const char DBusError * error
typedef QByteArray(EGLAPIENTRYP PFNQGSGETDISPLAYSPROC)()
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
#define Q_DECLARE_FLAGS(Flags, Enum)
Definition qflags.h:174
#define Q_DECLARE_OPERATORS_FOR_FLAGS(Flags)
Definition qflags.h:194
static ControlElement< T > * ptr(QWidget *widget)
constexpr const T & qMax(const T &a, const T &b)
Definition qminmax.h:42
GLboolean GLboolean GLboolean b
GLsizei const GLfloat * v
[13]
GLenum mode
GLuint64 key
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint index
[2]
GLint GLsizei GLsizei GLenum GLenum GLsizei void * data
GLfloat GLfloat f
GLenum type
GLenum target
GLbitfield flags
GLenum GLuint GLintptr offset
GLint ref
GLfloat n
GLdouble s
[6]
Definition qopenglext.h:235
GLdouble GLdouble t
Definition qopenglext.h:243
GLuint64EXT * result
[6]
GLenum GLsizei len
#define Q_ASSERT(cond)
Definition qrandom.cpp:47
QBasicUtf8StringView< false > QUtf8StringView
Definition qstringfwd.h:46
QStringView qToStringViewIgnoringNull(const QStringLike &s) noexcept
QT_BEGIN_NAMESPACE constexpr void qSwap(T &value1, T &value2) noexcept(std::is_nothrow_swappable_v< T >)
Definition qswap.h:20
QWidget Element
Definition main.cpp:7
@ Q_PRIMITIVE_TYPE
Definition qtypeinfo.h:157
#define Q_DECLARE_TYPEINFO(TYPE, FLAGS)
Definition qtypeinfo.h:180
unsigned int quint32
Definition qtypes.h:50
ptrdiff_t qptrdiff
Definition qtypes.h:164
ptrdiff_t qsizetype
Definition qtypes.h:165
long long qint64
Definition qtypes.h:60
QtConcurrent::task([]{ qDebug("Hello, world!");}).spawn(FutureResult void increment(QPromise< int > &promise, int i)
[10]
MyCustomStruct c2
QCborValue(QCborTag(2), QByteArray("\x01\0\0\0\0\0\0\0\0", 9))
[0]
\inmodule QtCore \inheaderfile QtCborCommon \reentrant
Definition qcborcommon.h:63
static int compareUtf8(QByteArrayView utf8, QStringView utf16, Qt::CaseSensitivity cs=Qt::CaseSensitive) noexcept
QString toUtf8String() const
QStringView asStringView() const
QUtf8StringView asUtf8StringView() const
QLatin1StringView asLatin1() const
QString asQStringRaw() const
const QChar * utf16() const
QString toString() const
const char * byte() const
QByteArray::size_type len
QByteArray asByteArrayView() const
QByteArray toByteArray() const
double fpvalue() const
Element(qint64 v=0, QCborValue::Type t=QCborValue::Undefined, ValueFlags f={})
Element(QCborContainerPrivate *d, QCborValue::Type t, ValueFlags f={})
QCborValue::Type type
ValueFlags flags
QCborContainerPrivate * container