Allow incubators to be driven recursively
authorAaron Kennedy <aaron.kennedy@nokia.com>
Thu, 6 Oct 2011 04:14:40 +0000 (14:14 +1000)
committerQt by Nokia <qt-info@nokia.com>
Thu, 6 Oct 2011 05:55:59 +0000 (07:55 +0200)
Change-Id: If8ce239372c3cf3166b666329ba812b25ee54669
Reviewed-on: http://codereview.qt-project.org/6102
Reviewed-by: Aaron Kennedy <aaron.kennedy@nokia.com>

src/declarative/qml/ftw/ftw.pri
src/declarative/qml/ftw/qrecursionwatcher_p.h [new file with mode: 0644]
src/declarative/qml/qdeclarativeincubator.cpp
src/declarative/qml/qdeclarativeincubator_p.h
src/declarative/qml/qdeclarativevme.cpp
src/declarative/qml/qdeclarativevme_p.h
tests/auto/declarative/qdeclarativeincubator/data/recursiveClear.1.qml [new file with mode: 0644]
tests/auto/declarative/qdeclarativeincubator/data/recursiveClear.2.qml [new file with mode: 0644]
tests/auto/declarative/qdeclarativeincubator/tst_qdeclarativeincubator.cpp

index 2d6729b..383520a 100644 (file)
@@ -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 (file)
index 0000000..6ad6821
--- /dev/null
@@ -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 <QtCore/qglobal.h>
+
+QT_BEGIN_NAMESPACE
+
+class QRecursionNode;
+class QRecursionNode {
+public:
+    inline QRecursionNode();
+    bool *_r;
+};
+
+template<class T, QRecursionNode T::*Node>
+class QRecursionWatcher {
+public:
+    inline QRecursionWatcher(T *);
+    inline ~QRecursionWatcher();
+    inline bool hasRecursed() const;
+private:
+    T *_t;
+    bool _r;
+};
+
+QRecursionNode::QRecursionNode()
+: _r(0) 
+{
+}
+
+template<class T, QRecursionNode T::*Node>
+QRecursionWatcher<T, Node>::QRecursionWatcher(T *t)
+: _t(t), _r(false)
+{
+    if ((_t->*Node)._r) *(_t->*Node)._r = true;
+    (_t->*Node)._r = &_r;
+}
+
+template<class T, QRecursionNode T::*Node>
+QRecursionWatcher<T, Node>::~QRecursionWatcher()
+{
+    if ((_t->*Node)._r == &_r) (_t->*Node)._r = 0;
+}
+
+template<class T, QRecursionNode T::*Node>
+bool QRecursionWatcher<T, Node>::hasRecursed() const
+{
+    return _r;
+}
+
+QT_END_NAMESPACE
+
+#endif // QRECURSIONWATCHER_P_H
index 4779caf..ae1ae2d 100644 (file)
@@ -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<IP, &IP::recursion> 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<IP, &IP::recursion> 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;
     }
 
index e71602a..d45f6d1 100644 (file)
@@ -44,6 +44,7 @@
 
 #include <private/qintrusivelist_p.h>
 #include <private/qdeclarativevme_p.h>
+#include <private/qrecursionwatcher_p.h>
 #include <private/qdeclarativeengine_p.h>
 
 //
@@ -85,6 +86,8 @@ public:
     typedef QDeclarativeEnginePrivate::Incubator QIPBase;
     QIntrusiveList<QIPBase, &QIPBase::nextWaitingFor> waitingFor;
 
+    QRecursionNode recursion;
+
     void clear();
 
     void incubate(QDeclarativeVME::Interrupt &i);
index 8f34fb5..53bcd8c 100644 (file)
@@ -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<const QDeclarativeInstruction *>(INSTRUCTIONSTREAM); \
     goto *genericInstr->common.code; \
     }
 
 #  define QML_END_INSTR(I) } \
+    if (watcher.hasRecursed()) return 0; \
     genericInstr = reinterpret_cast<const QDeclarativeInstruction *>(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<QDeclarativeError> *errors,
     QDeclarativePropertyPrivate::WriteFlags flags = QDeclarativePropertyPrivate::BypassInterceptor |
                                                     QDeclarativePropertyPrivate::RemoveBindingOnAliasWrite;
 
+    QRecursionWatcher<QDeclarativeVME, &QDeclarativeVME::recursion> 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<QDeclarativeVME, &QDeclarativeVME::recursion> 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<QDeclarativeVME, &QDeclarativeVME::recursion> 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();
 
index 771a2b4..5809ccc 100644 (file)
@@ -56,6 +56,7 @@
 #include "qdeclarativeerror.h"
 #include "private/qbitfield_p.h"
 #include "private/qdeclarativeinstruction_p.h"
+#include "private/qrecursionwatcher_p.h"
 
 #include <QtCore/QStack>
 #include <QtCore/QString>
@@ -141,6 +142,7 @@ private:
 #endif
 
     QDeclarativeEngine *engine;
+    QRecursionNode recursion;
 
     QFiniteStack<QObject *> objects;
     QFiniteStack<QDeclarativeVMETypes::List> 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 (file)
index 0000000..748a3f0
--- /dev/null
@@ -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 (file)
index 0000000..e96ac00
--- /dev/null
@@ -0,0 +1,8 @@
+import QtQuick 2.0
+
+Rectangle {
+    objectName: "blue"
+    width: 100
+    height: 100
+    color: "blue"
+}
index f870ffb..52ddf81 100644 (file)
@@ -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<QObject> 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"