Initial commit that has most of what I need.

This commit is contained in:
Zed A. Shaw 2024-09-24 18:28:01 -04:00
parent 933f47a440
commit ad143dca05
11 changed files with 317 additions and 0 deletions

28
fsm.hpp Normal file
View file

@ -0,0 +1,28 @@
#pragma once
#include <fmt/core.h>
#ifndef FSM_DEBUG
#define FSM_STATE(C, S, E, ...) case C::S: S(E, ##__VA_ARGS__); break
#else
#define FSM_STATE(C, S, E, ...) case C::S: fmt::println(">> " #C " " #S " event={}, state={}", int(E), int(_state)); S(E, ##__VA_ARGS__); fmt::println("<< " #C " state={}", int(_state)); break
#endif
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:
template<typename... Types>
void event(E event, Types... args);
void state(S next_state) {
_state = next_state;
}
bool in_state(S state) {
return _state == state;
}
};