ePrivacy and GPDR Cookie Consent by Cookie Consent

Accessing events in behaviors and guards

There are two possibilities to get the actual event types in behaviors and/or guards. One is by using the predefined macros and one is by manually casting the events to the actual event type.

Strongly typed events (C++11 only)

In yasmine there are two very useful macros for passing handlers as behaviors into a state or a transition:

Macro

Syntax

Description

Y_BEHAVIOR_METHOD2

Y_BEHAVIOR_METHOD2( _class_instance, _method1[, _method2, _method3, _method4, _method5, _method6, _method7, _method8, _method9, _method10] )

The macro adds the specified class method(s) as behavior(s) to states or transitions.
The macro is variadic and can take up to 10 class methods pointers as parameters.

Y_BEHAVIOR_FUNCTION2

Y_BEHAVIOR_FUNCTION2( _function1[, _function2, _function3, _function4, _function5, _function6, _function7, _function8, _function9, _function10] )

The macro adds the specified function(s) as behavior(s) to states or transitions.
The macro is variadic and can take up to 10 function pointers as parameters.

Y_GUARD_METHOD2

Y_GUARD_METHOD2( _class_instance, _method1[, _method2, _method3, _method4, _method5, _method6, _method7, _method8, _method9, _method10] )

The macro adds the specified class method(s) as guard(s) to states or transitions.
The macro is variadic and can take up to 10 class methods pointers as parameters.

Y_GUARD_FUNCTION2

Y_GUARD_FUNCTION2( _function1[, _function2, _function3, _function4, _function5, _function6, _function7, _function8, _function9, _function10] )

The macro adds the specified function(s) as guard(s) to states or transitions.
The macro is variadic and can take up to 10 function pointers as parameters.

Passing the handlers correctly

There are some things you have to pay attention to when passing handlers to the state machine:

You have to make sure to pass a handler for each and every event type that can reach a state (or that can trigger a transition) to Y_BEHAVIOR_METHOD2, Y_BEHAVIOR_FUNCTION2, Y_GUARD_METHOD2 or Y_GUARD_FUNCTION2! If you fail to do this, the state machine will abort, if such an event occurs.

These macros are only available with C++11 support. Inside the Events with parameters example some use cases of this macros are shown.

If you need macros for events with more than 5 parameters, you can just create your own macros by copying the predefined ones and extending them with more parameters.

Manual cast

It is possible to use a dynamic_cast to adjust the event type to its actual type inside a behavior:

const sxy::event_2* event_with_param = dynamic_cast< const sxy::event_2* >( &_event );

The complete code can be found in the Events with parameters example.