From 98f3ebcf1f6751cb76f9268d33faf0bc5ac70f6e Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Fri, 8 May 2009 16:37:21 +0200 Subject: gracefully handle deletion of transition's target state --- src/corelib/statemachine/qabstracttransition.cpp | 16 ++++++++++++++-- src/corelib/statemachine/qabstracttransition_p.h | 3 ++- src/corelib/statemachine/qstate.cpp | 2 +- tests/auto/qstatemachine/tst_qstatemachine.cpp | 13 +++++++++++++ 4 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/corelib/statemachine/qabstracttransition.cpp b/src/corelib/statemachine/qabstracttransition.cpp index 696da9f..6ddb416 100644 --- a/src/corelib/statemachine/qabstracttransition.cpp +++ b/src/corelib/statemachine/qabstracttransition.cpp @@ -273,7 +273,13 @@ void QAbstractTransition::setTargetState(QAbstractState* target) QList QAbstractTransition::targetStates() const { Q_D(const QAbstractTransition); - return d->targetStates; + QList result; + for (int i = 0; i < d->targetStates.size(); ++i) { + QAbstractState *target = d->targetStates.at(i); + if (target) + result.append(target); + } + return result; } /*! @@ -285,13 +291,19 @@ void QAbstractTransition::setTargetStates(const QList &targets) for (int i=0; imachine() != 0 && target->machine()->rootState() == target) { qWarning("QAbstractTransition::setTargetStates: root state cannot be target of transition"); return; } } - d->targetStates = targets; + d->targetStates.clear(); + for (int i = 0; i < targets.size(); ++i) + d->targetStates.append(targets.at(i)); } /*! diff --git a/src/corelib/statemachine/qabstracttransition_p.h b/src/corelib/statemachine/qabstracttransition_p.h index b4e1c88..eb0ec21 100644 --- a/src/corelib/statemachine/qabstracttransition_p.h +++ b/src/corelib/statemachine/qabstracttransition_p.h @@ -58,6 +58,7 @@ #endif #include +#include QT_BEGIN_NAMESPACE @@ -83,7 +84,7 @@ public: QState *sourceState() const; QStateMachine *machine() const; - QList targetStates; + QList > targetStates; #ifndef QT_NO_ANIMATION QList animations; diff --git a/src/corelib/statemachine/qstate.cpp b/src/corelib/statemachine/qstate.cpp index 63a0a69..5f61865 100644 --- a/src/corelib/statemachine/qstate.cpp +++ b/src/corelib/statemachine/qstate.cpp @@ -312,7 +312,7 @@ QAbstractTransition *QState::addTransition(QAbstractTransition *transition) return 0; } - const QList &targets = QAbstractTransitionPrivate::get(transition)->targetStates; + const QList > &targets = QAbstractTransitionPrivate::get(transition)->targetStates; for (int i = 0; i < targets.size(); ++i) { QAbstractState *t = targets.at(i); if (!t) { diff --git a/tests/auto/qstatemachine/tst_qstatemachine.cpp b/tests/auto/qstatemachine/tst_qstatemachine.cpp index fa49ecb..dbc67d1 100644 --- a/tests/auto/qstatemachine/tst_qstatemachine.cpp +++ b/tests/auto/qstatemachine/tst_qstatemachine.cpp @@ -106,6 +106,7 @@ private slots: void historyStates(); void startAndStop(); void targetStateWithNoParent(); + void targetStateDeleted(); void transitionToRootState(); void transitionEntersParent(); @@ -1940,6 +1941,18 @@ void tst_QStateMachine::targetStateWithNoParent() QCOMPARE(machine.error(), QStateMachine::NoCommonAncestorForTransitionError); } +void tst_QStateMachine::targetStateDeleted() +{ + QStateMachine machine; + QState *s1 = new QState(machine.rootState()); + s1->setObjectName("s1"); + QState *s2 = new QState(machine.rootState()); + QAbstractTransition *trans = s1->addTransition(s2); + delete s2; + QCOMPARE(trans->targetState(), (QAbstractState*)0); + QVERIFY(trans->targetStates().isEmpty()); +} + void tst_QStateMachine::defaultGlobalRestorePolicy() { QStateMachine machine; -- cgit v0.12