From 072f0d5717eefeffea3fe72135f9f05135eece71 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Fri, 12 Nov 2021 12:51:11 -0800 Subject: [PATCH] QCborValue: merge the non-const operator[] into a template Change-Id: I5e52dc5b093c43a3b678fffd16b6e7b32b3e2835 Reviewed-by: Edward Welbourne --- src/corelib/serialization/qcborvalue.cpp | 96 ++++++------------------ src/corelib/serialization/qcborvalue_p.h | 1 + 2 files changed, 24 insertions(+), 73 deletions(-) diff --git a/src/corelib/serialization/qcborvalue.cpp b/src/corelib/serialization/qcborvalue.cpp index c0f8a9a40db..a4ebf31d5e3 100644 --- a/src/corelib/serialization/qcborvalue.cpp +++ b/src/corelib/serialization/qcborvalue.cpp @@ -2251,6 +2251,7 @@ static Q_DECL_COLD_FUNCTION QCborMap arrayAsMap(const QCborArray &array) /*! \internal */ +[[maybe_unused]] static QCborContainerPrivate *maybeDetach(QCborContainerPrivate *container, qsizetype size) { auto replace = QCborContainerPrivate::detach(container, size); @@ -2282,6 +2283,25 @@ static QCborContainerPrivate *maybeGrow(QCborContainerPrivate *container, qsizet return replace; } +template inline QCborValueRef +QCborContainerPrivate::findOrAddMapKey(QCborValue &self, KeyType key) +{ + // we need a map, so convert if necessary + if (self.isArray()) + self = arrayAsMap(self.toArray()); + else if (!self.isMap()) + self = QCborValue(QCborValue::Map); + + QCborValueRef result = findOrAddMapKey(self.container, key); + if (result.d != self.container) { + if (self.container) + self.container->deref(); + self.container = result.d; + self.container->ref.ref(); + } + return result; +} + /*! Returns a QCborValueRef that can be used to read or modify the entry in this, as a map, with the given \a key. When this QCborValue is a QCborMap, @@ -2297,30 +2317,7 @@ static QCborContainerPrivate *maybeGrow(QCborContainerPrivate *container, qsizet */ QCborValueRef QCborValue::operator[](const QString &key) { - if (!isMap()) - *this = QCborValue(isArray() ? arrayAsMap(toArray()) : QCborMap()); - - const qsizetype size = container ? container->elements.size() : 0; - qsizetype index = size + 1; - bool found = false; - if (container) { - QCborMap proxy(*container); - auto it = proxy.constFind(key); - if (it < proxy.constEnd()) { - found = true; - index = it.item.i; - } - } - - container = maybeDetach(container, size + (found ? 0 : 2)); - Q_ASSERT(container); - if (!found) { - container->append(key); - container->append(QCborValue()); - } - Q_ASSERT(index & 1 && !(container->elements.size() & 1)); - Q_ASSERT(index < container->elements.size()); - return { container, index }; + return QCborContainerPrivate::findOrAddMapKey(*this, qToStringViewIgnoringNull(key)); } /*! @@ -2340,30 +2337,7 @@ QCborValueRef QCborValue::operator[](const QString &key) */ QCborValueRef QCborValue::operator[](QLatin1String key) { - if (!isMap()) - *this = QCborValue(isArray() ? arrayAsMap(toArray()) : QCborMap()); - - const qsizetype size = container ? container->elements.size() : 0; - qsizetype index = size + 1; - bool found = false; - if (container) { - QCborMap proxy(*container); - auto it = proxy.constFind(key); - if (it < proxy.constEnd()) { - found = true; - index = it.item.i; - } - } - - container = maybeDetach(container, size + (found ? 0 : 2)); - Q_ASSERT(container); - if (!found) { - container->append(key); - container->append(QCborValue()); - } - Q_ASSERT(index & 1 && !(container->elements.size() & 1)); - Q_ASSERT(index < container->elements.size()); - return { container, index }; + return QCborContainerPrivate::findOrAddMapKey(*this, key); } /*! @@ -2388,31 +2362,7 @@ QCborValueRef QCborValue::operator[](qint64 key) container = maybeGrow(container, key); return { container, qsizetype(key) }; } - if (!isMap()) - *this = QCborValue(isArray() ? arrayAsMap(toArray()) : QCborMap()); - - const qsizetype size = container ? container->elements.size() : 0; - Q_ASSERT(!(size & 1)); - qsizetype index = size + 1; - bool found = false; - if (container) { - QCborMap proxy(*container); - auto it = proxy.constFind(key); - if (it < proxy.constEnd()) { - found = true; - index = it.item.i; - } - } - - container = maybeDetach(container, size + (found ? 0 : 2)); - Q_ASSERT(container); - if (!found) { - container->append(key); - container->append(QCborValue()); - } - Q_ASSERT(index & 1 && !(container->elements.size() & 1)); - Q_ASSERT(index < container->elements.size()); - return { container, index }; + return QCborContainerPrivate::findOrAddMapKey(*this, key); } #if QT_CONFIG(cborstreamreader) diff --git a/src/corelib/serialization/qcborvalue_p.h b/src/corelib/serialization/qcborvalue_p.h index 185b74bc932..ade29df0f77 100644 --- a/src/corelib/serialization/qcborvalue_p.h +++ b/src/corelib/serialization/qcborvalue_p.h @@ -463,6 +463,7 @@ public: return { container, index }; } template static QCborValueRef findOrAddMapKey(QCborMap &map, KeyType key); + template static QCborValueRef findOrAddMapKey(QCborValue &self, KeyType key); #if QT_CONFIG(cborstreamreader) void decodeValueFromCbor(QCborStreamReader &reader, int remainingStackDepth);