From 625bb0520708879ef5d281ab0c62fc7ad5415441 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Thu, 6 Oct 2011 14:14:40 +1000 Subject: [PATCH] Allow incubators to be driven recursively Change-Id: If8ce239372c3cf3166b666329ba812b25ee54669 Reviewed-on: http://codereview.qt-project.org/6102 Reviewed-by: Aaron Kennedy --- src/declarative/qml/ftw/ftw.pri | 1 + src/declarative/qml/ftw/qrecursionwatcher_p.h | 105 ++++++++++++++++++++ src/declarative/qml/qdeclarativeincubator.cpp | 24 ++++- src/declarative/qml/qdeclarativeincubator_p.h | 3 + src/declarative/qml/qdeclarativevme.cpp | 21 +++- src/declarative/qml/qdeclarativevme_p.h | 2 + .../data/recursiveClear.1.qml | 9 ++ .../data/recursiveClear.2.qml | 8 ++ .../tst_qdeclarativeincubator.cpp | 47 +++++++++ 9 files changed, 212 insertions(+), 8 deletions(-) create mode 100644 src/declarative/qml/ftw/qrecursionwatcher_p.h create mode 100644 tests/auto/declarative/qdeclarativeincubator/data/recursiveClear.1.qml create mode 100644 tests/auto/declarative/qdeclarativeincubator/data/recursiveClear.2.qml diff --git a/src/declarative/qml/ftw/ftw.pri b/src/declarative/qml/ftw/ftw.pri index 2d6729b..383520a 100644 --- a/src/declarative/qml/ftw/ftw.pri +++ b/src/declarative/qml/ftw/ftw.pri @@ -13,6 +13,7 @@ HEADERS += \ $$PWD/qhashfield_p.h \ $$PWD/qdeclarativethread_p.h \ $$PWD/qfinitestack_p.h \ + $$PWD/qrecursionwatcher_p.h \ SOURCES += \ $$PWD/qintrusivelist.cpp \ diff --git a/src/declarative/qml/ftw/qrecursionwatcher_p.h b/src/declarative/qml/ftw/qrecursionwatcher_p.h new file mode 100644 index 0000000..6ad6821 --- /dev/null +++ b/src/declarative/qml/ftw/qrecursionwatcher_p.h @@ -0,0 +1,105 @@ +/**************************************************************************** +** +** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the QtDeclarative module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** GNU Lesser General Public License Usage +** This file may be used under the terms of the GNU Lesser General Public +** License version 2.1 as published by the Free Software Foundation and +** appearing in the file LICENSE.LGPL included in the packaging of this +** file. Please review the following information to ensure the GNU Lesser +** General Public License version 2.1 requirements will be met: +** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU General +** Public License version 3.0 as published by the Free Software Foundation +** and appearing in the file LICENSE.GPL included in the packaging of this +** file. Please review the following information to ensure the GNU General +** Public License version 3.0 requirements will be met: +** http://www.gnu.org/copyleft/gpl.html. +** +** Other Usage +** Alternatively, this file may be used in accordance with the terms and +** conditions contained in a signed written agreement between you and Nokia. +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QRECURSIONWATCHER_P_H +#define QRECURSIONWATCHER_P_H + +// +// W A R N I N G +// ------------- +// +// This file is not part of the Qt API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. +// + +#include + +QT_BEGIN_NAMESPACE + +class QRecursionNode; +class QRecursionNode { +public: + inline QRecursionNode(); + bool *_r; +}; + +template +class QRecursionWatcher { +public: + inline QRecursionWatcher(T *); + inline ~QRecursionWatcher(); + inline bool hasRecursed() const; +private: + T *_t; + bool _r; +}; + +QRecursionNode::QRecursionNode() +: _r(0) +{ +} + +template +QRecursionWatcher::QRecursionWatcher(T *t) +: _t(t), _r(false) +{ + if ((_t->*Node)._r) *(_t->*Node)._r = true; + (_t->*Node)._r = &_r; +} + +template +QRecursionWatcher::~QRecursionWatcher() +{ + if ((_t->*Node)._r == &_r) (_t->*Node)._r = 0; +} + +template +bool QRecursionWatcher::hasRecursed() const +{ + return _r; +} + +QT_END_NAMESPACE + +#endif // QRECURSIONWATCHER_P_H diff --git a/src/declarative/qml/qdeclarativeincubator.cpp b/src/declarative/qml/qdeclarativeincubator.cpp index 4779caf..ae1ae2d 100644 --- a/src/declarative/qml/qdeclarativeincubator.cpp +++ b/src/declarative/qml/qdeclarativeincubator.cpp @@ -49,7 +49,6 @@ // XXX TODO // - check that the Component.onCompleted behavior is the same as 4.8 in the synchronous and // async if nested cases -// - implement QDeclarativeIncubator::clear() void QDeclarativeEnginePrivate::incubate(QDeclarativeIncubator &i, QDeclarativeContextData *forContext) { QDeclarativeIncubatorPrivate *p = i.d; @@ -239,6 +238,9 @@ void QDeclarativeIncubationController::incubatingObjectCountChanged(int incubati void QDeclarativeIncubatorPrivate::incubate(QDeclarativeVME::Interrupt &i) { + typedef QDeclarativeIncubatorPrivate IP; + QRecursionWatcher watcher(this); + QDeclarativeEngine *engine = component->engine; QDeclarativeEnginePrivate *enginePriv = QDeclarativeEnginePrivate::get(engine); @@ -257,9 +259,13 @@ void QDeclarativeIncubatorPrivate::incubate(QDeclarativeVME::Interrupt &i) if (progress == QDeclarativeIncubatorPrivate::Execute) { enginePriv->referenceScarceResources(); - result = vme.execute(&errors, i); + QObject *tresult = vme.execute(&errors, i); enginePriv->dereferenceScarceResources(); + if (watcher.hasRecursed()) + return; + + result = tresult; if (errors.isEmpty() && result == 0) goto finishIncubate; @@ -271,6 +277,9 @@ void QDeclarativeIncubatorPrivate::incubate(QDeclarativeVME::Interrupt &i) q->setInitialState(result); } + if (watcher.hasRecursed()) + return; + if (errors.isEmpty()) progress = QDeclarativeIncubatorPrivate::Completing; else @@ -278,12 +287,18 @@ void QDeclarativeIncubatorPrivate::incubate(QDeclarativeVME::Interrupt &i) q->statusChanged(q->status()); + if (watcher.hasRecursed()) + return; + if (i.shouldInterrupt()) goto finishIncubate; } if (progress == QDeclarativeIncubatorPrivate::Completing) { do { + if (watcher.hasRecursed()) + return; + if (vme.complete(i)) { progress = QDeclarativeIncubatorPrivate::Completed; goto finishIncubate; @@ -477,6 +492,9 @@ Ready state, the created object is \b not deleted. */ void QDeclarativeIncubator::clear() { + typedef QDeclarativeIncubatorPrivate IP; + QRecursionWatcher watcher(d); + Status s = status(); if (s == Null) @@ -486,7 +504,7 @@ void QDeclarativeIncubator::clear() if (s == Loading) { Q_ASSERT(d->component); enginePriv = QDeclarativeEnginePrivate::get(d->component->engine); - delete d->result; + if (d->result) d->result->deleteLater(); d->result = 0; } diff --git a/src/declarative/qml/qdeclarativeincubator_p.h b/src/declarative/qml/qdeclarativeincubator_p.h index e71602a..d45f6d1 100644 --- a/src/declarative/qml/qdeclarativeincubator_p.h +++ b/src/declarative/qml/qdeclarativeincubator_p.h @@ -44,6 +44,7 @@ #include #include +#include #include // @@ -85,6 +86,8 @@ public: typedef QDeclarativeEnginePrivate::Incubator QIPBase; QIntrusiveList waitingFor; + QRecursionNode recursion; + void clear(); void incubate(QDeclarativeVME::Interrupt &i); diff --git a/src/declarative/qml/qdeclarativevme.cpp b/src/declarative/qml/qdeclarativevme.cpp index 8f34fb5..53bcd8c 100644 --- a/src/declarative/qml/qdeclarativevme.cpp +++ b/src/declarative/qml/qdeclarativevme.cpp @@ -205,11 +205,13 @@ static void removeBindingOnProperty(QObject *o, int index) QML_BEGIN_INSTR_COMMON(I) # define QML_NEXT_INSTR(I) { \ + if (watcher.hasRecursed()) return 0; \ genericInstr = reinterpret_cast(INSTRUCTIONSTREAM); \ goto *genericInstr->common.code; \ } # define QML_END_INSTR(I) } \ + if (watcher.hasRecursed()) return 0; \ genericInstr = reinterpret_cast(INSTRUCTIONSTREAM); \ if (interrupt.shouldInterrupt()) return 0; \ goto *genericInstr->common.code; @@ -219,9 +221,11 @@ static void removeBindingOnProperty(QObject *o, int index) case QDeclarativeInstruction::I: \ QML_BEGIN_INSTR_COMMON(I) -# define QML_NEXT_INSTR(I) break; +# define QML_NEXT_INSTR(I) \ + if (watcher.hasRecursed()) return 0; \ + break; # define QML_END_INSTR(I) \ - if (interrupt.shouldInterrupt()) return 0; \ + if (watcher.hasRecursed() || interrupt.shouldInterrupt()) return 0; \ } break; #endif @@ -259,6 +263,8 @@ QObject *QDeclarativeVME::run(QList *errors, QDeclarativePropertyPrivate::WriteFlags flags = QDeclarativePropertyPrivate::BypassInterceptor | QDeclarativePropertyPrivate::RemoveBindingOnAliasWrite; + QRecursionWatcher watcher(this); + #define COMP states.top().compiledData #define CTXT states.top().context #define INSTRUCTIONSTREAM states.top().instructionStream @@ -1173,6 +1179,8 @@ void QDeclarativeVME::reset() { Q_ASSERT(!states.isEmpty() || objects.isEmpty()); + QRecursionWatcher watcher(this); + if (!objects.isEmpty() && !(states.at(0).flags & State::Deferred)) delete objects.at(0); @@ -1324,6 +1332,7 @@ bool QDeclarativeVME::complete(const Interrupt &interrupt) return true; ActiveVMERestorer restore(this, QDeclarativeEnginePrivate::get(engine)); + QRecursionWatcher watcher(this); while (!bindValues.isEmpty()) { QDeclarativeAbstractBinding *b = bindValues.pop(); @@ -1334,7 +1343,7 @@ bool QDeclarativeVME::complete(const Interrupt &interrupt) QDeclarativePropertyPrivate::DontRemoveBinding); } - if (interrupt.shouldInterrupt()) + if (watcher.hasRecursed() || interrupt.shouldInterrupt()) return false; } bindValues.deallocate(); @@ -1347,7 +1356,7 @@ bool QDeclarativeVME::complete(const Interrupt &interrupt) status->componentComplete(); } - if (interrupt.shouldInterrupt()) + if (watcher.hasRecursed() || interrupt.shouldInterrupt()) return false; } parserStatus.deallocate(); @@ -1361,7 +1370,7 @@ bool QDeclarativeVME::complete(const Interrupt &interrupt) a->add(&d->context->componentAttached); emit a->completed(); - if (interrupt.shouldInterrupt()) + if (watcher.hasRecursed() || interrupt.shouldInterrupt()) return false; } @@ -1375,6 +1384,8 @@ bool QDeclarativeVME::complete(const Interrupt &interrupt) void *args[] = { 0 }; QMetaObject::metacall(obj, QMetaObject::InvokeMetaMethod, callback.second, args); } + if (watcher.hasRecursed()) + return false; } finalizeCallbacks.clear(); diff --git a/src/declarative/qml/qdeclarativevme_p.h b/src/declarative/qml/qdeclarativevme_p.h index 771a2b4..5809ccc 100644 --- a/src/declarative/qml/qdeclarativevme_p.h +++ b/src/declarative/qml/qdeclarativevme_p.h @@ -56,6 +56,7 @@ #include "qdeclarativeerror.h" #include "private/qbitfield_p.h" #include "private/qdeclarativeinstruction_p.h" +#include "private/qrecursionwatcher_p.h" #include #include @@ -141,6 +142,7 @@ private: #endif QDeclarativeEngine *engine; + QRecursionNode recursion; QFiniteStack objects; QFiniteStack lists; diff --git a/tests/auto/declarative/qdeclarativeincubator/data/recursiveClear.1.qml b/tests/auto/declarative/qdeclarativeincubator/data/recursiveClear.1.qml new file mode 100644 index 0000000..748a3f0 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeincubator/data/recursiveClear.1.qml @@ -0,0 +1,9 @@ +import QtQuick 2.0 + +Rectangle { + objectName: "switchMe" + signal switchMe + width: 100; height: 100 + color: "green" + Component.onCompleted: switchMe() +} diff --git a/tests/auto/declarative/qdeclarativeincubator/data/recursiveClear.2.qml b/tests/auto/declarative/qdeclarativeincubator/data/recursiveClear.2.qml new file mode 100644 index 0000000..e96ac00 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeincubator/data/recursiveClear.2.qml @@ -0,0 +1,8 @@ +import QtQuick 2.0 + +Rectangle { + objectName: "blue" + width: 100 + height: 100 + color: "blue" +} diff --git a/tests/auto/declarative/qdeclarativeincubator/tst_qdeclarativeincubator.cpp b/tests/auto/declarative/qdeclarativeincubator/tst_qdeclarativeincubator.cpp index f870ffb..52ddf81 100644 --- a/tests/auto/declarative/qdeclarativeincubator/tst_qdeclarativeincubator.cpp +++ b/tests/auto/declarative/qdeclarativeincubator/tst_qdeclarativeincubator.cpp @@ -78,6 +78,7 @@ private slots: void forceCompletion(); void setInitialState(); void clearDuringCompletion(); + void recursiveClear(); private: QDeclarativeIncubationController controller; @@ -422,10 +423,56 @@ void tst_qdeclarativeincubator::clearDuringCompletion() QPointer srt = SelfRegisteringType::me(); incubator.clear(); + QCoreApplication::processEvents(QEventLoop::DeferredDeletion); QVERIFY(incubator.isNull()); QVERIFY(srt.isNull()); } +class Switcher : public QObject +{ + Q_OBJECT +public: + Switcher(QDeclarativeEngine *e) : QObject(), engine(e) { } + + struct MyIncubator : public QDeclarativeIncubator + { + MyIncubator(QDeclarativeIncubator::IncubationMode mode, QObject *s) + : QDeclarativeIncubator(mode), switcher(s) {} + + virtual void setInitialState(QObject *o) { + if (o->objectName() == "switchMe") + connect(o, SIGNAL(switchMe()), switcher, SLOT(switchIt())); + } + + QObject *switcher; + }; + + void start() + { + incubator = new MyIncubator(QDeclarativeIncubator::Synchronous, this); + component = new QDeclarativeComponent(engine, TEST_FILE("recursiveClear.1.qml")); + component->create(*incubator); + } + + QDeclarativeEngine *engine; + MyIncubator *incubator; + QDeclarativeComponent *component; + +public slots: + void switchIt() { + component->deleteLater(); + incubator->clear(); + component = new QDeclarativeComponent(engine, TEST_FILE("recursiveClear.2.qml")); + component->create(*incubator); + } +}; + +void tst_qdeclarativeincubator::recursiveClear() +{ + Switcher switcher(&engine); + switcher.start(); +} + QTEST_MAIN(tst_qdeclarativeincubator) #include "tst_qdeclarativeincubator.moc" -- 1.7.2.5