我正在尝试SyncBox
在主状态机和子状态机之间共享对象。理想的方法是将它传递给构造函数(在更复杂的情况下,子状态机将是区域之一的初始状态)。无论如何,我无法使这段代码正确编译和执行。有什么帮助吗?
#include <iostream>
#include <mutex>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
#include <boost/msm/front/euml/common.hpp>
namespace msm = boost::msm;
namespace mpl = boost::mpl;
using namespace boost::msm::front;
class SyncBox {
std::mutex mtx;
};
struct SubSMFE : public msm::front::state_machine_def<SubSMFE>
{
struct InitState : public msm::front::state<> {};
struct FakeState : public msm::front::state<> {};
typedef InitState initial_state;
struct transition_table : mpl::vector<
Row < InitState, none, FakeState>
> {};
};
//typedef msm::back::state_machine<SubSMFE> SubSM;
class SubSM : public msm::back::state_machine<SubSMFE> {
private:
SyncBox& sb;
public:
SubSM(SyncBox& sb) : sb(sb) { std::cout << "SubSMFE constructor" << std::endl; };
void oneFunction() {
// here i use syncBox. it must be a function of SubSM, not SubSMFE (oneFunction overrides start or enqueue_event)
};
};
struct mainSMFE : public msm::front::state_machine_def<mainSMFE>
{
protected:
SyncBox sb;
public:
struct InitState : public msm::front::state<> {};
typedef InitState initial_state;
struct transition_table : mpl::vector<
Row < InitState, none, SubSM>
> {};
};
class mainSM : public msm::back::state_machine<mainSMFE> {
public:
mainSM() : msm::back::state_machine<mainSMFE>(msm::back::states_ << SubSM(sb)) { };
};
int main()
{
mainSM sm;
return 0;
}