Reputation: 6008
I am newbi to boost msm and boost in general. I would like to somehow encapsulate all the:
within a Class.
How can I do that?
// events
struct event1 {};
// front-end: define the FSM structure
struct my_machine_ : public msm::front::state_machine_def<my_machine_>
{
// The list of FSM states
struct State1 : public msm::front::state<>
{
// every (optional) entry/exit methods get the event passed.
template <class Event,class FSM>
void on_entry(Event const&,FSM& ) {std::cout << "entering: State1" << std::endl;}
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) {std::cout << "leaving: State1" << std::endl;}
};
struct State2 : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const& ,FSM&) {std::cout << "entering: State3" << std::endl;}
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) {std::cout << "leaving: State3" << std::endl;}
};
// the initial state of the player SM. Must be defined
typedef State1 initial_state;
typedef my_machine_ p; // makes transition table cleaner
// Transition table
struct transition_table : mpl::vector<
// Start Event Next Action Guard
// +---------+-------------+---------+---------------------+----------------------+
Row < State1 , event1 , State2 >,
Row < State2 , event1 , State1 >
> {};
};
Upvotes: 0
Views: 1267
Reputation: 1631
I got this question not long ago. If you declare the nested Fsm structure in the header, you'll pay for higher compile-times wherever included. An alternative would be:
// PublicClass.hpp
struct PublicClass
{
// forward-declare nested type. This will inherit msm::back::state_machine<...>
struct Fsm;
// with shared_ptr, you don't need the complete type yet.
boost::shared_ptr<Fsm> fsm_;
};
// PublicClass.cpp
// provide definition of Fsm.
struct PublicClass::Fsm : public msm::back::state_machine<my_machine_>{};
HTH, Christophe
Upvotes: 1
Reputation: 59841
To implement the member of a nested use:
// header file
struct Foo {
struct Nested {
void mem();
};
};
// cpp file
void Foo::Nested::mem() {
// implementation goes here
}
Upvotes: 1