Last active
November 18, 2019 22:56
-
-
Save ramunas/2c4474b8ea8f3b8252ac9751838d2b99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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