You can just save them on the stack, although it's a little more complicated :)
However, this is a fun decision, so you are here. The basic principle is to play with decorators and volatility. Code example:
State state1, state2;
How it works?
Instead of state1 being a โsimpleโ object, it will be a bit more confusing. Sort of:
struct State; struct StateImpl { StateImpl(char const* n): name(n) {} char const* name; }; struct StateNode { StateNode(Event e, State const& s, StateNode const* n): event(e), state(s), next(n) {} Event event; State const& destination; StateNode const* next; }; struct State { State(char const* name): me(0), impl(name) {} StateNode const* me; StateImpl impl; };
And then we define a Transition :
struct Transition { Transition(State& origin, Event e, State const& destination): node(e, destination, origin.me) { origin.me = node; } StateNode node; };
Informally, we build a singly linked list, and the head sits in State . We update the title every time we add a transition.
If an event occurs, you need to go through this list until an event occurs and, therefore, is properly sent, or a null pointer is reached, indicating that the event should not be received in this state.
source share