From fa9a407b9f7b89e4785c650dcb3e80d08ea3c042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Wed, 10 Sep 2014 10:52:14 +0200 Subject: Revert "Reading QJsonObject property should not modify the object itself." This reverts commit 20cf632ad5f3ffe7b0fd231724c971f4e07304eb. The commit produced to many problems during statics destruction. For example causing QtCreator crash (QTBUG-40987). Change-Id: Ib52f6a449c2d84deab2de792559a6a065ca45e8d Reviewed-by: Thiago Macieira Reviewed-by: Lars Knoll --- src/corelib/json/qjson_p.h | 71 ---------------------------------------- src/corelib/json/qjsonobject.cpp | 14 ++++---- src/corelib/json/qjsonvalue.cpp | 7 ++-- src/corelib/json/qjsonvalue.h | 2 -- 4 files changed, 11 insertions(+), 83 deletions(-) (limited to 'src') diff --git a/src/corelib/json/qjson_p.h b/src/corelib/json/qjson_p.h index 2cb26b95a1..104ddaabae 100644 --- a/src/corelib/json/qjson_p.h +++ b/src/corelib/json/qjson_p.h @@ -61,8 +61,6 @@ #include #include #include -#include -#include #include #include @@ -795,75 +793,6 @@ private: Q_DISABLE_COPY(Data) }; -struct ObjectUndefinedKeys -{ - static void insertKey(const QJsonObject *object, int index, const QString &key) - { - QMutexLocker lock(&mutex); - keys()[object][index] = key; - } - static QString takeKey(const QJsonObject *object, int index) - { - QMutexLocker lock(&mutex); - return keys()[object].take(index); - } - static void removeKeys(const QJsonObject *object) - { - QMutexLocker lock(&mutex); - keys().remove(object); - } -private: - typedef QHash > KeysHash; - static KeysHash &keys() - { - static KeysHash keys; - return keys; - } - static QBasicMutex mutex; -}; - -} // namespace QJsonPrivate - -struct QJsonValueRef::UnionHelper -{ - static inline QJsonObject *untaggedPointer(QJsonObject *object) - { - const quintptr Mask = ~quintptr(0) << 2; - return reinterpret_cast(quintptr(object) & Mask); - } - static inline QJsonObject *taggedPointer(QJsonObject *object) - { - const quintptr Mask = 1; - return reinterpret_cast(quintptr(object) | Mask); - } - - static void setValueAt(QJsonValueRef *ref, QJsonValue value) - { - using namespace QJsonPrivate; - QJsonObject *object = untaggedPointer(ref->o); - if (ref->o != object) - object->insert(ObjectUndefinedKeys::takeKey(object, ref->index), value); - else - object->setValueAt(ref->index, value); - } - - static QJsonValue valueAt(const QJsonValueRef *ref) - { - QJsonObject *object = untaggedPointer(ref->o); - if (ref->o != object) - return QJsonValue::Undefined; - return ref->o->valueAt(ref->index); - } -}; - -/*! - \internal - Constructor that creates reference to an undefined value in \a object. -*/ -inline QJsonValueRef::QJsonValueRef(QJsonObject *object, const QString &key) - : o(UnionHelper::taggedPointer(object)), is_object(true), index(qHash(key)) -{ - QJsonPrivate::ObjectUndefinedKeys::insertKey(object, index, key); } QT_END_NAMESPACE diff --git a/src/corelib/json/qjsonobject.cpp b/src/corelib/json/qjsonobject.cpp index b393701411..00cc2ddbd3 100644 --- a/src/corelib/json/qjsonobject.cpp +++ b/src/corelib/json/qjsonobject.cpp @@ -161,7 +161,6 @@ QJsonObject::~QJsonObject() { if (d && !d->ref.deref()) delete d; - QJsonPrivate::ObjectUndefinedKeys::removeKeys(this); // ### Qt6 move it to ~QJsonValueRef } /*! @@ -318,9 +317,14 @@ QJsonValue QJsonObject::operator [](const QString &key) const */ QJsonValueRef QJsonObject::operator [](const QString &key) { + // ### somewhat inefficient, as we lookup the key twice if it doesn't yet exist bool keyExists = false; int index = o ? o->indexOf(key, &keyExists) : -1; - return keyExists ? QJsonValueRef(this, index) : QJsonValueRef(this, key); + if (!keyExists) { + iterator i = insert(key, QJsonValue()); + index = i.i; + } + return QJsonValueRef(this, index); } /*! @@ -1034,9 +1038,7 @@ void QJsonObject::compact() detach(); d->compact(); - using namespace QJsonPrivate; - o = static_cast(d->header->root()); - ObjectUndefinedKeys::removeKeys(this); // ### Qt6 move it to ~QJsonValueRef + o = static_cast(d->header->root()); } /*! @@ -1073,8 +1075,6 @@ void QJsonObject::setValueAt(int i, const QJsonValue &val) insert(e->key(), val); } -QBasicMutex QJsonPrivate::ObjectUndefinedKeys::mutex; - #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) QDebug operator<<(QDebug dbg, const QJsonObject &o) { diff --git a/src/corelib/json/qjsonvalue.cpp b/src/corelib/json/qjsonvalue.cpp index 6e40308de3..2d957070ab 100644 --- a/src/corelib/json/qjsonvalue.cpp +++ b/src/corelib/json/qjsonvalue.cpp @@ -673,10 +673,11 @@ void QJsonValue::detach() However, they are not explicitly documented here. */ + QJsonValueRef &QJsonValueRef::operator =(const QJsonValue &val) { if (is_object) - UnionHelper::setValueAt(this, val); + o->setValueAt(index, val); else a->replace(index, val); @@ -686,7 +687,7 @@ QJsonValueRef &QJsonValueRef::operator =(const QJsonValue &val) QJsonValueRef &QJsonValueRef::operator =(const QJsonValueRef &ref) { if (is_object) - UnionHelper::setValueAt(this, ref); + o->setValueAt(index, ref); else a->replace(index, ref); @@ -707,7 +708,7 @@ QJsonValue QJsonValueRef::toValue() const { if (!is_object) return a->at(index); - return UnionHelper::valueAt(this); + return o->valueAt(index); } #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) diff --git a/src/corelib/json/qjsonvalue.h b/src/corelib/json/qjsonvalue.h index 2d0453f130..a35ab9ef4a 100644 --- a/src/corelib/json/qjsonvalue.h +++ b/src/corelib/json/qjsonvalue.h @@ -149,7 +149,6 @@ public: : a(array), is_object(false), index(idx) {} QJsonValueRef(QJsonObject *object, int idx) : o(object), is_object(true), index(idx) {} - inline QJsonValueRef(QJsonObject *object, const QString &key); inline operator QJsonValue() const { return toValue(); } QJsonValueRef &operator = (const QJsonValue &val); @@ -189,7 +188,6 @@ private: }; uint is_object : 1; uint index : 31; - struct UnionHelper; }; #ifndef Q_QDOC -- cgit v1.2.3