Unhandled event handler example

Starting with version 1.3.0 yasmine provides the possibility to handle events that were not handled by the state machine (no transition was found for the corresponding event type) and that were not deferred.

We use the state machine according to the following state machine diagram to demonstrate this feature:


In the state machine a behavior is set for the unhandled event handler. It is a very basic behavior that will display a message based on the ID of the events which are neither handled nor deferred.

Based on the events that are fired and handled, the following is going to happen:

  • message for the event E1 is emitted by the unhandled event handler

  • transition from S1 to S2 is executed for the event E2

  • message for the event E3 is emitted by the unhandled event handler

  • message for the event E1 is emitted by the unhandled event handler

Setting the unhandled event handler behavior

void handle_unhandled_event( const sxy::event& _event )
std::cout << "Hello, '" << _event.get_name() << "', unhandled event!" << std::endl;
state_machine_uptr setup_state_machine( const std::string& _name )
state_machine_uptr state_machine = SX_MAKE_UNIQUE< sxy::sync_state_machine >( _name );
sxy::composite_state& root_state = state_machine->get_root_state();
sxy::region& main_region = root_state.add_region( "main region" );
sxy::initial_pseudostate& initial_pseudostate = main_region.add_initial_pseudostate( "initial" );
sxy::simple_state& S1 = main_region.add_simple_state( "S1" );
sxy::simple_state& S2 = main_region.add_simple_state( "S2" );
state_machine->add_transition( sxy::Y_COMPLETION_EVENT_ID, initial_pseudostate, S1 );
state_machine->add_transition( E2, S1, S2 );
state_machine->set_behavior_of_unhandled_event_handler( Y_BEHAVIOR_FUNCTION2( handle_unhandled_event ) );
return( state_machine );

Setting the handler's behavior is possible by calling the set_behavior_of_unhandled_event_handler method of the state machine, passing it to the setter as a behavior_function. In this case we pass the function handle_unhandled_event to the state machine.

For each event that is fired and for which no transition is enabled and which is not deferred, the handle_unhandled_event function gets called.

For the above scenario, we're going to get the output:

Hello, '1', unhandled event!
Hello, '3', unhandled event!
Hello, '1', unhandled event!

Source code

The source code can be found on GitHub.