QJsonValueRef: add non-const operator[]
These can't be done before Qt 7, unlike their CBOR counterparts, for binary-compatibility reasons: in 6.x, QJson{Const,}ValueRef must hold a pointer to an extant QJsonArray or QJsonObject. So this is implemented for Qt 7 / bootstrap only for now. Unfortunately, they can't be tested either. Change-Id: I54f205f6b7314351b078fffd16d067d735c4fe2b Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
This commit is contained in:
parent
b852584556
commit
181d7ea053
@ -1072,6 +1072,44 @@ QJsonValue QJsonValueRef::toValue() const
|
||||
{
|
||||
return concrete(*this);
|
||||
}
|
||||
#else
|
||||
QJsonValueRef QJsonValueRef::operator[](qsizetype key)
|
||||
{
|
||||
if (d->elements.at(index).type != QCborValue::Array)
|
||||
d->replaceAt(index, QCborValue::Array);
|
||||
|
||||
auto &e = d->elements[index];
|
||||
e.container = QCborContainerPrivate::grow(e.container, key); // detaches
|
||||
e.flags |= QtCbor::Element::IsContainer;
|
||||
|
||||
return QJsonValueRef(e.container, key, false);
|
||||
}
|
||||
|
||||
QJsonValueRef QJsonValueRef::operator[](QAnyStringView key)
|
||||
{
|
||||
// must go through QJsonObject because some of the machinery is non-static
|
||||
// member or file-static in qjsonobject.cpp
|
||||
QJsonObject o = QJsonPrivate::Value::fromTrustedCbor(d->valueAt(index)).toObject();
|
||||
QJsonValueRef ret = key.visit([&](auto v) {
|
||||
if constexpr (std::is_same_v<decltype(v), QUtf8StringView>)
|
||||
return o[QString::fromUtf8(v)];
|
||||
else
|
||||
return o[v];
|
||||
});
|
||||
|
||||
// ### did the QJsonObject::operator[] above detach?
|
||||
QCborContainerPrivate *x = o.o.take();
|
||||
Q_ASSERT(x->ref.loadRelaxed() == 1);
|
||||
|
||||
auto &e = d->elements[index];
|
||||
if (e.flags & QtCbor::Element::IsContainer && e.container != x)
|
||||
o.o.reset(e.container); // might not an object!
|
||||
|
||||
e.flags |= QtCbor::Element::IsContainer;
|
||||
e.container = x;
|
||||
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
size_t qHash(const QJsonValue &value, size_t seed)
|
||||
|
@ -229,6 +229,10 @@ protected:
|
||||
quint64 is_object : 1;
|
||||
quint64 index : 63;
|
||||
#else
|
||||
constexpr QJsonValueConstRef(QCborContainerPrivate *d, size_t index, bool is_object)
|
||||
: d(d), is_object(is_object), index(index)
|
||||
{}
|
||||
|
||||
// implemented in qjsonarray.h & qjsonobject.h, to get their d
|
||||
QJsonValueConstRef(QJsonArray *array, qsizetype idx);
|
||||
QJsonValueConstRef(QJsonObject *object, qsizetype idx);
|
||||
@ -296,6 +300,9 @@ private:
|
||||
QJsonValue toValue() const;
|
||||
#else
|
||||
using QJsonValueConstRef::operator[];
|
||||
Q_CORE_EXPORT QJsonValueRef operator[](QAnyStringView key);
|
||||
Q_CORE_EXPORT QJsonValueRef operator[](qsizetype i);
|
||||
|
||||
private:
|
||||
using QJsonValueConstRef::QJsonValueConstRef;
|
||||
#endif // < Qt 7
|
||||
|
Loading…
x
Reference in New Issue
Block a user