Skip to content

Instantly share code, notes, and snippets.

@ramunas
Last active November 18, 2019 22:56
Show Gist options
  • Save ramunas/2c4474b8ea8f3b8252ac9751838d2b99 to your computer and use it in GitHub Desktop.
Save ramunas/2c4474b8ea8f3b8252ac9751838d2b99 to your computer and use it in GitHub Desktop.
template <typename State, typename ...Trans> struct FSMRunner;
template <typename State, typename T, typename ...Ts> struct FSMRunner<State, T, Ts...> {
inline static void step(State &state) {
if (T::from == state && T::precond()) {
T::action();
state = T::to;
} else {
FSMRunner<State, Ts...>::step(state);
}
}
};
template <typename State> struct FSMRunner<State> {
inline static void step(State &state) {}
};
template <typename State, typename ...Trans> struct FSM {
State state; // initial sstate
inline void step() {
FSMRunner<State, Trans...>::step(state);
}
};
enum class State {
A,
B,
C
};
struct TransitionX {
static const State from = State::A;
static const State to = State::B;
static bool precond() {
return true;
}
static void action() {
}
};
int main()
{
FSM<State, TransitionX, TransitionX, TransitionX> fsm;
fsm.state = State::A;
fsm.step();
cout << (int)fsm.state << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment