I'm trying to learn boost::statechart.
I want to make a little app which loads a file.
// --------------------------------
// | |
// | O Project |
// | | |
// | v |
// | ---------------------------- |
// | | | |
// | | Unloaded | |
// | ---------------------------- |
// | | ^ |
// | | EvLoad | EvUnload |<-----O
// | v | |
// | ---------------------------- |
// | | | |
// | | Loaded | |
// | ---------------------------- |
// | | ^ |
// | | | EvLoad |
// | ----- |
// --------------------------------
But how do i transport arguments to the state, e.g. a filename? If i store the filename inside EvLoad i can access it easily for the in state reaction
struct Loaded : sc::simple_state< Loaded, Project>
{
typedef sc::custom_reaction< EvLoad > reactions;
sc::result react( const EvLoad & e )
{
//load file e.path()
...
return discard_event();
}
}
But when I'm in the Unloaded state then I'm invoking the constructor of Loaded and i can't pass arguments to it. The only workaround I came up with is reposting the event before doing the transition, but this looks a bit dirty to me.
struct Unloaded : sc::simple_state< Unloaded, Project >
{
typedef sc::custom_reaction< EvLoad > reactions;
sc::result react( const EvLoad & e )
{
post_event( e ); //workaround to pass the event to the loaded state
return transit<Loaded>();
}
};
Is there a better alternative?
We use the triggering_event method to pull the triggering event, and then just attach the data as member variables to triggering event. It saves a lot of coding effort and keeps us from having to generate custom reactions or attaching transition variables to the statechart (two common approaches I've seen).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With