After trying a few SFM libraries I found they're just too much for what I need, so here's the start of a simple one I can use.
parent
356314406f
commit
b9025be45b
@ -0,0 +1,62 @@ |
||||
#include <fmt/core.h> |
||||
|
||||
using namespace fmt; |
||||
|
||||
template<typename S, typename E> |
||||
class DeadSimpleFSM { |
||||
protected: |
||||
S _state = S::START; |
||||
|
||||
public: |
||||
virtual void event(E event) = 0; |
||||
|
||||
void state(S next_state) { |
||||
_state = next_state; |
||||
} |
||||
}; |
||||
|
||||
#define T(S, F) case S: F(); break |
||||
|
||||
enum MyState { |
||||
START, RUNNING, END |
||||
}; |
||||
|
||||
enum MyEvent { |
||||
STARTED, PUSH, QUIT |
||||
}; |
||||
|
||||
class MyFSM : DeadSimpleFSM<MyState, MyEvent> { |
||||
public: |
||||
void event(MyEvent ev) override { |
||||
switch(ev) { |
||||
T(STARTED, start); |
||||
T(PUSH, push); |
||||
T(QUIT, stop); |
||||
} |
||||
} |
||||
|
||||
void start() { |
||||
println("<<< START"); |
||||
state(RUNNING); |
||||
} |
||||
|
||||
void push() { |
||||
println("<<< RUN"); |
||||
state(RUNNING); |
||||
} |
||||
|
||||
void quit() { |
||||
println("<<< STOP"); |
||||
state(END); |
||||
} |
||||
}; |
||||
|
||||
int main() { |
||||
MyFSM fsm; |
||||
|
||||
fsm.event(STARTED); |
||||
fsm.event(PUSH); |
||||
fsm.event(PUSH); |
||||
fsm.event(PUSH); |
||||
fsm.event(QUIT); |
||||
} |
Loading…
Reference in new issue