boost 里面有状态机了,如何将数据传递到当前Boost元状态机(MSM)子状态

该博客介绍了如何在Boost库的MSM(状态机库)中实现在当前子状态1中连续传递数据,并根据数据发送Event3或Event1。通过调用get_state获取状态对象,然后调用自定义方法如sendData()来传递数据。示例代码展示了如何在状态机中添加这个功能,并给出了修改后的输出结果。
摘要由CSDN通过智能技术生成

In the following example, while the current execution is still in substate1, I want to pass data to substate1 continuously and then send Event3 or Event1 based on the data. Looks like MSM supports only sending events using (process_event()), but I am not sure how to send data continuously to the current state.

#include

#include

#include

#include

namespace {

namespace msm = boost::msm;

namespace msmf = boost::msm::front;

namespace mpl = boost::mpl;

// ----- Events

struct Event1 {};

struct Event2 {};

struct Event3 {};

// ----- State machine

struct OuterSm_:msmf::state_machine_def

{

struct State1_:msmf::state_machine_def

{

template

void on_entry(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "State1::on_entry()" << std::endl;

}

template

void on_exit(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "State1::on_exit()" << std::endl;

}

struct SubState1:msmf::state<> {

template

void on_entry(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "SubState1::on_entry()" << std::endl;

}

template

void on_exit(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "SubState1::on_exit()" << std::endl;

}

};

struct SubState2:msmf::state<> {

template

void on_entry(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "SubState2::on_entry()" << std::endl;

}

template

void on_exit(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "SubState2::on_exit()" << std::endl;

}

};

// Set initial state

typedef mpl::vector initial_state;

// Transition table

struct transition_table:mpl::vector<

// Start Event Next Action Guard

msmf::Row < SubState1, Event2, SubState2, msmf::none, msmf::none >,

msmf::Row < SubState2, Event3, SubState1, msmf::none, msmf::none >

> {};

};

struct State2:msmf::state<>

{

template

void on_entry(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "State2::on_entry()" << std::endl;

}

template

void on_exit(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "State2::on_exit()" << std::endl;

}

};

typedef msm::back::state_machine State1;

// Set initial state

typedef State1 initial_state;

// Transition table

struct transition_table:mpl::vector<

// Start Event Next Action Guard

msmf::Row < State1, Event1, State2, msmf::none, msmf::none >

> {};

};

// Pick a back-end

typedef msm::back::state_machine Osm;

void test()

{

Osm osm;

osm.start();

std::cout << "> Send Event2()" << std::endl;

osm.process_event(Event2());

std::cout << "> Send Event1()" << std::endl;

osm.process_event(Event1());

}

}

int main()

{

test();

return 0;

}

Output:

State1::on_entry()

SubState1::on_entry()

> Send Event2()

SubState1::on_exit()

SubState2::on_entry()

> Send Event1()

SubState2::on_exit()

State1::on_exit()

State2::on_entry()

解决方案

You can use get_state to retrieve a state from the state machine and then you can call an arbitrary method on that state, e.g. sendData() in the following modification of your example (live example):

#include

#include

#include

#include

namespace {

namespace msm = boost::msm;

namespace msmf = boost::msm::front;

namespace mpl = boost::mpl;

// ----- Events

struct Event1 {};

struct Event2 {};

struct Event3 {};

// ----- State machine

struct OuterSm_:msmf::state_machine_def

{

struct State1_:msmf::state_machine_def

{

template

void on_entry(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "State1::on_entry()" << std::endl;

}

template

void on_exit(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "State1::on_exit()" << std::endl;

}

struct SubState1:msmf::state<> {

template

void on_entry(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "SubState1::on_entry()" << std::endl;

}

template

void on_exit(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "SubState1::on_exit()" << std::endl;

}

void sendData(const std::string& data)

{

std::cout << "data received in Substate1:" << data << std::endl;

}

};

struct SubState2:msmf::state<> {

template

void on_entry(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "SubState2::on_entry()" << std::endl;

}

template

void on_exit(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "SubState2::on_exit()" << std::endl;

}

};

// Set initial state

typedef mpl::vector initial_state;

// Transition table

struct transition_table:mpl::vector<

// Start Event Next Action Guard

msmf::Row < SubState1, Event2, SubState2, msmf::none, msmf::none >,

msmf::Row < SubState2, Event3, SubState1, msmf::none, msmf::none >

> {};

};

struct State2:msmf::state<>

{

template

void on_entry(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "State2::on_entry()" << std::endl;

}

template

void on_exit(Event const&, Fsm&) const {

BOOST_STATIC_ASSERT((boost::is_convertible::value));

std::cout << "State2::on_exit()" << std::endl;

}

};

typedef msm::back::state_machine State1;

// Set initial state

typedef State1 initial_state;

// Transition table

struct transition_table:mpl::vector<

// Start Event Next Action Guard

msmf::Row < State1, Event1, State2, msmf::none, msmf::none >

> {};

};

// Pick a back-end

typedef msm::back::state_machine Osm;

void test()

{

Osm osm;

osm.start();

Osm::State1& state1 = osm.get_state<:state1>();

Osm::State1::SubState1& substate1 = state1.get_state<:state1::substate1>();

substate1.sendData("hello from outside");

std::cout << "> Send Event2()" << std::endl;

osm.process_event(Event2());

std::cout << "> Send Event1()" << std::endl;

osm.process_event(Event1());

}

}

int main()

{

test();

return 0;

}

Output:

State1::on_entry()

SubState1::on_entry()

data received in Substate1:hello from outside

> Send Event2()

SubState1::on_exit()

SubState2::on_entry()

> Send Event1()

SubState2::on_exit()

State1::on_exit()

State2::on_entry()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值