如何在拆分为多个文件的分层 SM 中退出子机器?(使用boost::MSM)



这里有一个很好的教程,解释了如何使用"exit-pseudo-state"从子机器退出boost::MSM

但是我需要将我的SM拆分为多个文件,以使其易于管理,这就是问题出现的地方。

当在主 fsm 文件中定义子 SM 时,一切正常,即从"退出伪状态"退出会导致从子 SM 退出到下一个状态(示例代码(

在单独的文件中实现子 SM,我必须在其中进行额外的虚拟继承级别,这会导致问题。 这一次,在内部子 SM 中转换到退出伪状态不会触发到父 SM 中下一个状态的退出。下面是显示问题的示例代码。

从下面的输出中可以看出,退出子状态后,最后缺少State2::on_exit()21

Testing boost::msm ...
State1::on_entry()
State1::on_exit()
State2::on_entry()
SubState21::on_entry()
SubState21::on_exit()

提前感谢您的帮助

代码包括:

主文件:

#include "myfsm.h"
int main()
{    
std::cout << "Testing boost::msm ..." << std::endl;
MyFsm fsm;
fsm.start();
fsm.process_event(Event1());
fsm.process_event(Event3());
//fsm.process_event(Event2());
}

主要密克罗尼西亚联邦:

#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
#include "state2.h"
#include "events.h"
namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;
struct MyFsm_ : msmf::state_machine_def<MyFsm_>
{
struct State1 : msmf::state<>{
template<class Event, class Fsm> void on_entry(const Event&, Fsm&) const {std::cout << "State1::on_entry()" << std::endl;}
template<class Event, class Fsm> void on_exit(const Event&, Fsm&) const {std::cout << "State1::on_exit()" << std::endl;}
};    
struct State2m : State2 {};
// Set initial state
typedef State1 initial_state;
// Transition table
struct transition_table:mpl::vector<
msmf::Row < State1, Event1, State2m, msmf::none, msmf::none >,
msmf::Row < State2m, Event2, State1, msmf::none, msmf::none >,
msmf::Row < State2::exit_pt
<State2_::Exit2>, msmf::none, State1, msmf::none, msmf::none >
>{};
template<class Event, class Fsm>
void no_transition(Event const&, Fsm&, int state){
std::cout<<"no_transiton detected from state: "<< state << std::endl;
}
};
// Pick a back-end
typedef msm::back::state_machine<MyFsm_> MyFsm;    

子 SM 即状态 2.h:

#include <iostream>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
#include "events.h"
namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;

struct State2_ : msmf::state_machine_def<State2_>{
template<class Event, class Fsm> void on_entry(const Event&, Fsm&) const {std::cout << "State2::on_entry()" << std::endl;}
template<class Event, class Fsm> void on_exit(const Event&, Fsm&) const {std::cout << "State2::on_exit()" << std::endl;}
struct SubState21 : msmf::state<>{
template<class Event, class Fsm> void on_entry(const Event&, Fsm&) const {std::cout << "SubState21::on_entry()" <<std::endl;}
template<class Event, class Fsm> void on_exit(const Event&, Fsm&) const {std::cout << "SubState21::on_exit()" << std::endl;}
};
typedef mpl::vector<SubState21> initial_state;
struct Exit2 : msmf::exit_pseudo_state<msmf::none> {};
struct transition_table:mpl::vector<
msmf::Row < SubState21, Event3, Exit2, msmf::none, msmf::none >
>{};
};
typedef msm::back::state_machine<State2_> State2;

您需要在msmf::state_machine_def外部定义msm::back::state_machine

下面是示例的更新版本:

https://wandbox.org/permlink/DktAL169yjFNnmfl

有四个更新点。

state2.h(1(

// 1. move msm::back::state_machine to fsm
// typedef msm::back::state_machine<State2_> State2;

myfsm.h(2, 3, 4(

struct State2m : State2_ {}; // 2. replaced State2 to State2_
typedef msm::back::state_machine<State2m> State2mm; // 3. define backend here

// Transition table
struct transition_table:mpl::vector<
// 4. updated transition table using State2mm
msmf::Row < State1, Event1, State2mm, msmf::none, msmf::none >,
msmf::Row < State2mm, Event2, State1, msmf::none, msmf::none >,
msmf::Row < State2mm::exit_pt
<State2mm::Exit2>, msmf::none, State1, msmf::none, msmf::none >
>{};

在这种情况下,您可以做更优雅的方式。您可以删除struct State2m : State2_ {};继承。

这是代码:

https://wandbox.org/permlink/FO7lEELLtLhiismu

您可以按如下方式定义State2并在转换表中使用State2

typedef msm::back::state_machine<State2_> State2;
// Transition table
struct transition_table:mpl::vector<
msmf::Row < State1, Event1, State2, msmf::none, msmf::none >,
msmf::Row < State2, Event2, State1, msmf::none, msmf::none >,
msmf::Row < State2::exit_pt
<State2::Exit2>, msmf::none, State1, msmf::none, msmf::none >
>{};

最新更新