Continue To Analyze the State Machine Framework
Today is the first day to code our application. We works will, so I have some time to do this analyze. It’s tired for me to work 14 hours, so I can do it for a little time.
Now, let’s begin today’s work. At yesterday’s analyze, we got the addTransition() function.
/*!
Adds a transition associated with the given /a signal of the given /a sender
object, and returns the new QSignalTransition object. The transition has
this state as the source, and the given /a target as the target state.
*/
QSignalTransition *QState::addTransition(QObject *sender, const char *signal,
QAbstractState *target)
{
if (!sender) {
qWarning("QState::addTransition: sender cannot be null");
return 0;
}
if (!signal) {
qWarning("QState::addTransition: signal cannot be null");
return 0;
}
if (!target) {
qWarning("QState::addTransition: cannot add transition to null state");
return 0;
}
int offset = (*signal == '0'+QSIGNAL_CODE) ? 1 : 0;
const QMetaObject *meta = sender->metaObject();
if (meta->indexOfSignal(signal+offset) == -1) {
if (meta->indexOfSignal(QMetaObject::normalizedSignature(signal+offset)) == -1) {
qWarning("QState::addTransition: no such signal %s::%s",
meta->className(), signal+offset);
return 0;
}
}
QSignalTransition *trans = new QSignalTransition(sender, signal);
trans->setTargetState(target);
addTransition(trans);
return trans;
}
Here I make a mistake yesterday. I copy another overload function. I’m sorry.
int offset = (*signal == '0'+QSIGNAL_CODE) ? 1 : 0;
This line is a judge to make sure that it is a signal or not. Then, they get the Meta object to make sure if the signal or slot still exists.
const QMetaObject *meta = sender->metaObject();
if (meta->indexOfSignal(signal+offset) == -1) {
if (meta->indexOfSignal(QMetaObject::normalizedSignature(signal+offset)) == -1) {
qWarning("QState::addTransition: no such signal %s::%s",
meta->className(), signal+offset);
return 0;
}
}
If they pass the test, they will create an object of QSignalTransition. Then call the setTargetState () function.
QSignalTransition *trans = new QSignalTransition(sender, signal);
trans->setTargetState (target);
addTransition (trans);
So, let’s look at the setTargetState() function.
/*!
Sets the /a target state of this transition.
*/
void QAbstractTransition::setTargetState(QAbstractState* target)
{
Q_D(QAbstractTransition);
if (!target)
d->targetStates.clear();
else
setTargetStates(QList<QAbstractState*>() << target);
}
Here, they call another overload function, continue.
/*!
Sets the target states of this transition to be the given /a targets.
*/
void QAbstractTransition::setTargetStates(const QList<QAbstractState*> &targets)
{
Q_D(QAbstractTransition);
for (int i = 0; i < targets.size(); ++i) {
QAbstractState *target = targets.at(i);
if (!target) {
qWarning("QAbstractTransition::setTargetStates: target state(s) cannot be null");
return;
}
}
d->targetStates.clear();
for (int i = 0; i < targets.size(); ++i)
d->targetStates.append(targets.at(i));
}
Here, they save the state point.
Now, let’s come to the last function, start () function.
void QStateMachine::start()
{
Q_D(QStateMachine);
if (initialState() == 0) {
qWarning("QStateMachine::start: No initial state set for machine. Refusing to start.");
return;
}
switch (d->state) {
case QStateMachinePrivate::NotRunning:
d->state = QStateMachinePrivate::Starting;
QMetaObject::invokeMethod(this, "_q_start", Qt::QueuedConnection);
break;
case QStateMachinePrivate::Starting:
break;
case QStateMachinePrivate::Running:
qWarning("QStateMachine::start(): already running");
break;
}
}
At the begin to start the state machine framework, they call the initialState ().
/*!
Returns this state's initial state, or 0 if the state has no initial state.
*/
QAbstractState *QState::initialState() const
{
Q_D(const QState);
return d->initialState;
}
It’s easy to understand.
Before this, this sentence calls the QStateMachinePrivate object.
Q_D(QStateMachine);
After this function, they called the invokeMethod () function.
That’s all for this code. I must work for our application. Tomorrow, I will to know how to be trigger this state machine and change the state.
November 7, 2009 23:19