Continue To Analyze the State Machine Framework

 

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值