#include #include #include struct Off; // forward declaration // ---------------------------------------------------------------------------- // Event Declarations // struct Toggle : tinyfsm::Event { }; // Event Declarations // ---------------------------------------------------------------------------- // State Machine Declaration // struct Switch : tinyfsm::Fsm { static void reset(void); // NOTE: on reset: "tinyfsm::StateList::reset()", copy // constructor is used by default, so "this" points to neither // "Off" nor "On" (see operator=() below). Switch() : counter(0) { std::cout << "* Switch()" << std::endl << " this = " << this << std::endl; } ~Switch() { std::cout << "* ~Switch()" << std::endl << " this = " << this << std::endl; } Switch & operator=(const Switch & other) { std::cout << "* operator=()" << std::endl << " this = " << this << std::endl << " other = " << &other << std::endl; counter = other.counter; return *this; } virtual void react(Toggle const &) { }; void entry(void); void exit(void); int counter; }; struct On : Switch { void react(Toggle const &) override { transit(); }; }; struct Off : Switch { void react(Toggle const &) override { transit(); }; }; FSM_INITIAL_STATE(Switch, Off) // ---------------------------------------------------------------------------- // State Machine Definitions // void Switch::reset() { tinyfsm::StateList::reset(); } void Switch::entry() { counter++; // debugging only. properly designed state machines don't need this: if(is_in_state()) { std::cout << "* On::entry()" << std::endl; } else if(is_in_state()) { std::cout << "* Off::entry()" << std::endl; } else assert(true); assert(current_state_ptr == this); std::cout << " this (cur) = " << this << std::endl << " state = " << &state() << std::endl << " state = " << &state() << std::endl; } void Switch::exit() { assert(current_state_ptr == this); std::cout << "* exit()" << std::endl << " this (cur) = " << this << std::endl << " state = " << &state() << std::endl << " state = " << &state() << std::endl; } // ---------------------------------------------------------------------------- // Main // int main() { Switch::start(); while(1) { char c; std::cout << "* main()" << std::endl << " cur_counter = " << Switch::current_state_ptr->counter << std::endl << " on_counter = " << Switch::state().counter << std::endl << " off_counter = " << Switch::state().counter << std::endl; std::cout << std::endl << "t=Toggle, r=Restart, q=Quit ? "; std::cin >> c; switch(c) { case 't': Switch::dispatch(Toggle()); break; case 'r': Switch::reset(); Switch::start(); break; case 'q': return 0; default: std::cout << "> Invalid input" << std::endl; }; } }