From 058246c53767dc99ff466882cd890db8ebd7bd49 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Thu, 12 Jul 2012 20:00:15 +0200 Subject: [PATCH] statemachine: Small refactoring of transition registration Split the guts of registerTransitions() into a registerTransition() function. This allows a particular transition to be registered, instead of walking the source state's whole list of transitions every time. Move the logic for determining whether a transition should be registered to the state machine, since that's also where the actual registration takes place. Change-Id: I0496dee9454cd77b62cf2768942a82a96b320744 Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/corelib/statemachine/qeventtransition.cpp | 5 +- .../statemachine/qsignaltransition.cpp | 5 +- src/corelib/statemachine/qstate.cpp | 4 +- src/corelib/statemachine/qstatemachine.cpp | 46 +++++++++++++++---- src/corelib/statemachine/qstatemachine_p.h | 4 ++ 5 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/corelib/statemachine/qeventtransition.cpp b/src/corelib/statemachine/qeventtransition.cpp index 3c93281e98..bdd4f41545 100644 --- a/src/corelib/statemachine/qeventtransition.cpp +++ b/src/corelib/statemachine/qeventtransition.cpp @@ -125,9 +125,8 @@ void QEventTransitionPrivate::unregister() void QEventTransitionPrivate::maybeRegister() { Q_Q(QEventTransition); - if (!machine() || !machine()->configuration().contains(sourceState())) - return; - QStateMachinePrivate::get(machine())->registerEventTransition(q); + if (QStateMachine *mach = machine()) + QStateMachinePrivate::get(mach)->maybeRegisterEventTransition(q); } /*! diff --git a/src/corelib/statemachine/qsignaltransition.cpp b/src/corelib/statemachine/qsignaltransition.cpp index 26d2add8cf..f1f0e4ac09 100644 --- a/src/corelib/statemachine/qsignaltransition.cpp +++ b/src/corelib/statemachine/qsignaltransition.cpp @@ -131,9 +131,8 @@ void QSignalTransitionPrivate::unregister() void QSignalTransitionPrivate::maybeRegister() { Q_Q(QSignalTransition); - if (!machine() || !machine()->configuration().contains(sourceState())) - return; - QStateMachinePrivate::get(machine())->registerSignalTransition(q); + if (QStateMachine *mach = machine()) + QStateMachinePrivate::get(mach)->maybeRegisterSignalTransition(q); } /*! diff --git a/src/corelib/statemachine/qstate.cpp b/src/corelib/statemachine/qstate.cpp index 27cec1d87e..481f0e92a2 100644 --- a/src/corelib/statemachine/qstate.cpp +++ b/src/corelib/statemachine/qstate.cpp @@ -313,8 +313,8 @@ void QState::addTransition(QAbstractTransition *transition) return ; } } - if (machine() != 0 && machine()->configuration().contains(this)) - QStateMachinePrivate::get(machine())->registerTransitions(this); + if (QStateMachine *mach = machine()) + QStateMachinePrivate::get(mach)->maybeRegisterTransition(transition); } /*! diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp index 81be186a30..59b6b55494 100644 --- a/src/corelib/statemachine/qstatemachine.cpp +++ b/src/corelib/statemachine/qstatemachine.cpp @@ -1660,17 +1660,34 @@ void QStateMachinePrivate::registerTransitions(QAbstractState *state) QList transitions = QStatePrivate::get(group)->transitions(); for (int i = 0; i < transitions.size(); ++i) { QAbstractTransition *t = transitions.at(i); - if (QSignalTransition *st = qobject_cast(t)) { - registerSignalTransition(st); - } -#ifndef QT_NO_STATEMACHINE_EVENTFILTER - else if (QEventTransition *oet = qobject_cast(t)) { - registerEventTransition(oet); - } -#endif + registerTransition(t); } } +void QStateMachinePrivate::maybeRegisterTransition(QAbstractTransition *transition) +{ + if (QSignalTransition *st = qobject_cast(transition)) { + maybeRegisterSignalTransition(st); + } +#ifndef QT_NO_STATEMACHINE_EVENTFILTER + else if (QEventTransition *et = qobject_cast(transition)) { + maybeRegisterEventTransition(et); + } +#endif +} + +void QStateMachinePrivate::registerTransition(QAbstractTransition *transition) +{ + if (QSignalTransition *st = qobject_cast(transition)) { + registerSignalTransition(st); + } +#ifndef QT_NO_STATEMACHINE_EVENTFILTER + else if (QEventTransition *oet = qobject_cast(transition)) { + registerEventTransition(oet); + } +#endif +} + void QStateMachinePrivate::unregisterTransition(QAbstractTransition *transition) { if (QSignalTransition *st = qobject_cast(transition)) { @@ -1683,6 +1700,13 @@ void QStateMachinePrivate::unregisterTransition(QAbstractTransition *transition) #endif } +void QStateMachinePrivate::maybeRegisterSignalTransition(QSignalTransition *transition) +{ + Q_Q(QStateMachine); + if ((state == Running) && configuration.contains(transition->sourceState())) + registerSignalTransition(transition); +} + void QStateMachinePrivate::registerSignalTransition(QSignalTransition *transition) { Q_Q(QStateMachine); @@ -1784,6 +1808,12 @@ void QStateMachinePrivate::unregisterAllTransitions() } #ifndef QT_NO_STATEMACHINE_EVENTFILTER +void QStateMachinePrivate::maybeRegisterEventTransition(QEventTransition *transition) +{ + if ((state == Running) && configuration.contains(transition->sourceState())) + registerEventTransition(transition); +} + void QStateMachinePrivate::registerEventTransition(QEventTransition *transition) { Q_Q(QStateMachine); diff --git a/src/corelib/statemachine/qstatemachine_p.h b/src/corelib/statemachine/qstatemachine_p.h index 113f17f973..f3291e7d35 100644 --- a/src/corelib/statemachine/qstatemachine_p.h +++ b/src/corelib/statemachine/qstatemachine_p.h @@ -170,9 +170,13 @@ public: void goToState(QAbstractState *targetState); void registerTransitions(QAbstractState *state); + void maybeRegisterTransition(QAbstractTransition *transition); + void registerTransition(QAbstractTransition *transition); + void maybeRegisterSignalTransition(QSignalTransition *transition); void registerSignalTransition(QSignalTransition *transition); void unregisterSignalTransition(QSignalTransition *transition); #ifndef QT_NO_STATEMACHINE_EVENTFILTER + void maybeRegisterEventTransition(QEventTransition *transition); void registerEventTransition(QEventTransition *transition); void unregisterEventTransition(QEventTransition *transition); void handleFilteredEvent(QObject *watched, QEvent *event);