Extract the FSM before beginning to use it.

master
Zed A. Shaw 3 months ago
parent b9025be45b
commit dcf1a4020d
  1. 16
      fsm.hpp
  2. 22
      fsmtest.cpp

@ -0,0 +1,16 @@
#pragma once
template<typename S, typename E>
class DeadSimpleFSM {
protected:
S _state = S::START;
public:
virtual void event(E event) = 0;
void state(S next_state) {
_state = next_state;
}
};
#define FSM_T(S, F) case S: F(); break

@ -1,22 +1,8 @@
#include <fmt/core.h> #include <fmt/core.h>
#include "fsm.hpp"
using namespace fmt; using namespace fmt;
template<typename S, typename E>
class DeadSimpleFSM {
protected:
S _state = S::START;
public:
virtual void event(E event) = 0;
void state(S next_state) {
_state = next_state;
}
};
#define T(S, F) case S: F(); break
enum MyState { enum MyState {
START, RUNNING, END START, RUNNING, END
}; };
@ -29,9 +15,9 @@ class MyFSM : DeadSimpleFSM<MyState, MyEvent> {
public: public:
void event(MyEvent ev) override { void event(MyEvent ev) override {
switch(ev) { switch(ev) {
T(STARTED, start); FSM_T(STARTED, start);
T(PUSH, push); FSM_T(PUSH, push);
T(QUIT, stop); FSM_T(QUIT, quit);
} }
} }

Loading…
Cancel
Save