C++简单的状态机实现

这个状态机是利用c++的模板类的特性来实现的。

下面直接给出源码

/*  statemachine.h*/

#ifndef STM
#define STM

#include <iostream>

typedef enum EventActionResult { EventFailed, EventProcessedOK };

template<class T>
class State
{
    public:
        char InputEvent;
        State<T> * NextState;
        EventActionResult (T::*Action)(char event );
        State<T> * ErrorState;
};


#define ANY        '*'
    
template<class T>
class StateMachine
{
    private:
        State<T> * init;
        State<T> * current;
        T * target;

    public:
    
        StateMachine()
        {}

        void Init( T * _target, State<T> * initialState )
        {
            init = current = initialState;
            target = _target;
        }

        void Reset()
        {
            current = init;
        }
    
        void ProcessEvent( char event )
        {
            for( State<T> * p = this->current; p->NextState != NULL; p++ )
            {
                if( p->InputEvent == event || p->InputEvent == ANY )
                {
                    if( p->Action != NULL )
                    {
                        if( EventFailed == (this->target->*(p->Action))( event ) )
                        {
                            if( p->ErrorState != NULL )
                            {
                                //Only if there's an errorstate defined. Otherwise, just do nothing
                                this->current = p->ErrorState;
                            }
                            return;
                        }
                    }
                    this->current = p->NextState;
                    return;
                }
            }

            //Event not found. Do nothing
            return;
        }        
};


class App{
public:
//private:
  StateMachine<App> appStateMachine;

  EventActionResult HandleOne(char e);
  
  EventActionResult HandleTwo(char e);

  EventActionResult HandleThree(char e);


  void HandleEvent(char e);

  void Init();
  
  void Start();
};


#endif

 

/* statemachine.cpp*/

 

#include "statemachine.h"
#include <cstdlib>
#include <iostream>
#include <stdio.h>

using namespace std;

typedef State<App>  STATE;

extern STATE Main[];
extern STATE One[];
extern STATE Two[];

STATE Main[] =
{
    //EVENT,    NEXT,     ACTION,                     ERRORSTATE (where to land if there's an error)
    { '1',      One,      &App::HandleOne,            Two }, 
    { '2',      Two,      &App::HandleTwo,            NULL },
    { 0, NULL, NULL, NULL}, //End of table
};

STATE One[] = 
{
    { 'B',      Main,     &App::HandleThree,      NULL },
    { 0, NULL, NULL, NULL },
};

STATE Two[] = 
{
    { 'B',      Main,     NULL,      NULL },
    { 0, NULL, NULL, NULL },
};

  EventActionResult App::HandleOne(char e)
  {
    std::cout<<"handleOne"<<std::endl;
    return EventProcessedOK;
  }
  
  EventActionResult App::HandleTwo(char e)
  {
    std::cout<<"HandleTwo"<<std::endl;
    return EventProcessedOK;
  }

  EventActionResult App::HandleThree(char e){
    std::cout<<"HandleThree"<<std::endl;
    return EventProcessedOK;
  }


  void App::HandleEvent(char e)
  {
    appStateMachine.ProcessEvent(e);
  }

  void App::Init()
  {
    appStateMachine.Init(this, Main);
  }
  
  void App::Start()
  {
    while(1)
    {
      char event = getchar();
      this->HandleEvent(event);
    }
  }

int main(void)
{
    App app;
    
    app.Init();
    
    app.Start();
}

 

然后g++ statemachine.cpp -o test 就可以编过了

这个状态机框架比较简单,可以按自己需求添加状态和处理函数。

个人水平有限,请各位大佬给出宝贵的意见,初学者看不懂的可以在下面留言我看到了会及时回复的。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值