diff options
| author | Thiago Macieira <thiago.macieira@intel.com> | 2021-11-27 10:29:35 -0800 |
|---|---|---|
| committer | Thiago Macieira <thiago.macieira@intel.com> | 2022-02-15 17:00:05 -0800 |
| commit | 705ea62f9e08730793adc7f6cec753c88e66ac4e (patch) | |
| tree | a33dca0c92930c6a061ce228b2f8c0595b469f5f /src/corelib/serialization/qjsonvalue.cpp | |
| parent | a747ab0b72cac1bd9b666bdc27d2f40d900f7e9d (diff) | |
QJsonValueConstRef: prepare for Qt 7
Instead of storing a pointer to the QJsonArray or QJsonObject (like the
Qt 4 & pre-5.15 versions did), do like QCborValueConstRef and store the
pointer to the QCborContainerPrivate.
Unlike QCborValueRef, we must keep the is_object bit because of API
behavior that assigning an Undefined to an object reference deletes it
from the container. I've chosen to use size_t instead of qsizetype for
this because then we don't lose any bits of the index. Therefore, the
index in the case of objects is stored as pair count (like before),
different from QCborValueRef which stores the actual index in the
QCborContainerPrivate.
It's the LSB (on little-endian architectures) so the calculation of
2 * index + 1 is the actual value stored in memory or in the
register. Unfortunately, right now, both Clang and GCC don't realize
this and generate unnecessary instructions. Clang:
0000000000000000 <QJsonValueConstRef::concreteType(QJsonValueConstRef)>:
0: mov %rsi,%rax
3: shr %rax
6: mov %rsi,%rcx
9: or $0x1,%rcx
d: test $0x1,%sil
11: cmove %rax,%rcx
[GCC code is identical, except it uses an AND instead of TEST]
That OR at offset 9 is a no-op because it sets a bit that is already set
in those conditions. At least they don't do unnecessary shifts.
Change-Id: I89446ea06b5742efb194fffd16bb7aadb6a9b341
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'src/corelib/serialization/qjsonvalue.cpp')
| -rw-r--r-- | src/corelib/serialization/qjsonvalue.cpp | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/corelib/serialization/qjsonvalue.cpp b/src/corelib/serialization/qjsonvalue.cpp index 9fecbbb6266..37ead9648eb 100644 --- a/src/corelib/serialization/qjsonvalue.cpp +++ b/src/corelib/serialization/qjsonvalue.cpp @@ -921,6 +921,7 @@ bool QJsonValue::operator!=(const QJsonValue &other) const void QJsonValueRef::detach() { +#if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) QCborContainerPrivate *d = QJsonPrivate::Value::container(*this); d = QCborContainerPrivate::detach(d, d->elements.size()); @@ -928,6 +929,9 @@ void QJsonValueRef::detach() o->o.reset(d); else a->a.reset(d); +#else + d = QCborContainerPrivate::detach(d, d->elements.size()); +#endif } static QJsonValueRef &assignToRef(QJsonValueRef &ref, const QCborValue &value, bool is_object) @@ -1036,6 +1040,18 @@ QJsonValue QJsonValueConstRef::concrete(QJsonValueConstRef self) noexcept return QJsonPrivate::Value::fromTrustedCbor(d->valueAt(index)); } +QString QJsonValueConstRef::objectKey(QJsonValueConstRef self) +{ + Q_ASSERT(self.is_object); + Q_ASSUME(self.is_object); + const QCborContainerPrivate *d = QJsonPrivate::Value::container(self); + qsizetype index = QJsonPrivate::Value::indexHelper(self); + + Q_ASSERT(d); + Q_ASSERT(index < d->elements.size()); + return d->stringAt(index - 1); +} + #if QT_VERSION < QT_VERSION_CHECK(7, 0, 0) && !defined(QT_BOOTSTRAPPED) QVariant QJsonValueRef::toVariant() const { |
