36 static_assert(std::is_enum<Message>::value,
"Message should be an enum (it's easier that way)");
37 static_assert(std::is_enum<IDType>::value,
"IDType should be an enum (it's easier that way)");
80 virtual void entry(System &) {}
84 virtual void exit(System &) {}
86 virtual State *respond(System &s, Message m) = 0;
88 virtual IDType id()
const = 0;
96 using thread_data = std::pair<State *, StateMachine *>;
128 mutable vex::mutex mut;
129 MaybeMessage incoming_msg;
138 static int thread_runner(
void *vptr) {
139 thread_data *ptr =
static_cast<thread_data *
>(vptr);
140 State *cur_state = ptr->first;
143 System &derived = *
static_cast<System *
>(&sys);
145 cur_state->entry(derived);
147 sys.cur_type = cur_state->id();
149 auto respond_to_message = [&](Message msg) {
151 printf(
"responding to msg: %s\n", to_string(msg).c_str());
155 State *next_state = cur_state->respond(derived, msg);
157 if (cur_state != next_state) {
161 cur_state->exit(derived);
162 next_state->entry(derived);
166 cur_state = next_state;
167 sys.cur_type = cur_state->id();
175 std::string str = to_string(cur_state->id());
176 std::string str2 = to_string(sys.cur_type);
178 printf(
"state: %s %s\n", str.c_str(), str2.c_str());
182 MaybeMessage internal_msg = cur_state->work(derived);
184 if (internal_msg.has_message()) {
185 respond_to_message(internal_msg.message());
190 MaybeMessage incoming = sys.incoming_msg;
191 sys.incoming_msg = {};
194 if (incoming.has_message()) {
195 respond_to_message(incoming.message());
IDType current_state() const
retrieve the current state of the state machine. This is safe to call from external threads
Definition state_machine.h:109