Make the FSM_STATE work better with optional loggins set by a define and standardize on using enum class by default.

master
Zed A. Shaw 2 months ago
parent 8df6898d0b
commit 0aeb5b4ceb
  1. 22
      builder.cpp
  2. 19
      builder.hpp
  3. 7
      fsm.hpp
  4. 2
      game_engine.cpp
  5. 10
      game_engine.hpp
  6. 4
      gui.cpp
  7. 26
      scratchpad/fsmtest.cpp
  8. 1
      status.txt

@ -88,7 +88,7 @@ void Builder::building(BuildEvent ev) {
build_out = NULL; build_out = NULL;
game.event(GameEvent::BUILD_DONE); game.event(GameEvent::BUILD_DONE);
state(DONE); state(BuildState::DONE);
} else { } else {
auto m = parse_line(line); auto m = parse_line(line);
@ -96,7 +96,7 @@ void Builder::building(BuildEvent ev) {
gui.output(format("HIT WITH {} @ {}:{}:{} {}", m.type, m.file_name, m.lnumber, m.col, m.message)); gui.output(format("HIT WITH {} @ {}:{}:{} {}", m.type, m.file_name, m.lnumber, m.col, m.message));
game.event(GameEvent::HIT, m.type); game.event(GameEvent::HIT, m.type);
} }
state(READING); state(BuildState::READING);
} }
} }
@ -117,7 +117,7 @@ void Builder::start(BuildEvent ev) {
wid = fileWatcher->addWatch(git_path, listener, true); wid = fileWatcher->addWatch(git_path, listener, true);
fileWatcher->watch(); fileWatcher->watch();
state(WAITING); state(BuildState::WAITING);
} }
void Builder::waiting(BuildEvent ev) { void Builder::waiting(BuildEvent ev) {
@ -125,7 +125,7 @@ void Builder::waiting(BuildEvent ev) {
game.event(GameEvent::BUILD_START); game.event(GameEvent::BUILD_START);
gui.building(); gui.building();
gui.output(format("CHANGES! Running build {}", build_cmd)); gui.output(format("CHANGES! Running build {}", build_cmd));
state(FORKING); state(BuildState::FORKING);
} }
} }
@ -135,16 +135,16 @@ void Builder::forking(BuildEvent ev) {
if(status == std::future_status::ready) { if(status == std::future_status::ready) {
build_out = build_fut.get(); build_out = build_fut.get();
state(READING); state(BuildState::READING);
} else { } else {
state(FORKING); state(BuildState::FORKING);
} }
} else { } else {
build_fut = std::async([&]() { build_fut = std::async([&]() {
return start_command(build_cmd); return start_command(build_cmd);
}); });
state(FORKING); state(BuildState::FORKING);
} }
} }
@ -155,9 +155,9 @@ void Builder::reading(BuildEvent ev) {
if(status == std::future_status::ready) { if(status == std::future_status::ready) {
line = read_fut.get(); line = read_fut.get();
state(BUILDING); state(BuildState::BUILDING);
} else { } else {
state(READING); state(BuildState::READING);
} }
} else { } else {
read_fut = std::async([&]() { read_fut = std::async([&]() {
@ -173,14 +173,14 @@ void Builder::done(BuildEvent ev) {
listener->reset_state(); listener->reset_state();
gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^"); gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^");
state(WAITING); state(BuildState::WAITING);
} }
void Builder::exit(BuildEvent ev) { void Builder::exit(BuildEvent ev) {
if(ev == QUIT) { if(ev == QUIT) {
fileWatcher->removeWatch(wid); fileWatcher->removeWatch(wid);
git_libgit2_shutdown(); git_libgit2_shutdown();
state(EXIT); state(BuildState::EXIT);
} }
} }

@ -19,7 +19,7 @@ struct MatchResult {
string message = ""; string message = "";
}; };
enum BuildState { enum class BuildState {
START, WAITING, BUILDING, DONE, FORKING, READING, START, WAITING, BUILDING, DONE, FORKING, READING,
EXIT, ERROR EXIT, ERROR
}; };
@ -60,16 +60,17 @@ class Builder : DeadSimpleFSM<BuildState, BuildEvent> {
} }
switch(_state) { switch(_state) {
FSM_STATE(BUILDING, building, ev); FSM_STATE(BuildState, BUILDING, building, ev);
FSM_STATE(START, start, ev); FSM_STATE(BuildState, START, start, ev);
FSM_STATE(WAITING, waiting, ev); FSM_STATE(BuildState, WAITING, waiting, ev);
FSM_STATE(DONE, done, ev); FSM_STATE(BuildState, DONE, done, ev);
FSM_STATE(FORKING, forking, ev); FSM_STATE(BuildState, FORKING, forking, ev);
FSM_STATE(READING, reading, ev); FSM_STATE(BuildState, READING, reading, ev);
FSM_STATE(EXIT, exit, ev); FSM_STATE(BuildState, EXIT, exit, ev);
FSM_STATE(ERROR, exit, ev); FSM_STATE(BuildState, ERROR, exit, ev);
} }
} catch(...) { } catch(...) {
fmt::println("ERROR: unhandled state: {}", int(_state));
error(ev); error(ev);
} }
} }

