How to retreive containing state machine with Boost MSM

602 views Asked by At

I use Boost MSM, and I have to pass data / use functions around my state machine. For this I use actions or on_entry hooks to get the data from the event, or get_state() to get data from a previous use state.

Howerver I cannot access the underlying state machine with this technique, and I cannot access at all at the main state machine when I am in a sub-statemachine.

For instance (with playground link):

#include <iostream>

#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/functor_row.hpp>
#include <boost/msm/front/state_machine_def.hpp>

namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;

// ----- Events
struct Ev1 { int data; };
struct Ev2 {};

// ----- State machine
struct MainStateMachine_front : msmf::state_machine_def<MainStateMachine_front>
{
    struct StartState: msmf::state<>
    {};

    struct SubStateMachine_front: msmf::state_machine_def<SubStateMachine_front>
    {
        struct StartSubState: msmf::state<>
        {};

        struct ProcessingState: msmf::state<>
        {
            template <class Event, class FSM>
            void on_entry(const Event &, FSM &fsm)
            {
                const SubStateMachine_front &subSm = fsm.template get_state<const SubStateMachine_front&>(); // Doesn't compile
                MainStateMachine_front &sm = fsm.template get_state<MainStateMachine_front&>(); // Doesn't compile

                // Doesn't compile either
                // const msmf::state_machine_def<SubStateMachine_front> &subSm = fsm.template get_state<const msmf::state_machine_def<SubStateMachine_front>&>(); // Doesn't compile
                // msmf::state_machine_def<MainStateMachine_front> &sm = fsm.template get_state<msmf::state_machine_def<MainStateMachine_front>&>(); // Doesn't compile

                Ev1 dataEvent = subSm.entry_event;
                sm.my_awesome_function(dataEvent.data);
            }
        };

        using initial_state = StartSubState;

        struct transition_table:mpl::vector<
            //          Start             Event     Target
            msmf::Row < StartSubState,    Ev2,      ProcessingState>
        > {};

        template <class FSM>
        void on_entry(const Ev1 &evt, FSM &)
        {
            entry_event = evt;
        }

        Ev1 entry_event;
    };

    using SubStateMachine = msm::back::state_machine<SubStateMachine_front>;

    // Set initial state
    using initial_state = StartState;
    // Transition table
    struct transition_table:mpl::vector<
        //          Start             Event     Target
        msmf::Row < StartState,       Ev1,      SubStateMachine>
    > {};

    void my_awesome_function(int data)
    {
        std::cout << "Awesome " << data << std::endl;
    }
};

using MainStateMachine = msm::back::state_machine<MainStateMachine_front>;

int main()
{
    MainStateMachine sm;
    sm.start();
    sm.process_event(Ev1 { .data = 42 } );
    sm.process_event(Ev2 {});
}

In ProcessingState::on_entry, I cannot use get_state to get the SubStateMachine, nor the MainStateMachine. I know FSM will resolve to SubStateMachine, but is there a way to get the MainStateMachine ?

0

There are 0 answers