diff options
author | Mat Sutcliffe <oktal3700@gmail.com> | 2019-07-02 02:30:26 +0100 |
---|---|---|
committer | Mat Sutcliffe <oktal3700@gmail.com> | 2019-07-20 12:04:14 +0100 |
commit | a4e9fa03cabfc11105e700e38184a70888da4e7a (patch) | |
tree | 7c31003488c22c5a648a20edf7006c6715d24bb9 /src/corelib/serialization | |
parent | 8010e906d3612aface0daccde41d1a65fca04b0c (diff) |
QJsonObject: minor refactoring
Applied DRY principle.
Change-Id: Ic3035552c6174167b4fe19fd4c825500dff16ded
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Anton Kudryavtsev <antkudr@mail.ru>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/serialization')
-rw-r--r-- | src/corelib/serialization/qjsonobject.cpp | 40 | ||||
-rw-r--r-- | 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 a13dd3f9e7..e800d76ab6 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; @@ -1268,6 +1256,15 @@ void QJsonObject::compact() /*! \internal */ +void QJsonObject::compactIfNeeded() +{ + if (d->compactionCounter > 32u && d->compactionCounter >= unsigned(o->length) / 2u) + compact(); +} + +/*! + \internal + */ QString QJsonObject::keyAt(int i) const { Q_ASSERT(o && i >= 0 && i < (int)o->length); @@ -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 7f7a17e259..b12769d3d3 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; |