CFsmBase.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include "receiver.hpp"
  3. #include <string>
  4. #include <iostream>
  5. #include <regex>
  6. #include <functional>
  7. using namespace ns_mess;
  8. #define CLASS_INFO \
  9. virtual std::string __className() { \
  10. std::string nameBuffer( __FUNCTION__ ); \
  11. std::smatch result; \
  12. std::regex pattern( "(\\w+)::" ); \
  13. std::regex_search( nameBuffer, result, pattern ); \
  14. return result[1]; \
  15. }
  16. //typedef std::function<string ()> StateFunc;
  17. #define CLASSNAME __className()
  18. #define FUNCNAME __func__
  19. #define CLASSFUNCNAME __className() + "_" + __func__
  20. #define DESCRIBE(str) str,CLASSNAME,FUNCNAME
  21. struct RUN_FLAG
  22. {
  23. string func;
  24. string step;
  25. bool flag;
  26. };
  27. class __declspec(dllexport) CFsmBase
  28. {
  29. //没有下个Step动作
  30. #define STEP_NULL [&](CEventBase const& msg) { return true; }
  31. //Step执行完后执行的函数
  32. #define STEP_FUNC(func) [&](CEventBase const& msg) {func; return true; }
  33. //本状态机的下个Step动作
  34. #define NEXT_STEP(step) [&](CEventBase const& msg) {m_ThisSender().Send(step); return true; }
  35. //调度状态机的Step动作
  36. #define NEXT_STEP_EXTERNAL(step) [&](CEventBase const& msg) {m_ExternalSender().Send(step); return true; }
  37. private:
  38. CLASS_INFO;
  39. static std::mutex g_FlagMutex;
  40. static std::vector<RUN_FLAG> m_vecRunFlag;
  41. CSender m_MsgSender;
  42. void SetSender(CSender _sender);
  43. public:
  44. static void SetFlag(string func, string step);
  45. static bool Getflag(string func, string step);
  46. static void ClearFlag(string func);
  47. protected:
  48. StateFunc m_StateCurrent;
  49. CReceiver m_MsgReceiver;
  50. CFsmBase(CFsmBase const&) = delete;
  51. CFsmBase& operator=(CFsmBase const&) = delete;
  52. CFsmBase();
  53. //空闲状态
  54. virtual string Idle() = 0;
  55. //校准状态
  56. virtual string Calib() = 0;
  57. //编程状态
  58. virtual string Programming() = 0;
  59. //诊断状态
  60. virtual string Diagnosis() = 0;
  61. //自动固晶状态
  62. virtual string AutoBond() = 0;
  63. //手动操作状态
  64. virtual string ManualOperation() = 0;
  65. public:
  66. bool Init(CSender _sender);
  67. void StopFsm(); //停止运行
  68. void ExitFsm(); //退出
  69. void RunFsm(); //开始运行
  70. CSender m_ThisSender();//向本状态机发送消息
  71. CSender m_ExternalSender(); //向外部状态机发消息
  72. template <class _Fx, class... _Types>
  73. void SetState(_Fx&& _Func, _Types&&... _Args)
  74. {
  75. StateFunc m_chState = _Binder<_Unforced, _Fx, _Types...>(_STD forward<_Fx>(_Func), _STD forward<_Types>(_Args)...);
  76. m_ThisSender().Send(ChangeState(m_chState));
  77. }
  78. };