boost::msm -一种获取字符串表示的方法(例如:getName)
boost::msm - A way to get a string representation (ie. getName) of a state?
我试图使用boost::msm库在我的代码中创建一个状态机。有人知道一种方法来获得一个状态的字符串名称(不是int id)吗?我试图得到这个日志/调试的目的。例如,在no_transition函数中,我得到的是状态id但我想要得到的是名称,这样更容易读:
template <class Event ,class Fsm>
void no_transition(Event const& e, Fsm& fsm, int stateId)
{
//This is what I'm trying:
auto state = fsm.get_state_by_id(stateId); //This returns a boost::msm::front::default_base_state. Anything I can override in there to set a name?
const char* stateName = state->getStateName(); //I want to do something like this since I can do e.getEventId()
print("FSM rejected the event %s as there is no transition from current state %s (%d)n", e.getEventId(), stateName, stateId);
}
下面是我如何定义事件和状态的:状态:struct Idle : front::state<> {
static const char* const getStateName() {
return "Idle";
}
};
事件:struct SampleEvent {
SampleEvent() {}
static const char* const getEventId() {
return "SampleEvent";
}
};
任何想法都会很棒。谢谢!
您可以使用以下代码实现所需的效果:
#include <boost/msm/back/tools.hpp>
#include <boost/msm/back/metafunctions.hpp>
#include <boost/mpl/for_each.hpp>
.......
.......
template <class Event ,class Fsm>
void no_transition(Event const& e, Fsm& fsm, int stateId){
typedef typename boost::msm::back::recursive_get_transition_table<FSM>::type recursive_stt;
typedef typename boost::msm::back::generate_state_set<recursive_stt>::type all_states;
std::string stateName;
boost::mpl::for_each<all_states,boost::msm::wrap<boost::mpl::placeholders::_1> >(boost::msm::back::get_state_name<recursive_stt>(stateName, state));
std::cout << "No transition from state: " << stateName << std::endl;}
-
最佳选择:使用访客。
浏览http://www.boost.org/doc/libs/1_53_0/libs/msm/doc/HTML/examples/SM-2Arg.cpp -
使用no_transition的最后一个参数(state_id),您可以从数组中检索名称:
static char const* const state_names[] = {"State1", "State2",…};
print (state_name [state_id]);
请注意,状态ID仅用于调试目的。ID是在编译时生成的,取决于转换表
中条目的顺序。对MSM文档的引用:
- http://www.boost.org/doc/libs/1_53_0_beta1/libs/msm/doc/HTML/ch06s03.html
相关文章:
- 寻找一种更好的方法来表示无符号字符数组
- 内存效率表示最短路径的方法?
- 在 C++ 中将整数数组转换为位集表示形式的最佳方法?
- 在C++中,将无符号整数转换为八进制表示,反之亦然的最佳方法是什么
- 在 C++17 中表示 std::byte 文字的正确方法是什么?
- 用各种语言表示模式的方法
- 在C++中将数据结构表示为字符串的首选方法是什么?
- 将两位数字转换为低内存表示的最快方法
- 在 C++11 中表示日内时间 HH:MM:SS 的最佳方法是什么?
- 计算机是否总是遵循 2 的补码方法来表示负数?
- 一种使用 unordered_map 表示对称稀疏矩阵的有效方法
- 类方法接受表示各种参数数函数的 lambda
- C 便携式方法获取u8字符串文字的值表示
- STL表示隐含交叉引用的数据结构的方法
- 将二进制字符的字符串表示转换为字符串的最佳方法
- 有没有一种方法可以打印对象的位表示
- C++ - 方法重写未按预期表示
- C++ 一种方法,该方法将十进制值作为用布尔数组表示的二进制的整数返回
- 有没有一种方法可以将表示为字符串的数字转换为等效的二进制数字
- 寻找连接对象的字符串表示的惯用方法