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 3 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;
game.event(GameEvent::BUILD_DONE);
state(DONE);
state(BuildState::DONE);
} else {
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));
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);
fileWatcher->watch();
state(WAITING);
state(BuildState::WAITING);
}
void Builder::waiting(BuildEvent ev) {
@ -125,7 +125,7 @@ void Builder::waiting(BuildEvent ev) {
game.event(GameEvent::BUILD_START);
gui.building();
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) {
build_out = build_fut.get();
state(READING);
state(BuildState::READING);
} else {
state(FORKING);
state(BuildState::FORKING);
}
} else {
build_fut = std::async([&]() {
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) {
line = read_fut.get();
state(BUILDING);
state(BuildState::BUILDING);
} else {
state(READING);
state(BuildState::READING);
}
} else {
read_fut = std::async([&]() {
@ -173,14 +173,14 @@ void Builder::done(BuildEvent ev) {
listener->reset_state();
gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^");
state(WAITING);
state(BuildState::WAITING);
}
void Builder::exit(BuildEvent ev) {
if(ev == QUIT) {
fileWatcher->removeWatch(wid);
git_libgit2_shutdown();
state(EXIT);
state(BuildState::EXIT);
}
}

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

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

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

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

@ -9,6 +9,7 @@
#include <nlohmann/json.hpp>
#include <fstream>
#include "builder.hpp"
#include <iostream>
using std::string, std::vector;
@ -28,7 +29,8 @@ GUI::GUI() {
}
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) {

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

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

Loading…
Cancel
Save