You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
27 lines
701 B
27 lines
701 B
#pragma once
|
|
|
|
#include <fmt/core.h>
|
|
|
|
#define FSM_EV(S, F) case S: F(); break
|
|
#define FSM_STATE(S, F, E) case S: F(E); break
|
|
|
|
#define FSM_STATE_LOG(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
|
|
|
|
template<typename S, typename E>
|
|
class DeadSimpleFSM {
|
|
protected:
|
|
// BUG: don't put this in your class because state() won't work
|
|
S _state = S::START;
|
|
|
|
public:
|
|
virtual void event(E event) = 0;
|
|
|
|
void state(S next_state) {
|
|
// fmt::println("STATE {}->{}", int(_state), int(next_state));
|
|
_state = next_state;
|
|
}
|
|
|
|
bool in_state(S state) {
|
|
return _state == state;
|
|
}
|
|
};
|
|
|