Refined the FSM to allow easily passing in data to the even handler and state functions.

This commit is contained in:
Zed A. Shaw 2024-09-16 07:50:04 -04:00
parent 40ba7f0b66
commit c9425aebf9
7 changed files with 75 additions and 74 deletions

View file

@ -3,9 +3,9 @@
#include <fmt/core.h>
#ifndef FSM_DEBUG
#define FSM_STATE(C, S, F, E) case C::S: F(E); break
#define FSM_STATE(C, S, F, E, ...) case C::S: F(E, ##__VA_ARGS__); break
#else
#define FSM_STATE(C, S, F, E) case C::S: fmt::println(">> " #C " " #S ":" #F " event={}, state={}", int(E), int(_state)); F(E); fmt::println("<< " #C " state={}", int(_state)); break
#define FSM_STATE(C, S, F, E, ...) case C::S: fmt::println(">> " #C " " #S ":" #F " event={}, state={}", int(E), int(_state)); F(E, ##__VA_ARGS__); fmt::println("<< " #C " state={}", int(_state)); break
#endif
template<typename S, typename E>
@ -15,7 +15,8 @@ protected:
S _state = S::START;
public:
virtual void event(E event) = 0;
template<typename... Types>
void event(E event, Types... args);
void state(S next_state) {
_state = next_state;