21#if QT_CONFIG(cborstreamreader)
22# include "qcborstreamreader.h"
25#include <private/qglobal_p.h>
26#include <private/qstringconverter_p.h>
67 memcpy(&d, &value,
sizeof(d));
71Q_DECLARE_OPERATORS_FOR_FLAGS(
Element::ValueFlags)
72static_assert(
sizeof(
Element) == 16);
78 const char *
byte()
const {
return reinterpret_cast<
const char *>(
this + 1); }
79 char *
byte() {
return reinterpret_cast<
char *>(
this + 1); }
80 const QChar *
utf16()
const {
return reinterpret_cast<
const QChar *>(
this + 1); }
81 QChar *
utf16() {
return reinterpret_cast<QChar *>(
this + 1); }
103 ~QCborContainerPrivate();
118 void deref() {
if (!ref.deref())
delete this; }
125 const char *block, qsizetype len)
131 qptrdiff offset = target.size();
139 targetUsed += increment;
140 target.resize(offset + increment);
142 char *ptr = target.begin() + offset;
143 auto b =
new (ptr) QtCbor::ByteData;
146 memcpy(b->byte(), block, len);
153 return addByteDataImpl(data, usedData, block, len);
161 size_t offset = size_t(e.value);
162 Q_ASSERT((offset %
alignof(QtCbor::ByteData)) == 0);
163 Q_ASSERT(offset +
sizeof(QtCbor::ByteData) <= size_t(data.size()));
165 auto b =
reinterpret_cast<
const QtCbor::ByteData *>(data.constData() + offset);
166 Q_ASSERT(offset +
sizeof(*b) + size_t(b->len) <= size_t(data.size()));
171 return byteData(elements.at(idx));
186 return replaceAt_complex(e, value, disp);
188 e = { value.value_helper(), value.type() };
189 if (value.isContainer())
190 e.container =
nullptr;
196 e.container->deref();
197 e.container =
nullptr;
200 usedData -= b->len +
sizeof(QtCbor::ByteData);
202 replaceAt_internal(e, value, disp);
206 replaceAt_internal(*elements.insert(idx, {}), value, disp);
211 elements.append(QtCbor::Element());
215 elements.append(QtCbor::Element(value , QCborValue::Integer));
219 elements.append(QtCbor::Element(qint64(tag), QCborValue::Tag));
222 QtCbor::Element::ValueFlags extraFlags = {})
224 elements.append(QtCbor::Element(addByteData(data, len), type,
225 QtCbor::Element::HasByteData | extraFlags));
230 appendByteData(str, len, QCborValue::String, QtCbor::Element::StringIsAscii);
234 appendByteData(str, len, QCborValue::String);
238 if (!QtPrivate::isAscii(s))
239 return appendNonAsciiString(QString(s));
242 appendByteData(s.latin1(), s.size(), QCborValue::String,
243 QtCbor::Element::StringIsAscii);
250 append(qToStringViewIgnoringNull(s)
);
255 if (QtPrivate::isAscii(s))
256 appendAsciiString(s);
258 appendNonAsciiString(s);
262 insertAt(elements.size(), v);
266 insertAt(elements.size(), v, MoveContainer);
267 v.container =
nullptr;
268 v.t = QCborValue::Undefined;
273 const auto &e = elements.at(idx);
277 return data->toByteArray();
281 const auto &e = elements.at(idx);
286 return data->toString();
288 return data->asLatin1();
289 return data->toUtf8String();
293 const auto &e = elements.at(idx);
298 return data->asStringView();
300 return data->asLatin1();
301 return data->asUtf8StringView();
306 v.container =
nullptr;
314 result.container = d;
322 const auto &e = elements.at(idx);
325 if (e.type == QCborValue::Tag && e.container->elements.size() != 2) {
327 return makeValue(QCborValue::Invalid, 0,
nullptr);
329 return makeValue(e.type, -1, e.container);
333 return makeValue(e.type, e.value);
339 qSwap(e, elements[idx]);
342 if (e.type == QCborValue::Tag && e.container->elements.size() != 2) {
344 e.container->deref();
345 return makeValue(QCborValue::Invalid, 0,
nullptr);
349 return extractAt_complex(e);
351 return makeValue(e.type, e.value);
356 if (value.n >= 0 && value.container)
357 return value.container->elements.at(value.n);
362 if (value.container) {
363 e.container = value.container;
371 return QUtf8::compareUtf8(QByteArrayView(b->byte(), b->len), s);
376 return QUtf8::compareUtf8(QByteArrayView(b->byte(), b->len), s);
379 template<
typename String>
382 if (e.type != QCborValue::String)
383 return int(e.type) -
int(QCborValue::String);
387 return s.isEmpty() ? 0 : -1;
391 return b->asStringView() == s ? 0 : 1;
392 return b->asStringView().compare(s);
394 return compareUtf8(b, s);
397 template<
typename String>
403 template<
typename String>
406 return stringEqualsElement(elements.at(idx), s);
414 auto &e1 = elements.at(idx);
415 auto e2 = elementFromValue(value);
422 elements.remove(idx);
429 for ( ; i < elements.size(); i += 2) {
430 const auto &e = elements.at(i);
432 if constexpr (std::is_same_v<std::decay_t<KeyType>, QCborValue>) {
433 equals = (compareElement(i, key, QtCbor::Comparison::ForEquality) == 0);
434 }
else if constexpr (std::is_integral_v<KeyType>) {
435 equals = (e.type == QCborValue::Integer && e.value == key);
438 equals = stringEqualsElement(i, key);
443 return {
this, i + 1 };
447 if (self.isMap() && self.container) {
448 qsizetype idx = self.container->findCborMapKey(key).i;
449 if (idx < self.container->elements.size())
450 return self.container->valueAt(idx);
454 template <
typename KeyType>
static QCborValueRef
458 qsizetype index = size + 1;
460 size = container->elements.size();
461 index = container->findCborMapKey<KeyType>(key).i;
464 Q_ASSERT((size & 1) == 0);
466 container = detach(container, qMax(index + 1, size));
468 Q_ASSERT((container->elements.size() & 1) == 0);
474 Q_ASSERT(index < container->elements.size());
475 return { container, index };
477 template <
typename KeyType>
static QCborValueRef
findOrAddMapKey(QCborMap &map, KeyType key);
478 template <
typename KeyType>
static QCborValueRef
findOrAddMapKey(QCborValue &self, KeyType key);
479 template <
typename KeyType>
static QCborValueRef
findOrAddMapKey(QCborValueRef self, KeyType key);
481#if QT_CONFIG(cborstreamreader)
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
void replaceAt_internal(QtCbor::Element &e, const QCborValue &value, ContainerDisposition disp)
QCborContainerPrivate(QCborContainerPrivate &&)=default
void appendUtf8String(const char *str, qsizetype len)
void append(QCborValue &&v)
void append(const QString &s)
static QCborValueRef findOrAddMapKey(QCborMap &map, KeyType key)
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
QCborContainerPrivate(const QCborContainerPrivate &)=default
static QCborContainerPrivate * detach(QCborContainerPrivate *d, qsizetype reserved)
QCborContainerPrivate & operator=(QCborContainerPrivate &&)=delete
bool stringEqualsElement(const QtCbor::Element &e, String s) const
QCborContainerPrivate()=default
void removeAt(qsizetype idx)
static int compareUtf8(const QtCbor::ByteData *b, QLatin1StringView s)
QCborValueConstRef findCborMapKey(KeyType key)
void replaceAt(qsizetype idx, const QCborValue &value, ContainerDisposition disp=CopyContainer)
QList< QtCbor::Element > elements
QCborContainerPrivate & operator=(const QCborContainerPrivate &)=delete
void appendAsciiString(const char *str, qsizetype len)
static QCborValueRef findOrAddMapKey(QCborValueRef self, KeyType key)
QCborValue extractAt(qsizetype idx)
QAnyStringView anyStringViewAt(qsizetype idx) const
void appendAsciiString(QStringView s)
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)
static QCborValueRef findOrAddMapKey(QCborValue &self, KeyType key)
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
QDebug operator<<(QDebug dbg, const QCborArray &a)
size_t qHash(const QCborArray &array, size_t seed)
QDataStream & operator>>(QDataStream &stream, QCborArray &value)
Q_DECLARE_TYPEINFO(QtCbor::Element, Q_PRIMITIVE_TYPE)
\inmodule QtCore\reentrant
QString toUtf8String() const
QStringView asStringView() const
QUtf8StringView asUtf8StringView() const
QLatin1StringView asLatin1() const
QString asQStringRaw() const
const QChar * utf16() const
const char * byte() const
QByteArray::size_type len
QByteArray asByteArrayView() const
QByteArray toByteArray() const
Element(qint64 v=0, QCborValue::Type t=QCborValue::Undefined, ValueFlags f={})
Element(QCborContainerPrivate *d, QCborValue::Type t, ValueFlags f={})