c++boostboost-msmboost-mp11

boost::mp11::mp_list can't define proper transition table for FSM based on boost::msm, what is missing?


Does boost::mp11::mp_list can be used instead of boost::mpl::vector as transition-list in state-machines constructed with boost::msm?

I just tried it (link) and it seems that:

  1. it compiles
  2. but it does not work - transitions tables are missing
  3. and produces 1/3 code comparing to boost::mpl::vector.

I also tried boost::fusion::vector and it works.

What I did:

I simplified the example from boost::msm - see - just to have 2 states and 2 transitions.

I replaced all mpl::vector with TypeList defined as:

#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

The state machine is as follows:

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;
}

}

For this simple scenario:

test_fsm::player p2;
p2.start();
p2.process_event(test_fsm::play());
p2.process_event(test_fsm::stop()); 
return 0;

The example based on boost::mpl::vector (and boost::fusion::vector and boost::mpl::list) produces the output as expected:

entering: Empty 
leaving: Empty 
entering: Playing 
leaving: Playing
entering: Empty

while using of boost::mp11::mp_list outputs this:

entering: Empty
no transition from state 0 on event N8test_fsm4playE
no transition from state 0 on event N8test_fsm4stopE

Do you know what is missing to use boost::mp11 or that it is currently impossible to use boost::mp11::mp_list as transition tables for boost::msm states?


Solution

  • It seems that the only way to use boost::mp11::mp_list as a list of transition rows in boost::msm is to use typedef (using) - inheritance does not work; required changes are as follows:

    
        // Transition table for player
        using transition_table = boost::mp11::mp_list<
                _row < Empty , play        , Playing       >,
                _row < Playing , stop        , Empty       >
        >;
    
    

    Next observation is that this version, with type-alias, works also for other types: boost::mpl::vector, boost::fusion::vector.

    I guess, using of inheritance in all boost::msm examples (instead of type-alias) for defining transition tables, is probably reasoned by shortening template instantiations names length (not sure that).

    Well, anyway, I reported it as boost::mp11 issue.