在用boost::msm
构造的状态机中,boost::mp11::mp_list
而不是boost::mpl::vector
是否可以用作转换列表?
我刚试过它(链接(,它似乎是:
- 它编译
- 但它不起作用-缺少转换表
- 并产生与CCD_ 4相比较的1/3码
我也试过boost::fusion::vector
,它有效。
我做了什么:
我简化了boost::msm
中的示例—请参阅—只得到2个状态和2个转换。
我用TypeList
替换了所有mpl::vector
,定义为:
#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
(以及boost::fusion::vector
和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
状态的转换表吗?
在boost::msm
中使用boost::mp11::mp_list
作为转换行列表的唯一方法似乎是使用typedef(using(-继承不起作用;所需的更改如下:
// Transition table for player
using transition_table = boost::mp11::mp_list<
_row < Empty , play , Playing >,
_row < Playing , stop , Empty >
>;
接下来的观察是,这个带有类型别名的版本也适用于其他类型:boost::mpl::vector
、boost::fusion::vector
。
我想,在所有boost::msm
示例中使用继承(而不是类型别名(来定义转换表,可能是因为缩短了模板实例化名称的长度(不确定(。
好吧,不管怎样,我把它作为boost::mp11
期报道了。