From 832fcbf37f41160947955421522bb4f783059817 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 12 Nov 2021 14:15:23 -0800 Subject: [PATCH] QCborValue: merge the const operator[] into a template Change-Id: I5e52dc5b093c43a3b678fffd16b6ec4b7d841955 Reviewed-by: Edward Welbourne --- src/corelib/serialization/qcborvalue.cpp | 16 +++++----------- src/corelib/serialization/qcborvalue.h | 2 +- src/corelib/serialization/qcborvalue_p.h | 13 +++++++++++-- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/corelib/serialization/qcborvalue.cpp b/src/corelib/serialization/qcborvalue.cpp index a4ebf31d5e..36d09b05f7 100644 --- a/src/corelib/serialization/qcborvalue.cpp +++ b/src/corelib/serialization/qcborvalue.cpp @@ -2169,9 +2169,7 @@ QCborMap QCborValue::toMap(const QCborMap &defaultValue) const */ const QCborValue QCborValue::operator[](const QString &key) const { - if (isMap()) - return toMap().value(key); - return QCborValue(); + return QCborContainerPrivate::findCborMapKey(*this, qToStringViewIgnoringNull(key)); } /*! @@ -2190,9 +2188,7 @@ const QCborValue QCborValue::operator[](const QString &key) const */ const QCborValue QCborValue::operator[](QLatin1String key) const { - if (isMap()) - return toMap().value(key); - return QCborValue(); + return QCborContainerPrivate::findCborMapKey(*this, key); } /*! @@ -2208,11 +2204,9 @@ const QCborValue QCborValue::operator[](QLatin1String key) const */ const QCborValue QCborValue::operator[](qint64 key) const { - if (isMap()) - return toMap().value(key); - if (isArray()) - return toArray().at(key); - return QCborValue(); + if (isArray() && container && quint64(key) < quint64(container->elements.size())) + return container->valueAt(key); + return QCborContainerPrivate::findCborMapKey(*this, key); } static bool shouldArrayRemainArray(qint64 key, QCborValue::Type t, QCborContainerPrivate *container) diff --git a/src/corelib/serialization/qcborvalue.h b/src/corelib/serialization/qcborvalue.h index 710165057c..d7b5ec68aa 100644 --- a/src/corelib/serialization/qcborvalue.h +++ b/src/corelib/serialization/qcborvalue.h @@ -437,9 +437,9 @@ public: protected: friend class QCborValue; - friend class QCborValueRef; friend class QCborArray; friend class QCborMap; + friend class QCborContainerPrivate; QCborValue concrete() const noexcept { return concrete(*this); } diff --git a/src/corelib/serialization/qcborvalue_p.h b/src/corelib/serialization/qcborvalue_p.h index ade29df0f7..879306ce06 100644 --- a/src/corelib/serialization/qcborvalue_p.h +++ b/src/corelib/serialization/qcborvalue_p.h @@ -420,7 +420,7 @@ public: } // doesn't apply to JSON - template QCborValueRef findCborMapKey(KeyType key) + template QCborValueConstRef findCborMapKey(KeyType key) { qsizetype i = 0; for ( ; i < elements.size(); i += 2) { @@ -437,7 +437,16 @@ public: if (equals) break; } - return QCborValueRef{ this, i + 1 }; + return { this, i + 1 }; + } + template static QCborValue findCborMapKey(const QCborValue &self, KeyType key) + { + if (self.isMap() && self.container) { + qsizetype idx = self.container->findCborMapKey(key).i; + if (idx < self.container->elements.size()) + return self.container->valueAt(idx); + } + return QCborValue(); } template static QCborValueRef findOrAddMapKey(QCborContainerPrivate *container, KeyType key)