QCborValue: merge the const operator[] into a template

Change-Id: I5e52dc5b093c43a3b678fffd16b6ec4b7d841955
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Thiago Macieira 2021-11-12 14:15:23 -08:00
parent 072f0d5717
commit 832fcbf37f
3 changed files with 17 additions and 14 deletions

View File

@ -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)

View File

@ -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); }

View File

@ -420,7 +420,7 @@ public:
}
// doesn't apply to JSON
template <typename KeyType> QCborValueRef findCborMapKey(KeyType key)
template <typename KeyType> 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 <typename KeyType> 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 <typename KeyType> static QCborValueRef
findOrAddMapKey(QCborContainerPrivate *container, KeyType key)