As we saw in chapter - 4 : Working with Meta States, if an event is unhandled in Inner State, it gets automatically propagated to Outer State. Which means all unhandled states can be handled in Outer State.
This is good unless we come to a situation where we want only certain events to be propagated to Outer State because we want to do something as part of this event handling in Inner State as well as in_ Outer state
We can use forward_even() _for these situations
Let's construct a state with an inner state, which will forward the event to outer state
struct firstState;
// Inner States of firstState
struct firstState_Inner_1;
// Inner State Events that will be forwarded
struct event_First_Inner_1 : sc::event<event_First_Inner_1> {};
// The State Machine
struct statemachine : sc::state_machine<statemachine, firstState> {};
// First State
struct firstState : sc::simple_state<firstState, statemachine, firstState_Inner_1>
{
firstState() { cout << "In State => firstState" << endl; }
typedef sc::custom_reaction<event_First_Inner_1> reactions;
sc::result react(const event_First_Inner_1 & event) {
cout << "Received Event @ Outer State => firstState" << endl;
return discard_event();
}
};
// Inner State
struct firstState_Inner_1 : sc::simple_state<firstState_Inner_1, firstState> {
firstState_Inner_1() { cout << "In State => firstState_Inner_1" << endl; }
typedef sc::custom_reaction<event_First_Inner_1> reactions;
sc::result react(const event_First_Inner_1 & event) {
cout << "Forwarding Event => event_First_Inner_1 => From firstState_Inner_1" << endl;
return forward_event();
}
};
// The Main
int main() {
statemachine sm;
sm.initiate();
// Inner state Events
sm.process_event(event_First_Inner_1());
return 0;
}
As the output of the above program shows that, the event event_First_Inner_1 first gets handled in the Inner State where it forwards the same, which is then handled by Outer State .
The same behaviour can be repetitive, if we trigger the event more than once using the code
sm.process_event(event_First_Inner_1());
sm.process_event(event_First_Inner_1());
forward_event() also provides us the flexibility of choosing, which all events are allowed to be propagate to outer state and which are not
In this chapter, we learnt how to forward the events from an inner state to outer state selectively. We also know from the code that the same event could be handled twice, once in Inner State and then inside Outer State