首页 > 解决方案 > 如何在 boost MSM 中传递其他参数 int 状态进入或退出函数

问题描述

我想在 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)

标签: booststate-machine

解决方案


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

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));
    }
};

推荐阅读