QCborMap: merge the constFind methods into a template

Change-Id: I5e52dc5b093c43a3b678fffd16b6e558c4e1f089
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Thiago Macieira 2021-11-12 12:08:04 -08:00
parent 3531f578d3
commit 11ea172178
2 changed files with 26 additions and 23 deletions

View File

@ -932,12 +932,7 @@ QCborMap::iterator QCborMap::find(const QCborValue &key)
*/
QCborMap::const_iterator QCborMap::constFind(qint64 key) const
{
for (qsizetype i = 0; i < 2 * size(); i += 2) {
const auto &e = d->elements.at(i);
if (e.type == QCborValue::Integer && e.value == key)
return { d.data(), i + 1 };
}
return constEnd();
return d ? d->findCborMapKey(key) : constEnd();
}
/*!
@ -958,11 +953,7 @@ QCborMap::const_iterator QCborMap::constFind(qint64 key) const
*/
QCborMap::const_iterator QCborMap::constFind(QLatin1String key) const
{
for (qsizetype i = 0; i < 2 * size(); i += 2) {
if (d->stringEqualsElement(i, key))
return { d.data(), i + 1 };
}
return constEnd();
return d ? d->findCborMapKey(key) : constEnd();
}
/*!
@ -981,13 +972,9 @@ QCborMap::const_iterator QCborMap::constFind(QLatin1String key) const
remove(const QString &), contains(const QString &)
value(qint64), value(QLatin1String), value(const QCborValue &)
*/
QCborMap::const_iterator QCborMap::constFind(const QString & key) const
QCborMap::const_iterator QCborMap::constFind(const QString &key) const
{
for (qsizetype i = 0; i < 2 * size(); i += 2) {
if (d->stringEqualsElement(i, key))
return { d.data(), i + 1 };
}
return constEnd();
return d ? d->findCborMapKey(qToStringViewIgnoringNull(key)) : constEnd();
}
/*!
@ -1008,12 +995,7 @@ QCborMap::const_iterator QCborMap::constFind(const QString & key) const
*/
QCborMap::const_iterator QCborMap::constFind(const QCborValue &key) const
{
for (qsizetype i = 0; i < 2 * size(); i += 2) {
int cmp = d->compareElement(i, key);
if (cmp == 0)
return { d.data(), i + 1 };
}
return constEnd();
return d ? d->findCborMapKey<const QCborValue &>(key) : constEnd();
}
/*!

View File

@ -419,6 +419,27 @@ public:
elements.remove(idx);
}
// doesn't apply to JSON
template <typename KeyType> QCborValueRef findCborMapKey(KeyType key)
{
qsizetype i = 0;
for ( ; i < elements.size(); i += 2) {
const auto &e = elements.at(i);
bool equals;
if constexpr (std::is_same_v<std::decay_t<KeyType>, QCborValue>) {
equals = (compareElement(i, key) == 0);
} else if constexpr (std::is_integral_v<KeyType>) {
equals = (e.type == QCborValue::Integer && e.value == key);
} else {
// assume it's a string
equals = stringEqualsElement(i, key);
}
if (equals)
break;
}
return QCborValueRef{ this, i + 1 };
}
#if QT_CONFIG(cborstreamreader)
void decodeValueFromCbor(QCborStreamReader &reader, int remainingStackDepth);
void decodeStringFromCbor(QCborStreamReader &reader);