gpt4 book ai didi

c++ - boost :msm 中没有默认构造函数的状态机

转载 作者:行者123 更新时间:2023-11-30 05:41:58 31 4
gpt4 key购买 nike

在 boost::msm 的文档中有一个 example没有默认构造函数的状态机。我可以让它在我直接从 super SM 跳到子 SM 的地方工作。但是当我从一个子 SM 跳转到另一个子 SM 时,目标 SM 是默认构造的,我的数据消失了。下面是一个例子。如果不为子状态提供默认构造函数,我就无法编译它。我是否需要在我的子 SM 中提供一些额外的信息才能正常工作?

当我运行示例时,我得到:

Jumping directly to Sub1
Sub1.data_ = 0x7fff5fbfd778
Jumping directly to Sub2
Sub2.data_ = 0x7fff5fbfd778
Jumping from Sub1 to Sub2
Sub1.data_ = 0x7fff5fbfd778
Sub2.data_ = 0xdeadbeef

我希望 0xdeadbeef 与其他地址相同。

#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 {

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

struct MyData {
int data;
};

struct EvGotoSub1 {};
struct EvGotoSub2 {};

struct MainSM_;
using Main = back::state_machine<MainSM_>;
struct Sub1SM_;
using Sub1 = back::state_machine<Sub1SM_>;
struct Sub2SM_;
using Sub2 = back::state_machine<Sub2SM_>;

struct Sub1SM_ : state_machine_def<Sub1SM_> {
struct Started : state<> {
template <class Event,class Fsm>
void on_entry(Event const& e, Fsm& fsm) {
std::cout << "Sub1.data_ = " << (void*) fsm.data_ << std::endl;
}
};
using initial_state = mpl::vector<Started>;
struct transition_table:mpl::vector<
Row<Started, EvGotoSub2, Sub2, none, none>
> {};
MyData* data_;

Sub1SM_() : data_((MyData*) 0xdeadbeef) {};
Sub1SM_(MyData* data) : data_(data) {};
};

struct Sub2SM_ : state_machine_def<Sub2SM_> {
struct Started : state<> {
template <class Event,class Fsm>
void on_entry(Event const& e, Fsm& fsm) {
std::cout << "Sub2.data_ = " << (void*) fsm.data_ << std::endl;
}
};
using initial_state = mpl::vector<Started>;
struct transition_table:mpl::vector<
//Row<Started, EvGotoSub1, Sub1, none, none>
> {};
MyData* data_;

Sub2SM_() : data_((MyData*) 0xdeadbeef) {};
Sub2SM_(MyData* data) : data_(data) {};
};

struct MainSM_ : state_machine_def<MainSM_> {
struct Started : state<> {};
using initial_state = mpl::vector<Started>;
struct transition_table:mpl::vector<
Row<Started, EvGotoSub1, Sub1, none, none>,
Row<Started, EvGotoSub2, Sub2, none, none>
> {};
MyData* data_;

MainSM_(MyData* data) : data_(data) {};
};
}

int main() {
MyData data { 123 };

auto CreateMain = [&data] {
auto ret = Main(back::states_ << Sub1(&data) << Sub2(&data), &data);
ret.start();
return ret;
};

std::cout << "Jumping directly to Sub1" << std::endl;
Main main = CreateMain();
main.process_event(EvGotoSub1());

std::cout << "Jumping directly to Sub2" << std::endl;
main = CreateMain();
main.process_event(EvGotoSub2());

std::cout << "Jumping from Sub1 to Sub2" << std::endl;
main = CreateMain();
main.process_event(EvGotoSub1());
main.process_event(EvGotoSub2());
}

最佳答案

必须定义默认构造函数。对于 Sub1,您还必须指定要使用 Sub2 的哪个构造函数:

auto ret = Main(back::states_ << Sub1(back::states_ << Sub2(&data), &data) << Sub2(&data), &data);

这个输出:

Jumping directly to Sub1
Sub1.data_ = 0xbfe82bf0
Jumping directly to Sub2
Sub2.data_ = 0xbfe82bf0
Jumping from Sub1 to Sub2
Sub1.data_ = 0xbfe82bf0
Sub2.data_ = 0xbfe82bf0

实例:https://ideone.com/XMtDye

关于c++ - boost :msm 中没有默认构造函数的状态机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30909933/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com