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();
121 void deref() {
if (!ref.deref())
delete this; }
128 const char *block, qsizetype len)
134 qptrdiff offset = target.size();
142 targetUsed += increment;
143 target.resize(offset + increment);
145 char *ptr = target.begin() + offset;
146 auto b =
new (ptr) QtCbor::ByteData;
149 memcpy(b->byte(), block, len);
156 return addByteDataImpl(data, usedData, block, len);
164 size_t offset = size_t(e.value);
165 Q_ASSERT((offset %
alignof(QtCbor::ByteData)) == 0);
166 Q_ASSERT(offset +
sizeof(QtCbor::ByteData) <= size_t(data.size()));
168 auto b =
reinterpret_cast<
const QtCbor::ByteData *>(data.constData() + offset);
169 Q_ASSERT(offset +
sizeof(*b) + size_t(b->len) <= size_t(data.size()));
189 return replaceAt_complex(e, value, disp);
191 e = { value.value_helper(), value.type() };
192 if (value.isContainer())
193 e.container =
nullptr;
199 e.container->deref();
200 e.container =
nullptr;
203 usedData -= b->len +
sizeof(QtCbor::ByteData);
205 replaceAt_internal(e, value, disp);
209 replaceAt_internal(*elements.insert(idx, {}), value, disp);
214 elements.append(QtCbor::Element());
218 elements.append(QtCbor::Element(value , QCborValue::Integer));
222 elements.append(QtCbor::Element(qint64(tag), QCborValue::Tag));
225 QtCbor::Element::ValueFlags extraFlags = {})
227 elements.append(QtCbor::Element(addByteData(data, len), type,
228 QtCbor::Element::HasByteData | extraFlags));
232 appendByteData(str, len, QCborValue::String, QtCbor::Element::StringIsAscii);
236 appendByteData(str, len, QCborValue::String);
240 if (!QtPrivate::isAscii(s))
241 return appendNonAsciiString(QString(s));
244 appendByteData(s.latin1(), s.size(), QCborValue::String,
245 QtCbor::Element::StringIsAscii);
252 append(qToStringViewIgnoringNull(s)
);
257 if (QtPrivate::isAscii(s))
258 appendAsciiString(s);
260 appendNonAsciiString(s);
264 insertAt(elements.size(), v);
268 insertAt(elements.size(), v, MoveContainer);
269 v.container =
nullptr;
270 v.t = QCborValue::Undefined;
275 const auto &e = elements.at(idx);
279 return data->toByteArray();
283 const auto &e = elements.at(idx);
288 return data->toString();
290 return data->asLatin1();
291 return data->toUtf8String();
295 const auto &e = elements.at(idx);
300 return data->asStringView();
302 return data->asLatin1();
303 return data->asUtf8StringView();
308 v.container =
nullptr;
316 result.container = d;
324 const auto &e = elements.at(idx);
327 if (e.type == QCborValue::Tag && e.container->elements.size() != 2) {
329 return makeValue(QCborValue::Invalid, 0,
nullptr);
331 return makeValue(e.type, -1, e.container);
335 return makeValue(e.type, e.value);
341 qSwap(e, elements[idx]);
344 if (e.type == QCborValue::Tag && e.container->elements.size() != 2) {
346 e.container->deref();
347 return makeValue(QCborValue::Invalid, 0,
nullptr);
351 return extractAt_complex(e);
353 return makeValue(e.type, e.value);
358 if (value.n >= 0 && value.container)
359 return value.container->elements.at(value.n);
364 if (value.container) {
365 e.container = value.container;
373 return QUtf8::compareUtf8(QByteArrayView(b->byte(), b->len), s);
378 return QUtf8::compareUtf8(QByteArrayView(b->byte(), b->len), s);
381 template<
typename String>
384 if (e.type != QCborValue::String)
385 return int(e.type) -
int(QCborValue::String);
389 return s.isEmpty() ? 0 : -1;
393 return b->asStringView() == s ? 0 : 1;
394 return b->asStringView().compare(s);
396 return compareUtf8(b, s);
399 template<
typename String>
405 template<
typename String>
408 return stringEqualsElement(elements.at(idx), s);
416 auto &e1 = elements.at(idx);
417 auto e2 = elementFromValue(value);
424 elements.remove(idx);
431 for ( ; i < elements.size(); i += 2) {
432 const auto &e = elements.at(i);
434 if constexpr (std::is_same_v<std::decay_t<KeyType>, QCborValue>) {
435 equals = (compareElement(i, key, QtCbor::Comparison::ForEquality) == 0);
436 }
else if constexpr (std::is_integral_v<KeyType>) {
437 equals = (e.type == QCborValue::Integer && e.value == key);
440 equals = stringEqualsElement(i, key);
445 return {
this, i + 1 };
449 if (self.isMap() && self.container) {
450 qsizetype idx = self.container->findCborMapKey(key).i;
451 if (idx < self.container->elements.size())
452 return self.container->valueAt(idx);
456 template <
typename KeyType>
static QCborValueRef
460 qsizetype index = size + 1;
462 size = container->elements.size();
463 index = container->findCborMapKey<KeyType>(key).i;
466 Q_ASSERT((size & 1) == 0);
468 container = detach(container, qMax(index + 1, size));
470 Q_ASSERT((container->elements.size() & 1) == 0);
476 Q_ASSERT(index < container->elements.size());
477 return { container, index };
479 template <
typename KeyType>
static QCborValueRef
findOrAddMapKey(QCborMap &map, KeyType key);
480 template <
typename KeyType>
static QCborValueRef
findOrAddMapKey(QCborValue &self, KeyType key);
481 template <
typename KeyType>
static QCborValueRef
findOrAddMapKey(QCborValueRef self, KeyType key);
483#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
QCborContainerPrivate * nextToDelete
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)
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)
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
Combined button and popup list for selecting options.
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={})