The following example is a Boost statechart implementation based on the motor state machine described here. Instead of using a custom_reaction to pull the speed out of the EvSetSpeed event (as in other states here), I've modified it to call triggering_event() in the constructor of the Start state.
Things to note:
- The older documentation for boost statechart here states: "the caller of triggering_event needs to make a type check or cast the return value", as triggering_event returns a const event_base pointer.
- Calling triggering_event during construction requires that the calling state derive from state, not simple_state. This is because triggering_event is accessing the state machine from the constructor. For what its worth, I also noted the documentation states that the use of triggering_event often implies a problem with the state machine design and should be avoided.
- Perhaps someone more familiar with boost statechart can comment on this, but I found I had to re-post the triggering event in the Start state constructor if I wanted it to transition to Running (as my reaction list states).
Finally, it's worth mentioning that trying to call triggering_event in the constructor of the initial state of a state machine will result in a null return value. This is because the initial state is constructed during the call to initiate, where there is no triggering event.
#include <boost/statechart/state_machine.hpp>
#include <boost/statechart/simple_state.hpp>
#include <boost/statechart/state.hpp>
#include <boost/statechart/event.hpp>
#include <boost/statechart/in_state_reaction.hpp>
#include <boost/statechart/transition.hpp>
#include <boost/mpl/list.hpp>
#include <boost/statechart/custom_reaction.hpp>
#include <iostream>
namespace mpl = boost::mpl;
namespace sc = boost::statechart;
class EvHalt : public sc::event< EvHalt > {};
class EvSetSpeed : public sc::event< EvSetSpeed >
{
public:
EvSetSpeed(int speed) : _speed(speed) {}
int getSpeed() const { return _speed; };
private:
int _speed;
};
class Idle;
class Start;
class Stop;
class Running;
class Motor : public sc::state_machine<Motor, Idle> {};
class Idle : public sc::simple_state<Idle, Motor>
{
public:
typedef mpl::list<
sc::transition< EvSetSpeed, Start >,
sc::transition< EvHalt, Idle > > reactions;
Idle() { std::cout << "Entering Idle State" << std::endl; };
~Idle() { std::cout << "Exiting Idle State" << std::endl; };
};
class Start : public sc::state<Start, Motor>
{
public:
typedef mpl::list<
sc::transition< EvSetSpeed, Running >,
sc::transition< EvHalt, Stop > > reactions;
Start(my_context ctx) : _speed(0), my_base(ctx)
{
auto trigEvt = dynamic_cast<const EvSetSpeed*>(triggering_event());
if (trigEvt) // returns true if trigEvt is indeed an EvSetSpeed event
{
_speed = trigEvt->getSpeed();
post_event(*trigEvt);
std::cout << "Entering Start State at speed " << _speed << std::endl;
}
else
std::cout << "Entering Start State. Triggering Event is NOT EvSetSpeed." << std::endl;
};
~Start() { std::cout << "Exiting Start State" << std::endl; };
private:
int _speed; // Unused in this example.
};
class Running : public sc::simple_state<Running, Motor>
{
public:
typedef mpl::list<
sc::custom_reaction< EvSetSpeed >,
sc::custom_reaction< EvHalt > > reactions;
sc::result react(const EvSetSpeed& ev)
{
std::cout << "In Running Mode: Set Speed to " << ev.getSpeed() << std::endl;
return discard_event();
}
sc::result react(const EvHalt& ev)
{
std::cout << "In Running Mode: Halting Motor" << std::endl;
post_event(ev);
return transit<Stop>();
}
Running() { std::cout << "Entering Running State" << std::endl; };
~Running() { std::cout << "Exiting Running State" << std::endl; };
};
class Stop : public sc::simple_state<Stop, Motor>
{
public:
typedef sc::transition< EvHalt, Idle > reactions;
Stop() { std::cout << "Entering Stop State" << std::endl; };
~Stop() { std::cout << "Exiting Stop State" << std::endl; };
};
int main()
{
Motor myMotor;
myMotor.initiate();
myMotor.process_event(EvSetSpeed(100));
myMotor.process_event(EvSetSpeed(200));
myMotor.process_event(EvHalt());
myMotor.process_event(EvSetSpeed(300));
myMotor.process_event(EvHalt());
myMotor.process_event(EvHalt());
return 0;
}