1

在用?构造的状态机中,是否boost::mp11::mp_list可以用作转换列表boost::mpl::vectorboost::msm

我刚刚尝试过(链接),似乎:

  1. 它编译
  2. 但它不起作用 - 缺少转换表
  3. 并产生与 相比 1/3 的代码boost::mpl::vector

我也试过boost::fusion::vector了,它有效。

我做了什么:

我简化了这个例子boost::msm————只是为了有 2 个状态和 2 个转换。

我将所有替换mpl::vectorTypeList定义为:

#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::vectorand 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状态的转换表?

4

1 回答 1

0

似乎boost::mp11::mp_list用作转换行列表的唯一方法boost::msm是使用 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 问题

于 2021-07-01T06:34:23.323 回答