From a4e9fa03cabfc11105e700e38184a70888da4e7a Mon Sep 17 00:00:00 2001 From: Mat Sutcliffe Date: Tue, 2 Jul 2019 02:30:26 +0100 Subject: [PATCH] QJsonObject: minor refactoring Applied DRY principle. Change-Id: Ic3035552c6174167b4fe19fd4c825500dff16ded Reviewed-by: Marc Mutz Reviewed-by: Anton Kudryavtsev Reviewed-by: Thiago Macieira --- src/corelib/serialization/qjsonobject.cpp | 40 ++++++++++++++--------- src/corelib/serialization/qjsonobject.h | 2 ++ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/corelib/serialization/qjsonobject.cpp b/src/corelib/serialization/qjsonobject.cpp index a13dd3f9e73..e800d76ab6b 100644 --- a/src/corelib/serialization/qjsonobject.cpp +++ b/src/corelib/serialization/qjsonobject.cpp @@ -525,8 +525,7 @@ QJsonObject::iterator QJsonObject::insertAt(int pos, const QString &key, const Q if (valueSize) QJsonPrivate::Value::copyData(val, (char *)e + valueOffset, latinOrIntValue); - if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u) - compact(); + compactIfNeeded(); return iterator(this, pos); } @@ -546,11 +545,7 @@ void QJsonObject::remove(const QString &key) if (!keyExists) return; - detach2(); - o->removeItems(index, 1); - ++d->compactionCounter; - if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u) - compact(); + removeAt(index); } /*! @@ -573,11 +568,7 @@ QJsonValue QJsonObject::take(const QString &key) return QJsonValue(QJsonValue::Undefined); QJsonValue v(d, o, o->entryAt(index)->value); - detach2(); - o->removeItems(index, 1); - ++d->compactionCounter; - if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u) - compact(); + removeAt(index); return v; } @@ -659,10 +650,7 @@ QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it) int index = it.i; - o->removeItems(index, 1); - ++d->compactionCounter; - if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u) - compact(); + removeAt(index); // iterator hasn't changed return it; @@ -1265,6 +1253,15 @@ void QJsonObject::compact() o = static_cast(d->header->root()); } +/*! + \internal + */ +void QJsonObject::compactIfNeeded() +{ + if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u) + compact(); +} + /*! \internal */ @@ -1299,6 +1296,17 @@ void QJsonObject::setValueAt(int i, const QJsonValue &val) insertAt(i, e->key(), val, true); } +/*! + \internal + */ +void QJsonObject::removeAt(int index) +{ + detach2(); + o->removeItems(index, 1); + ++d->compactionCounter; + compactIfNeeded(); +} + uint qHash(const QJsonObject &object, uint seed) { QtPrivate::QHashCombine hash; diff --git a/src/corelib/serialization/qjsonobject.h b/src/corelib/serialization/qjsonobject.h index 7f7a17e2591..b12769d3d3c 100644 --- a/src/corelib/serialization/qjsonobject.h +++ b/src/corelib/serialization/qjsonobject.h @@ -247,10 +247,12 @@ private: void detach(uint reserve = 0); bool detach2(uint reserve = 0); void compact(); + void compactIfNeeded(); QString keyAt(int i) const; QJsonValue valueAt(int i) const; void setValueAt(int i, const QJsonValue &val); + void removeAt(int i); iterator insertAt(int i, const QString &key, const QJsonValue &val, bool exists); QJsonPrivate::Data *d;