【发布时间】:2018-11-08 05:57:39
【问题描述】:
我想将 boost::msm 状态机的实现拆分成多个文件。我正在寻找类似的东西:
1) 每个州一个标题
2) 主状态机的一个标头(最外面的 SM) 但是我不知道这个文件应该怎么写
3) 使用 SM 的客户端代码。
我想出的内容如下(无法编译,以以下形式给出错误:“不完整类型的无效使用”和其他错误)。
第一个 Sample 状态:
//State1.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>
namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;
struct State1:msmf::state<>
{
// Entry action
template <class Event,class Fsm>
void on_entry(Event const&, Fsm&) const {
std::cout << "State1::on_entry()" << std::endl;
}
// Exit action
template <class Event,class Fsm>
void on_exit(Event const&, Fsm&) const {
std::cout << "State1::on_exit()" << std::endl;
}
};
第二个样本状态:
//State2.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>
namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;
struct State2:msmf::state<>
{
// Entry action
template <class Event,class Fsm>
void on_entry(Event const&, Fsm&) const {
std::cout << "State2::on_entry()" << std::endl;
}
// Exit action
template <class Event,class Fsm>
void on_exit(Event const&, Fsm&) const {
std::cout << "State2::on_exit()" << std::endl;
}
};
主要的 SM:
//MyFsm.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 "state1.h"
#include "state2.h"
namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;
// ----- Events
struct Event1 {};
struct Event2 {};
struct MyFsm_ : msmf::state_machine_def<MyFsm_>
{
struct State1;//??? is this the correct way
struct State2;//???
// Set initial state
typedef State1 initial_state;
// Transition table
struct transition_table:mpl::vector<
...
>{};
};
// Pick a back-end
typedef msm::back::state_machine<MyFsm_> MyFsm;
客户端代码:
//main.h
#include "myfsm.h"
int main()
{
MyFsm fsm;
fsm.start();
fsm.process_event(Event1());
}
感谢任何有关如何将 boost:msm 拆分为多个文件的帮助和提示。
【问题讨论】:
标签: c++ boost template-meta-programming state-machine boost-msm