如何在Boost MSM中传递附加参数int状态进入或退出函数

时间:2019-07-13 06:40:54

标签: boost state-machine

我想在Boost.MSM状态进入或退出函数中提供自己的函数参数。有可能吗?

例如,原始示例是:

BOOST_MSM_EUML_ACTION(state_entry)
{
  template <class Event, class Fsm, class State>
  void operator()(const Event &ev, Fsm &fsm, State &state) const
  {
    std::cout << "Entering\n";
  }
};

BOOST_MSM_EUML_ACTION(state_exit)
{
  template <class Event, class Fsm, class State>
  void operator()(const Event &ev, Fsm &fsm, State &state) const
  {
    std::cout << "Exiting\n";
  }
};

BOOST_MSM_EUML_STATE((state_entry, state_exit), Off)
BOOST_MSM_EUML_STATE((state_entry, state_exit), On)

我想要的是这样的

BOOST_MSM_EUML_ACTION(state_entry)
{
  template <class Event, class Fsm, class State>
  void operator()(const Event &ev, Fsm &fsm, State &state, int n) const
  {
    std::cout << "Entering\n";
  }
};

BOOST_MSM_EUML_STATE((state_entry(100), state_exit), Off)

1 个答案:

答案 0 :(得分:0)

不确定是否可行。但是据我所知,您必须在那些函数之外声明该属性。 例如,首先声明新属性:

BOOST_MSM_EUML_DECLARE_ATTRIBUTE(int, myValue)

然后在状态机定义上传递此属性:

BOOST_MSM_EUML_DECLARE_STATE_MACHINE((yourSmTable, // the transition table
                                     init_ << InitialState, // the initial state
                                     no_action,       // no entry action defined
                                     no_action,       // no exit action defined
                                     attributes_ << myValue // add attribute to state machine
                                     ), YourStateMachine_)

最后在函数state_entry上,获取属性myValue:

BOOST_MSM_EUML_ACTION(state_entry)
{
    template <class Event, class Fsm, class State>
    void operator()(const Event &ev, Fsm &fsm, State &state, int n) const
    {
        std::cout << "Entering\n";
        int yourValue = fsm.get_attribute(myValue));
    }
};