提升msm课程?

时间:2012-06-05 14:55:06

标签: class boost state-machine

我是新手,以提升msm和提升。 我想以某种方式封装所有:

  • 事件结构
  • 包含转换表结构和状态结构的stateMahine结构

在一个班级内。

我该怎么做?

// 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                                           >
    > {};

};

2 个答案:

答案 0 :(得分:1)

实现嵌套使用的成员:

// header file
struct Foo {
  struct Nested {
    void mem();
  };
};
// cpp file
void Foo::Nested::mem() {
  // implementation goes here
}

答案 1 :(得分:1)

我不久前得到了这个问题。 如果在标头中声明嵌套的Fsm结构,则无论何处包含,都将支付更高的编译时间。 另一种选择是:

// 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, 克里斯托夫