C ++中的状态机设计 [英] State Machine Design in C++

查看:172
本文介绍了C ++中的状态机设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我尝试在C ++中实现状态机.我发现了一个非常好的例子
http://www.drdobbs.com/cpp/state-machine- design-in-c/184401236?pgno = 3 [ ^ ].

在实施过程中,我遇到了一些问题.我将完整的代码复制到一个新的c ++项目中.在发生一些编译器错误后,我在状态映射条目中添加了& Motor ::".

Hey guys,

i try to implement a state machine in c++. I found this very nice example
http://www.drdobbs.com/cpp/state-machine-design-in-c/184401236?pgno=3[^].

During the implementation I had some problems. I copied the full code in a new c++ project. After some compiler errors, I added "&Motor::" to the state map entry.

// the Motor state machine class
class Motor : public StateMachine
{
public:
    Motor() : StateMachine(ST_MAX_STATES) {}
 
    // external events taken by this state machine
    void Halt();
    void SetSpeed(MotorData*);
private:
    // state machine state functions
    void ST_Idle();
    void ST_Stop();
    void ST_Start(MotorData*);
    void ST_ChangeSpeed(MotorData*);
 
    // state map to define state function order
    BEGIN_STATE_MAP
        STATE_MAP_ENTRY(&Motor::ST_Idle)
        STATE_MAP_ENTRY(&Motor::ST_Stop)
        STATE_MAP_ENTRY(&Motor::ST_Start)
        STATE_MAP_ENTRY(&Motor::ST_ChangeSpeed)
    END_STATE_MAP
 
    // state enumeration order must match the order of state
    // method entries in the state map
    enum E_States {
        ST_IDLE = 0,
        ST_STOP,
        ST_START,
        ST_CHANGE_SPEED,
        ST_MAX_STATES
    };
};
#endif //MOTOR_H



我对代码进行了如下测试



I tested the code as follows

Motor motor;
MotorData* pData = new MotorData;
pData->speed = 50;
motor.SetSpeed(pData);

MotorData* pData2 = new MotorData;
pData2->speed = 100;
motor.SetSpeed(pData2);



到这里为止,一切工作正常.当我在第一个测试的末尾插入以下函数时:



Until here everything is working fine. When I insert the following function at the end of the first test:

motor.Halt();



我收到错误消息

运行时检查失败#0-在函数调用中未正确保存ESP的值.这通常是由于用一种调用约定声明的函数与用另一种调用约定声明的函数指针一起调用的结果."

在这个位置



I get the error

"Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention."

at this position

void StateMachine::StateEngine(void){

  ...      

        // execute the state passing in event data, if any
        const StateStruct* pStateMap = GetStateMap();
        (this->*pStateMap[currentState].pStateFunc)(pDataTemp);    <-- EXCEPTION

  ... 

}



我不知道错误在哪里.我看不到任何调用约定错误.

感谢您的帮助
最好的问候



I have no idea where the error is. I can''t see any calling convention errors.

Thanks for your help
Best regards

推荐答案

我看到你自己弄清楚了,仍然是克里斯托夫·亨利(Christophe Henrys)''
I see that you figured it out yourself, still Christophe Henrys'' Meta State Machine[^] provides an alternative that performs well; and is pretty well documented and tested.

Best regards
Espen Harlinn


我知道了! StateFunc ist定义为
I''ve got it! The StateFunc ist defined as
typedef void (StateMachine::*StateFunc)(EventData *);


但是空闲和停止状态未使用参数EventData.
如果我插入缺少的参数,则一切正常.

@pasztorpisti
我认为您是对的,我将尝试修改此状态机

谢谢大家.


but the idle and stop state didn''t use the param EventData.
If I insert the missing parameter, everything works fine.

@pasztorpisti
i think you are right, i will try to modify this state machine

Thank you guys.


这篇关于C ++中的状态机设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