C函数指针状态机实现

Posted gavinpan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C函数指针状态机实现相关的知识,希望对你有一定的参考价值。

C函数指针状态机实现

有限状态机(finite state machine)简称FSM,表示有限个状态及在这些状态之间的转移和动作等行为的数学模型,在计算机领域有着广泛的应用。FSM是一种逻辑单元内部的一种高效编程方法,在服务器编程中,服务器可以根据不同状态或者消息类型进行相应的处理逻辑,使得程序逻辑清晰易懂。

#include <stdio.h>

//函数指针实现FSM
enum year_state

    SPRING = 1,
    SUMMER,
    AUTUMN,
    WINTER
;

enum year_event

    EVENT1 = 1,
    EVENT2,
    EVENT3,
    EVENT4
;

typedef struct FsmTable_s

    int event; // 事件
    int CurState; // 当前状态
    void (*eventActFun)(); //函数指针
    int nextState; // 下一个状态
 FsmTable_t;

void spring_thing()

    printf("this is spring\n");

void summer_thing()

    printf("this is summer\n");

void autumn_thing()

    printf("this is autumn\n");

void winter_thing()

    printf("this is winter\n");


FsmTable_t year_table[] = 

    //到来的事件,当前的状态,将要要执行的函数,下一个状态
     EVENT1,  SPRING,    summer_thing,  SUMMER ,
     EVENT2,  SUMMER,    autumn_thing,  AUTUMN ,
     EVENT3,  AUTUMN,    winter_thing,  WINTER ,
     EVENT4,  WINTER,    spring_thing,  SPRING ,
    //add your codes here
;

typedef struct FSM_s

    int curState;//当前状态
    FsmTable_t * pFsmTable;//状态表
    int size;//表的项数
 FSM_t;

/*状态机注册,给它一个状态表*/
void FSM_Regist(FSM_t *pFSM, FsmTable_t *pFsmTable)

    pFSM->pFsmTable = pFsmTable;


/*状态迁移*/
void FSM_StateTransfer(FSM_t* pFsm, int state)

    pFsm->curState = state;


/*事件处理*/
void FSM_EventHandle(FSM_t* pFsm, int event)

    FsmTable_t* pActTable = pFsm->pFsmTable;
    void (*eventActFun)() = NULL;  //函数指针初始化为空
    int NextState;
    int CurState = pFsm->curState;
    int g_max_num = pFsm->size;
    int flag = 0; //标识是否满足条件
    int i;

    for (i = 0; i < g_max_num; i++)
    
        if (event == pActTable[i].event && CurState == pActTable[i].CurState)
        
            flag = 1;
            eventActFun = pActTable[i].eventActFun;
            NextState = pActTable[i].nextState;
            break;
        
    

    if (flag)
    
        if (eventActFun)
        
            eventActFun();
        
        FSM_StateTransfer(pFsm, NextState);
    
    else
    
        printf("There is no match\n");
    


int main()

    FSM_t year_fsm;
    FSM_Regist(&year_fsm, year_table);
    year_fsm.curState = SPRING;
    year_fsm.size = sizeof(year_table)/sizeof(FsmTable_t);

    printf("\n-------1--init spring------\n");
    printf("state:%d\n",year_fsm.curState);

    printf("\n-------2--spring->summer------\n");
    FSM_EventHandle(&year_fsm,EVENT1);
    printf("state:%d\n",year_fsm.curState);

    printf("\n-------3--summer->autumn------\n");
    FSM_EventHandle(&year_fsm,EVENT2);
    printf("state:%d\n",year_fsm.curState);

    printf("\n-------4--autumn->winter------\n");
    FSM_EventHandle(&year_fsm,EVENT3);
    printf("state:%d\n",year_fsm.curState);

    printf("\n-------5--winter->spring------\n");
    FSM_EventHandle(&year_fsm,EVENT4);
    printf("state:%d\n",year_fsm.curState);

    printf("\n-------6--receive EVENT2 not EVENT1------\n");
    FSM_EventHandle(&year_fsm,EVENT2);
    printf("state:%d\n",year_fsm.curState);

 

以上是关于C函数指针状态机实现的主要内容,如果未能解决你的问题,请参考以下文章

状态机编程思维学习笔记(C语言)

状态机编程思维学习笔记(C语言)

状态机编程思维学习笔记(C语言)

状态机编程思维学习笔记(C语言)

C ++ - 在运行时从基类指针识别派生类

状态机、子类和函数指针