@ -2,10 +2,11 @@
#include <fmt/core.h> #include <fmt/core.h>
#define FSM_EV(S, F) case S: F(); break #ifndef FSM_DEBUG
#define FSM_STATE(S, F, E) case S: F(E); break #define FSM_STATE(C, S, F, E) case C::S: F(E); break
#else
#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 #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
#endif
template<typename S, typename E> template<typename S, typename E>
class DeadSimpleFSM { class DeadSimpleFSM {

@ -6,6 +6,8 @@
#include "game_engine.hpp" #include "game_engine.hpp"
#include <cassert> #include <cassert>
#define FSM_DEBUG 1
const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red); const auto ERROR = fmt::emphasis::bold | fg(fmt::color::red);
using namespace fmt; using namespace fmt;

@ -65,11 +65,11 @@ class GameEngine : DeadSimpleFSM<GameState, GameEvent> {
void event(GameEvent ev) { void event(GameEvent ev) {
switch(_state) { switch(_state) {
FSM_STATE_LOG(GameState, START, start, ev); FSM_STATE(GameState, START, start, ev);
FSM_STATE_LOG(GameState, IDLE, idle, ev); FSM_STATE(GameState, IDLE, idle, ev);
FSM_STATE_LOG(GameState, DEAD, dead, ev); FSM_STATE(GameState, DEAD, dead, ev);
FSM_STATE_LOG(GameState, SUCCESS, success, ev); FSM_STATE(GameState, SUCCESS, success, ev);
FSM_STATE_LOG(GameState, FAILURE, failure, ev); FSM_STATE(GameState, FAILURE, failure, ev);
case GameState::IN_ROUND: { case GameState::IN_ROUND: {
string hit_type = ""; string hit_type = "";
in_round(ev, hit_type); in_round(ev, hit_type);

@ -9,6 +9,7 @@
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
#include <fstream> #include <fstream>
#include "builder.hpp" #include "builder.hpp"
#include <iostream>
using std::string, std::vector; using std::string, std::vector;
@ -28,7 +29,8 @@ GUI::GUI() {
} }
void GUI::output(const string msg) { void GUI::output(const string msg) {
_lines.push_back(msg); // _lines.push_back(msg);
std::cout << msg << std::endl;
} }
void GUI::main_loop(SFMLBackend &gui) { void GUI::main_loop(SFMLBackend &gui) {

@ -3,11 +3,11 @@
using namespace fmt; using namespace fmt;
enum MyState { enum class MyState {
START, RUNNING, END START, RUNNING, END
}; };
enum MyEvent { enum class MyEvent {
STARTED, PUSH, QUIT STARTED, PUSH, QUIT
}; };
@ -15,34 +15,34 @@ class MyFSM : DeadSimpleFSM<MyState, MyEvent> {
public: public:
void event(MyEvent ev) override { void event(MyEvent ev) override {
switch(_state) { switch(_state) {
FSM_STATE(START, start, ev); FSM_STATE(MyState, START, start, ev);
FSM_STATE(RUNNING, push, ev); FSM_STATE(MyState, RUNNING, push, ev);
FSM_STATE(END, quit, ev); FSM_STATE(MyState, END, quit, ev);
} }
} }
void start(MyEvent ev) { void start(MyEvent ev) {
println("<<< START"); println("<<< START");
state(RUNNING); state(MyState::RUNNING);
} }
void push(MyEvent ev) { void push(MyEvent ev) {
println("<<< RUN"); println("<<< RUN");
state(RUNNING); state(MyState::RUNNING);
} }
void quit(MyEvent ev) { void quit(MyEvent ev) {
println("<<< STOP"); println("<<< STOP");
state(END); state(MyState::END);
} }
}; };
int main() { int main() {
MyFSM fsm; MyFSM fsm;
fsm.event(STARTED); fsm.event(MyEvent::STARTED);
fsm.event(PUSH); fsm.event(MyEvent::PUSH);
fsm.event(PUSH); fsm.event(MyEvent::PUSH);
fsm.event(PUSH); fsm.event(MyEvent::PUSH);
fsm.event(QUIT); fsm.event(MyEvent::QUIT);
} }

@ -2,6 +2,7 @@
BUGS: BUGS:
* BUG: Log doesn't scroll. * BUG: Log doesn't scroll.
* BUG: lots of errors crash it
TODO: TODO:
* Rewrite dbc.hpp to actually work. * Rewrite dbc.hpp to actually work.

Loading…
Cancel
Save