Extract the FSM before beginning to use it.

master
Zed A. Shaw 4 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 "fsm.hpp"
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 {
START, RUNNING, END
};
@ -29,9 +15,9 @@ class MyFSM : DeadSimpleFSM<MyState, MyEvent> {
public:
void event(MyEvent ev) override {
switch(ev) {
T(STARTED, start);
T(PUSH, push);
T(QUIT, stop);
FSM_T(STARTED, start);
FSM_T(PUSH, push);
FSM_T(QUIT, quit);
}
}

Loading…
Cancel
Save