C++ 有限狀態機器

來源:互聯網
上載者:User
用一個類定義事件和狀態檔案:GlobalAutoDcl.hConst int NbMaxAction =10;Const int NbMaxState =5;Const int NbMaxEvent =6;Class GlobalAutoDcl{public:typedef enum {event0 = 0;event1 =1;.......}typedef enum {state0  = 0;state1   =1;.......}}用一個抽象類別定義一些數組,Action等:檔案: GlobalAuto.hclass GlobalResetAuto{public:protected://定義action的函數指標類型typedef GlobalAutoDcl::StateForGlobalReset_t (GlobalAuto::*fPtr) (Signal *);//定義一個Action的表static fPtr ActionTable[NbMaxAction];// 定義一個state/event的二維數組static unsigned char Automaton[NbMaxState ][NbMaxEvent];//二維數組的值就是Action,對應的state,event// 定義虛函數,也就是Action函數// 注意函數名字定義,由state和event組成,表示這個action是在此state下收到此eventvirtual GlobalAutoDcl::State_t  state1_event2(Signal *msg) =0;virtual GlobalAutoDcl::State_t  state3_event5(Signal *msg) =0;// 此類中通常還包括如下方法定義//  1.  返回當前state//   2. 把當前state和event轉換成字串//   3. ActionTable的初始化調用函數//    4. 初始化Action數組的函數(也可以不放這裡)}在globalauto.h檔案中方法的具體實現檔案: globalauto.cpp// 上面的state/event數組可以放在這裡初始化// Action也可以放在這裡初始化// 通常還會增加debug功能,記錄過去發生的event// 一個接收input signal的函數, 傳進來的event應該包含event, 而state是本地有的// 然後調用action中的函數指標,更改新state
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.