在用?构造的状态机中,是否boost::mp11::mp_list
可以用作转换列表boost::mpl::vector
boost::msm
我刚刚尝试过(链接),似乎:
- 它编译
- 但它不起作用 - 缺少转换表
- 并产生与 相比 1/3 的代码
boost::mpl::vector
。
我也试过boost::fusion::vector
了,它有效。
我做了什么:
我简化了这个例子boost::msm
——见——只是为了有 2 个状态和 2 个转换。
我将所有替换mpl::vector
为TypeList
定义为:
#ifdef USE_FUSION
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/include/mpl.hpp>
template <typename ...T>
using TypeList = boost::fusion::vector<T...>;
#elif USE_MP11
#include <boost/mp11/list.hpp>
#include <boost/mp11/algorithm.hpp>
#include <boost/mp11/mpl.hpp>
template <typename ...T>
using TypeList = boost::mp11::mp_list<T...>;
#else
template <typename ...T>
using TypeList = boost::mpl::vector<T...>;
#endif
状态机如下:
namespace msm = boost::msm;
namespace test_fsm // Concrete FSM implementation
{
// events
struct play {};
struct stop {};
// Concrete FSM implementation
struct player_ : public msm::front::state_machine_def<player_>
{
// no need for exception handling or message queue
typedef int no_exception_thrown;
typedef int no_message_queue;
// The list of FSM states
struct Empty : public msm::front::state<>
{
// optional entry/exit methods
template <class Event,class FSM>
void on_entry(Event const&,FSM& ) { std::cout << "entering: Empty" << std::endl; }
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) { std::cout << "leaving: Empty" << std::endl; }
};
struct Playing : public msm::front::state<>
{
template <class Event,class FSM>
void on_entry(Event const&,FSM& ) { std::cout << "entering: Playing" << std::endl; }
template <class Event,class FSM>
void on_exit(Event const&,FSM& ) { std::cout << "leaving: Playing" << std::endl; }
};
// the initial state of the player SM. Must be defined
typedef Empty initial_state;
// transition actions
void playing(play const&) { }
void stop_playing(stop const&) { }
// guard conditions
typedef player_ p; // makes transition table cleaner
// Transition table for player
struct transition_table : TypeList<
// Start Event Next Action Guard
// +---------+-------------+---------+---------------------+----------------------+
_row < Empty , play , Playing >,
_row < Playing , stop , Empty >
> {};
// Replaces the default no-transition response.
template <class FSM,class Event>
void no_transition(Event const& e, FSM&,int state)
{
std::cout << "no transition from state " << state
<< " on event " << typeid(e).name() << std::endl;
}
};
typedef msm::back::state_machine<player_> player;
//
// Testing utilities.
//
static char const* const state_names[] = { "Empty", "Playing" };
void pstate(player const& p)
{
std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
}
}
对于这个简单的场景:
test_fsm::player p2;
p2.start();
p2.process_event(test_fsm::play());
p2.process_event(test_fsm::stop());
return 0;
基于boost::mpl::vector
(and boost::fusion::vector
and boost::mpl::list
) 的示例按预期生成输出:
entering: Empty
leaving: Empty
entering: Playing
leaving: Playing
entering: Empty
在使用boost::mp11::mp_list
输出时:
entering: Empty
no transition from state 0 on event N8test_fsm4playE
no transition from state 0 on event N8test_fsm4stopE
您是否知道缺少什么boost::mp11
或目前无法boost::mp11::mp_list
用作boost::msm
状态的转换表?