From 0652bdcf5e60461cd9a513d64be00b8830826fad Mon Sep 17 00:00:00 2001 From: Mat Sutcliffe Date: Tue, 16 Jul 2019 21:00:24 +0100 Subject: QJsonObject: add QLatin1String overloads of non-const methods Also optimized the existing QL1S overload of non-const operator[](), and applied Extract Method refactoring to the other existing QL1S overloads. [ChangeLog][QtCore][QJsonObject] Added insert(), remove(), and take() overloads taking QLatin1String. Change-Id: I5e737cf2d7d9ffb325d6981db1e4a6a9f093657b Reviewed-by: Thiago Macieira Reviewed-by: Anton Kudryavtsev --- src/corelib/serialization/qjsonobject.cpp | 148 +++++++++++++++++++++++------- 1 file changed, 113 insertions(+), 35 deletions(-) (limited to 'src/corelib/serialization/qjsonobject.cpp') diff --git a/src/corelib/serialization/qjsonobject.cpp b/src/corelib/serialization/qjsonobject.cpp index 19a16d85373..99ab25f2650 100644 --- a/src/corelib/serialization/qjsonobject.cpp +++ b/src/corelib/serialization/qjsonobject.cpp @@ -397,14 +397,7 @@ QJsonValue QJsonObject::value(const QString &key) const */ QJsonValue QJsonObject::value(QStringView key) const { - if (!d) - return QJsonValue(QJsonValue::Undefined); - - bool keyExists; - int i = o->indexOf(key, &keyExists); - if (!keyExists) - return QJsonValue(QJsonValue::Undefined); - return QJsonValue(d, o, o->entryAt(i)->value); + return valueImpl(key); } /*! @@ -412,6 +405,15 @@ QJsonValue QJsonObject::value(QStringView key) const \since 5.7 */ QJsonValue QJsonObject::value(QLatin1String key) const +{ + return valueImpl(key); +} + +/*! + \internal +*/ +template +QJsonValue QJsonObject::valueImpl(T key) const { if (!d) return QJsonValue(QJsonValue::Undefined); @@ -477,13 +479,7 @@ QJsonValueRef QJsonObject::operator [](const QString &key) */ QJsonValueRef QJsonObject::operator [](QStringView key) { - bool keyExists = false; - int index = o ? o->indexOf(key, &keyExists) : 0; - if (!keyExists) { - iterator i = insertAt(index, key, QJsonValue(), false); - index = i.i; - } - return QJsonValueRef(this, index); + return atImpl(key); } /*! @@ -492,8 +488,22 @@ QJsonValueRef QJsonObject::operator [](QStringView key) */ QJsonValueRef QJsonObject::operator [](QLatin1String key) { - // ### optimize me - return operator[](QString(key)); + return atImpl(key); +} + +/*! + \internal +*/ +template +QJsonValueRef QJsonObject::atImpl(T key) +{ + bool keyExists = false; + int index = o ? o->indexOf(key, &keyExists) : 0; + if (!keyExists) { + iterator i = insertAt(index, key, QJsonValue(), false); + index = i.i; + } + return QJsonValueRef(this, index); } #if QT_STRINGVIEW_LEVEL < 2 @@ -521,6 +531,24 @@ QJsonObject::iterator QJsonObject::insert(const QString &key, const QJsonValue & \since 5.14 */ QJsonObject::iterator QJsonObject::insert(QStringView key, const QJsonValue &value) +{ + return insertImpl(key, value); +} + +/*! + \overload + \since 5.14 +*/ +QJsonObject::iterator QJsonObject::insert(QLatin1String key, const QJsonValue &value) +{ + return insertImpl(key, value); +} + +/*! + \internal +*/ +template +QJsonObject::iterator QJsonObject::insertImpl(T key, const QJsonValue &value) { if (value.t == QJsonValue::Undefined) { remove(key); @@ -534,7 +562,8 @@ QJsonObject::iterator QJsonObject::insert(QStringView key, const QJsonValue &val /*! \internal */ -QJsonObject::iterator QJsonObject::insertAt(int pos, QStringView key, const QJsonValue &value, bool keyExists) +template +QJsonObject::iterator QJsonObject::insertAt(int pos, T key, const QJsonValue &value, bool keyExists) { QJsonValue val = value; @@ -589,6 +618,24 @@ void QJsonObject::remove(const QString &key) \since 5.14 */ void QJsonObject::remove(QStringView key) +{ + removeImpl(key); +} + +/*! + \overload + \since 5.14 +*/ +void QJsonObject::remove(QLatin1String key) +{ + removeImpl(key); +} + +/*! + \internal +*/ +template +void QJsonObject::removeImpl(T key) { if (!d) return; @@ -622,6 +669,24 @@ QJsonValue QJsonObject::take(const QString &key) \since 5.14 */ QJsonValue QJsonObject::take(QStringView key) +{ + return takeImpl(key); +} + +/*! + \overload + \since 5.14 +*/ +QJsonValue QJsonObject::take(QLatin1String key) +{ + return takeImpl(key); +} + +/*! + \internal +*/ +template +QJsonValue QJsonObject::takeImpl(T key) { if (!o) return QJsonValue(QJsonValue::Undefined); @@ -655,12 +720,7 @@ bool QJsonObject::contains(const QString &key) const */ bool QJsonObject::contains(QStringView key) const { - if (!o) - return false; - - bool keyExists; - o->indexOf(key, &keyExists); - return keyExists; + return containsImpl(key); } /*! @@ -668,6 +728,15 @@ bool QJsonObject::contains(QStringView key) const \since 5.7 */ bool QJsonObject::contains(QLatin1String key) const +{ + return containsImpl(key); +} + +/*! + \internal +*/ +template +bool QJsonObject::containsImpl(T key) const { if (!o) return false; @@ -751,12 +820,7 @@ QJsonObject::iterator QJsonObject::find(const QString &key) */ QJsonObject::iterator QJsonObject::find(QStringView key) { - bool keyExists = false; - int index = o ? o->indexOf(key, &keyExists) : 0; - if (!keyExists) - return end(); - detach2(); - return iterator(this, index); + return findImpl(key); } /*! @@ -764,6 +828,15 @@ QJsonObject::iterator QJsonObject::find(QStringView key) \since 5.7 */ QJsonObject::iterator QJsonObject::find(QLatin1String key) +{ + return findImpl(key); +} + +/*! + \internal +*/ +template +QJsonObject::iterator QJsonObject::findImpl(T key) { bool keyExists = false; int index = o ? o->indexOf(key, &keyExists) : 0; @@ -812,11 +885,7 @@ QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const */ QJsonObject::const_iterator QJsonObject::constFind(QStringView key) const { - bool keyExists = false; - int index = o ? o->indexOf(key, &keyExists) : 0; - if (!keyExists) - return end(); - return const_iterator(this, index); + return constFindImpl(key); } /*! @@ -824,6 +893,15 @@ QJsonObject::const_iterator QJsonObject::constFind(QStringView key) const \since 5.7 */ QJsonObject::const_iterator QJsonObject::constFind(QLatin1String key) const +{ + return constFindImpl(key); +} + +/*! + \internal +*/ +template +QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const { bool keyExists = false; int index = o ? o->indexOf(key, &keyExists) : 0; -- cgit v1.2.3