boost - 如何在 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)
解决方案
不确定这是否可能。但是据我所知,您必须在这些函数之外声明该属性。例如,首先声明新属性:
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));
}
};
推荐阅读
- sql-server - 从 SQL Server 连接到 MS Access 数据库的问题
- sql - 如何对列表的所有元素进行“和”运算符?
- python - 如何使用 Pandas 导入谷歌表格?
- reactjs - How To do mapping of click , text and className in Redux's presentational componant
- linux - 使用密码对 wget 请求进行简单的服务器端响应
- javascript - 在 try catch 块内调用的函数中无法捕获错误
- java - Java SE 安装错误
- c# - 单元测试时无法修改应用程序设置
- java - JFrame不显示画布
- r - 在R中绘制相关矩阵