summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKent Hansen <khansen@trolltech.com>2009-09-29 13:04:09 (GMT)
committerKent Hansen <khansen@trolltech.com>2009-09-29 13:09:36 (GMT)
commitf6a6e30eb16616b90d90fd6e20f9d840da41b9d1 (patch)
tree5e3a5ca259a06d78cb7cc2777084bc4118724322
parent1fd189619149c6d3489a35e42c4034d4145237dc (diff)
downloadQt-f6a6e30eb16616b90d90fd6e20f9d840da41b9d1.zip
Qt-f6a6e30eb16616b90d90fd6e20f9d840da41b9d1.tar.gz
Qt-f6a6e30eb16616b90d90fd6e20f9d840da41b9d1.tar.bz2
Introduce state machine event priority, make it possible to cancel events
The priority specifies whether the event should be posted to what the SCXML spec refers to as the "external" (NormalPriority) queue, or the "internal" (HighPriority) queue. Delayed events are now posted through a separate function, postDelayedEvent(). That function returns an id that can be passed to cancelDelayedEvent() to cancel it. Reviewed-by: Eskil Abrahamsen Blomfeldt
-rw-r--r--examples/statemachine/pingpong/main.cpp4
-rw-r--r--src/corelib/statemachine/qstatemachine.cpp98
-rw-r--r--src/corelib/statemachine/qstatemachine.h11
-rw-r--r--tests/auto/qstatemachine/tst_qstatemachine.cpp49
4 files changed, 135 insertions, 27 deletions
diff --git a/examples/statemachine/pingpong/main.cpp b/examples/statemachine/pingpong/main.cpp
index 7e3d8b1..358c499 100644
--- a/examples/statemachine/pingpong/main.cpp
+++ b/examples/statemachine/pingpong/main.cpp
@@ -86,7 +86,7 @@ protected:
}
virtual void onTransition(QEvent *)
{
- machine()->postEvent(new PingEvent(), 500);
+ machine()->postDelayedEvent(new PingEvent(), 500);
fprintf(stdout, "ping?\n");
}
};
@@ -104,7 +104,7 @@ protected:
}
virtual void onTransition(QEvent *)
{
- machine()->postEvent(new PongEvent(), 500);
+ machine()->postDelayedEvent(new PongEvent(), 500);
fprintf(stdout, "pong!\n");
}
};
diff --git a/src/corelib/statemachine/qstatemachine.cpp b/src/corelib/statemachine/qstatemachine.cpp
index 8d50870c..256763b 100644
--- a/src/corelib/statemachine/qstatemachine.cpp
+++ b/src/corelib/statemachine/qstatemachine.cpp
@@ -1587,6 +1587,18 @@ QStateMachine::~QStateMachine()
{
}
+/*!
+ \enum QStateMachine::EventPriority
+
+ This enum type specifies the priority of an event posted to the state
+ machine using postEvent().
+
+ Events of high priority are processed before events of normal priority.
+
+ \value NormalPriority The event has normal priority.
+ \value HighPriority The event has high priority.
+*/
+
/*! \enum QStateMachine::Error
This enum type defines errors that can occur in the state machine at run time. When the state
@@ -1798,47 +1810,99 @@ void QStateMachine::stop()
}
/*!
- Posts the given \a event for processing by this state machine, with a delay
- of \a delay milliseconds.
+ Posts the given \a event of the given \a priority for processing by this
+ state machine.
This function returns immediately. The event is added to the state machine's
event queue. Events are processed in the order posted. The state machine
takes ownership of the event and deletes it once it has been processed.
You can only post events when the state machine is running.
+
+ \sa postDelayedEvent()
*/
-void QStateMachine::postEvent(QEvent *event, int delay)
+void QStateMachine::postEvent(QEvent *event, EventPriority priority)
{
Q_D(QStateMachine);
if (d->state != QStateMachinePrivate::Running) {
qWarning("QStateMachine::postEvent: cannot post event when the state machine is not running");
return;
}
+ if (!event) {
+ qWarning("QStateMachine::postEvent: cannot post null event");
+ return;
+ }
#ifdef QSTATEMACHINE_DEBUG
- qDebug() << this << ": posting external event" << event << "with delay" << delay;
+ qDebug() << this << ": posting event" << event;
#endif
- if (delay) {
- int tid = startTimer(delay);
- d->delayedEvents[tid] = event;
- } else {
+ switch (priority) {
+ case NormalPriority:
d->externalEventQueue.append(event);
- d->processEvents(QStateMachinePrivate::QueuedProcessing);
+ break;
+ case HighPriority:
+ d->internalEventQueue.append(event);
+ break;
}
+ d->processEvents(QStateMachinePrivate::QueuedProcessing);
}
/*!
- \internal
+ Posts the given \a event for processing by this state machine, with the
+ given \a delay in milliseconds. Returns an identifier associated with the
+ delayed event, or -1 if the event could not be posted.
+
+ This function returns immediately. When the delay has expired, the event
+ will be added to the state machine's event queue for processing. The state
+ machine takes ownership of the event and deletes it once it has been
+ processed.
+
+ You can only post events when the state machine is running.
- Posts the given internal \a event for processing by this state machine.
+ \sa cancelDelayedEvent(), postEvent()
*/
-void QStateMachine::postInternalEvent(QEvent *event)
+int QStateMachine::postDelayedEvent(QEvent *event, int delay)
{
Q_D(QStateMachine);
+ if (d->state != QStateMachinePrivate::Running) {
+ qWarning("QStateMachine::postDelayedEvent: cannot post event when the state machine is not running");
+ return -1;
+ }
+ if (!event) {
+ qWarning("QStateMachine::postDelayedEvent: cannot post null event");
+ return -1;
+ }
+ if (delay < 0) {
+ qWarning("QStateMachine::postDelayedEvent: delay cannot be negative");
+ return -1;
+ }
#ifdef QSTATEMACHINE_DEBUG
- qDebug() << this << ": posting internal event" << event;
+ qDebug() << this << ": posting event" << event << "with delay" << delay;
#endif
- d->internalEventQueue.append(event);
- d->processEvents(QStateMachinePrivate::QueuedProcessing);
+ int tid = startTimer(delay);
+ d->delayedEvents[tid] = event;
+ return tid;
+}
+
+/*!
+ Cancels the delayed event identified by the given \a id. The id should be a
+ value returned by a call to postDelayedEvent(). Returns true if the event
+ was successfully cancelled, otherwise returns false.
+
+ \sa postDelayedEvent()
+*/
+bool QStateMachine::cancelDelayedEvent(int id)
+{
+ Q_D(QStateMachine);
+ if (d->state != QStateMachinePrivate::Running) {
+ qWarning("QStateMachine::cancelDelayedEvent: the machine is not running");
+ return false;
+ }
+ QEvent *e = d->delayedEvents.take(id);
+ if (!e)
+ return false;
+ killTimer(id);
+ delete e;
+ return true;
}
/*!
@@ -1882,9 +1946,9 @@ bool QStateMachine::event(QEvent *e)
if (e->type() == QEvent::Timer) {
QTimerEvent *te = static_cast<QTimerEvent*>(e);
int tid = te->timerId();
- if (d->delayedEvents.contains(tid)) {
+ QEvent *ee = d->delayedEvents.take(tid);
+ if (ee != 0) {
killTimer(tid);
- QEvent *ee = d->delayedEvents.take(tid);
d->externalEventQueue.append(ee);
d->processEvents(QStateMachinePrivate::DirectProcessing);
return true;
diff --git a/src/corelib/statemachine/qstatemachine.h b/src/corelib/statemachine/qstatemachine.h
index a0b2b14..321a05c 100644
--- a/src/corelib/statemachine/qstatemachine.h
+++ b/src/corelib/statemachine/qstatemachine.h
@@ -102,6 +102,11 @@ public:
QEvent *m_event;
};
+ enum EventPriority {
+ NormalPriority,
+ HighPriority
+ };
+
enum RestorePolicy {
DoNotRestoreProperties,
RestoreProperties
@@ -138,7 +143,9 @@ public:
QStateMachine::RestorePolicy globalRestorePolicy() const;
void setGlobalRestorePolicy(QStateMachine::RestorePolicy restorePolicy);
- void postEvent(QEvent *event, int delay = 0);
+ void postEvent(QEvent *event, EventPriority priority = NormalPriority);
+ int postDelayedEvent(QEvent *event, int delay);
+ bool cancelDelayedEvent(int id);
QSet<QAbstractState*> configuration() const;
@@ -158,8 +165,6 @@ protected:
void onEntry(QEvent *event);
void onExit(QEvent *event);
- void postInternalEvent(QEvent *event);
-
virtual void beginSelectTransitions(QEvent *event);
virtual void endSelectTransitions(QEvent *event);
diff --git a/tests/auto/qstatemachine/tst_qstatemachine.cpp b/tests/auto/qstatemachine/tst_qstatemachine.cpp
index 37b34bf..463dbf6 100644
--- a/tests/auto/qstatemachine/tst_qstatemachine.cpp
+++ b/tests/auto/qstatemachine/tst_qstatemachine.cpp
@@ -119,6 +119,7 @@ private slots:
void assignProperty();
void assignPropertyWithAnimation();
void postEvent();
+ void cancelDelayedEvent();
void stateFinished();
void parallelStates();
void parallelRootState();
@@ -1543,8 +1544,8 @@ private:
class StringEventPoster : public QState
{
public:
- StringEventPoster(QStateMachine *machine, const QString &value, QState *parent = 0)
- : QState(parent), m_machine(machine), m_value(value), m_delay(0) {}
+ StringEventPoster(const QString &value, QState *parent = 0)
+ : QState(parent), m_value(value), m_delay(-1) {}
void setString(const QString &value)
{ m_value = value; }
@@ -1554,12 +1555,14 @@ public:
protected:
virtual void onEntry(QEvent *)
{
- m_machine->postEvent(new StringEvent(m_value), m_delay);
+ if (m_delay == -1)
+ machine()->postEvent(new StringEvent(m_value));
+ else
+ machine()->postDelayedEvent(new StringEvent(m_value), m_delay);
}
virtual void onExit(QEvent *) {}
private:
- QStateMachine *m_machine;
QString m_value;
int m_delay;
};
@@ -1573,7 +1576,7 @@ void tst_QStateMachine::postEvent()
QTest::ignoreMessage(QtWarningMsg, "QStateMachine::postEvent: cannot post event when the state machine is not running");
machine.postEvent(&e);
}
- StringEventPoster *s1 = new StringEventPoster(&machine, "a");
+ StringEventPoster *s1 = new StringEventPoster("a");
if (x == 1)
s1->setDelay(100);
QFinalState *s2 = new QFinalState;
@@ -1599,6 +1602,42 @@ void tst_QStateMachine::postEvent()
}
}
+void tst_QStateMachine::cancelDelayedEvent()
+{
+ QStateMachine machine;
+ QTest::ignoreMessage(QtWarningMsg, "QStateMachine::cancelDelayedEvent: the machine is not running");
+ QVERIFY(!machine.cancelDelayedEvent(-1));
+
+ QState *s1 = new QState(&machine);
+ QFinalState *s2 = new QFinalState(&machine);
+ s1->addTransition(new StringTransition("a", s2));
+ machine.setInitialState(s1);
+
+ QSignalSpy startedSpy(&machine, SIGNAL(started()));
+ machine.start();
+ QTRY_COMPARE(startedSpy.count(), 1);
+ QCOMPARE(machine.configuration().size(), 1);
+ QVERIFY(machine.configuration().contains(s1));
+
+ int id1 = machine.postDelayedEvent(new StringEvent("c"), 50000);
+ QVERIFY(id1 != -1);
+ int id2 = machine.postDelayedEvent(new StringEvent("b"), 25000);
+ QVERIFY(id2 != -1);
+ QVERIFY(id2 != id1);
+ int id3 = machine.postDelayedEvent(new StringEvent("a"), 100);
+ QVERIFY(id3 != -1);
+ QVERIFY(id3 != id2);
+ QVERIFY(machine.cancelDelayedEvent(id1));
+ QVERIFY(!machine.cancelDelayedEvent(id1));
+ QVERIFY(machine.cancelDelayedEvent(id2));
+ QVERIFY(!machine.cancelDelayedEvent(id2));
+
+ QSignalSpy finishedSpy(&machine, SIGNAL(finished()));
+ QTRY_COMPARE(finishedSpy.count(), 1);
+ QCOMPARE(machine.configuration().size(), 1);
+ QVERIFY(machine.configuration().contains(s2));
+}
+
void tst_QStateMachine::stateFinished()
{
QStateMachine machine;