#pragma once #include "receiver.hpp" #include #include #include #include using namespace ns_mess; #define CLASS_INFO \ virtual std::string __className() { \ std::string nameBuffer( __FUNCTION__ ); \ std::smatch result; \ std::regex pattern( "(\\w+)::" ); \ std::regex_search( nameBuffer, result, pattern ); \ return result[1]; \ } //typedef std::function StateFunc; #define CLASSNAME __className() #define FUNCNAME __func__ #define CLASSFUNCNAME __className() + "_" + __func__ #define DESCRIBE(str) str,CLASSNAME,FUNCNAME struct RUN_FLAG { string func; string step; bool flag; }; class __declspec(dllexport) CFsmBase { //没有下个Step动作 #define STEP_NULL [&](CEventBase const& msg) { return true; } //Step执行完后执行的函数 #define STEP_FUNC(func) [&](CEventBase const& msg) {func; return true; } //本状态机的下个Step动作 #define NEXT_STEP(step) [&](CEventBase const& msg) {m_ThisSender().Send(step); return true; } //调度状态机的Step动作 #define NEXT_STEP_EXTERNAL(step) [&](CEventBase const& msg) {m_ExternalSender().Send(step); return true; } private: CLASS_INFO; static std::mutex g_FlagMutex; static std::vector m_vecRunFlag; CSender m_MsgSender; void SetSender(CSender _sender); public: static void SetFlag(string func, string step); static bool Getflag(string func, string step); static void ClearFlag(string func); protected: StateFunc m_StateCurrent; CReceiver m_MsgReceiver; CFsmBase(CFsmBase const&) = delete; CFsmBase& operator=(CFsmBase const&) = delete; CFsmBase(); //空闲状态 virtual string Idle() = 0; //校准状态 virtual string Calib() = 0; //编程状态 virtual string Programming() = 0; //诊断状态 virtual string Diagnosis() = 0; //自动固晶状态 virtual string AutoBond() = 0; //手动操作状态 virtual string ManualOperation() = 0; public: bool Init(CSender _sender); void StopFsm(); //停止运行 void ExitFsm(); //退出 void RunFsm(); //开始运行 CSender m_ThisSender();//向本状态机发送消息 CSender m_ExternalSender(); //向外部状态机发消息 template void SetState(_Fx&& _Func, _Types&&... _Args) { StateFunc m_chState = _Binder<_Unforced, _Fx, _Types...>(_STD forward<_Fx>(_Func), _STD forward<_Types>(_Args)...); m_ThisSender().Send(ChangeState(m_chState)); } };