我真的是“提升msm”的新手,现在我遇到一个问题,如何在boost msm中使用boost信号,我已经尝试了很多次,但是没有用。
我想要实现的是当机器更改其状态时,操作员发送信号,我的外部实体捕获该信号并执行其他功能,但是现在我无法将信号发送出机器,我可以不要将信号与外部实体连接。 有例子吗?
答案 0 :(得分:0)
您可以结合使用Boost.Signals2和Boost.MSM。
这是简单的signal2示例。
https://wandbox.org/permlink/XZzGIIVWXjvOPzdd(正在运行的演示)
#include <iostream>
#include <string>
#include <boost/signals2/signal.hpp>
#include <iostream>
// begin -- signals2 code
struct signal_data {
int i;
std::string str;
};
struct sender {
boost::signals2::signal<void(signal_data const&)> foo;
void send() {
foo(signal_data {42, "ABC"} );
}
};
struct receiver_1 {
void on_foo(signal_data const& sd) {
std::cout << __PRETTY_FUNCTION__ << " " << sd.i << ", " << sd.str << std::endl;
}
};
struct receiver_2 {
void on_foo(signal_data const& sd) {
std::cout << __PRETTY_FUNCTION__ << " " << sd.i << ", " << sd.str << std::endl;
}
};
// end -- signals2 code
int main() {
// signals setup ----------------------------------------
// external entities
receiver_1 r1;
receiver_2 r2;
sender s;
// make connection
// using lambda expression
s.foo.connect(
[&] (signal_data const& param) {
r1.on_foo(param);
}
);
// or bind
s.foo.connect(std::bind(&receiver_2::on_foo, &r2, std::placeholders::_1));
s.send();
}
信号发送点为s.send()
。如果我们可以从msm的操作中调用s.send()
,那么目标就可以实现。
这是简单的msm示例。
https://wandbox.org/permlink/tnRSQ07anNe49GpO(正在运行的演示)
#include <iostream>
#include <string>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
// begin -- msm code
namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;
// ----- Events
struct event1 {};
// ----- State machine
struct sm1_:msmf::state_machine_def<sm1_> {
// States
struct state1:msmf::state<> {};
// Set initial state
using initial_state = state1;
// Actions
struct action {
template <class Event, class Fsm, class SourceState, class TargetState>
void operator()(Event const&, Fsm&, SourceState&, TargetState&) const {
std::cout << "action()" << std::endl;
// want to call s.send() here
}
};
// Transition table
struct transition_table:mpl::vector<
// Start Event Next Action Guard
msmf::Row < state1, event1, msmf::none, action, msmf::none >
> {};
};
// Pick a back-end
using sm1 = msm::back::state_machine<sm1_>;
// end -- msm code
int main() {
sm1 s1;
s1.start();
std::cout << "Send event1" << std::endl;
s1.process_event(event1());
}
动作定义如下:
struct action {
template <class Event, class Fsm, class SourceState, class TargetState>
void operator()(Event const&, Fsm&, SourceState&, TargetState&) const {
std::cout << "action()" << std::endl;
// want to call s.send() here
}
};
如何在操作中致电s.send()
?
首先,将发送者的引用添加为sm1_的成员变量。
sender& s; // define sender reference as a member variable
然后,在构造函数中对其进行初始化。
sm1_(sender& s):s(s) {} // initialize sender in constructor
然后,在创建状态机时传递std::ref
包装的发件人。
sm1 s1(std::ref(s)); // pass sender as reference
最后,在操作中调用s.send()
。您可以通过s
参考访问Fsm
,如下所示:
struct action {
template <class Event, class Fsm, class SourceState, class TargetState>
void operator()(Event const&, Fsm& f, SourceState&, TargetState&) const {
std::cout << "action()" << std::endl;
f.s.send();
}
};
这是完整的代码:
https://wandbox.org/permlink/gh83EW8eado5iOi8(正在运行的演示)
#include <iostream>
#include <string>
#include <boost/signals2/signal.hpp>
#include <iostream>
#include <boost/msm/back/state_machine.hpp>
#include <boost/msm/front/state_machine_def.hpp>
#include <boost/msm/front/functor_row.hpp>
// begin -- signals2 code
struct signal_data {
int i;
std::string str;
};
struct sender {
boost::signals2::signal<void(signal_data const&)> foo;
void send() {
foo(signal_data {42, "ABC"} );
}
};
struct receiver_1 {
void on_foo(signal_data const& sd) {
std::cout << __PRETTY_FUNCTION__ << " " << sd.i << ", " << sd.str << std::endl;
}
};
struct receiver_2 {
void on_foo(signal_data const& sd) {
std::cout << __PRETTY_FUNCTION__ << " " << sd.i << ", " << sd.str << std::endl;
}
};
// end -- signals2 code
// begin -- msm code
namespace msm = boost::msm;
namespace msmf = boost::msm::front;
namespace mpl = boost::mpl;
// ----- Events
struct event1 {};
// ----- State machine
struct sm1_:msmf::state_machine_def<sm1_> {
sm1_(sender& s):s(s) {} // initialize sender in constructor
// States
struct state1:msmf::state<> {};
// Set initial state
using initial_state = state1;
// Actions
struct action {
template <class Event, class Fsm, class SourceState, class TargetState>
void operator()(Event const&, Fsm& f, SourceState&, TargetState&) const {
std::cout << "action()" << std::endl;
f.s.send();
}
};
// Transition table
struct transition_table:mpl::vector<
// Start Event Next Action Guard
msmf::Row < state1, event1, msmf::none, action, msmf::none >
> {};
sender& s; // define sender reference as a member variable
};
// Pick a back-end
using sm1 = msm::back::state_machine<sm1_>;
// end -- msm code
int main() {
// signals setup ----------------------------------------
// external entities
receiver_1 r1;
receiver_2 r2;
sender s;
// make connection
// using lambda expression
s.foo.connect(
[&] (signal_data const& param) {
r1.on_foo(param);
}
);
// or bind
s.foo.connect(std::bind(&receiver_2::on_foo, &r2, std::placeholders::_1));
// msm setup and process ---------------------------------
sm1 s1(std::ref(s)); // pass sender as reference
s1.start();
std::cout << "Send event1" << std::endl;
s1.process_event(event1());
}