C++ Boost状态机语言--`action中的`post`事件`
我已经使用了几天,现在我需要在一个动作中发布/处理一个事件。 我可以从转换表中直接看到: 使用名称空间sml; 返回make_transition_表 *s1_s+事件/过程_事件其他_事件{}=s2_s, s2_s+事件=X ; 但我的用例不同: 我有一个状态,它有一个actionfor on entry事件,它执行一些工作,并最终从状态操作中触发一个事件 例如,使用boost statechart,每个状态都可以访问上下文并可以发布事件C++ Boost状态机语言--`action中的`post`事件`,c++,boost,state-machine,C++,Boost,State Machine,我已经使用了几天,现在我需要在一个动作中发布/处理一个事件。 我可以从转换表中直接看到: 使用名称空间sml; 返回make_transition_表 *s1_s+事件/过程_事件其他_事件{}=s2_s, s2_s+事件=X ; 但我的用例不同: 我有一个状态,它有一个actionfor on entry事件,它执行一些工作,并最终从状态操作中触发一个事件 例如,使用boost statechart,每个状态都可以访问上下文并可以发布事件 这在sml中是可能的吗?这是可能的。你需要做两件事。
这在sml中是可能的吗?这是可能的。你需要做两件事。 一种是将boost::sml::queue模板参数设置为boost::sml::sm 参数进程是可调用的。所以您可以将进程作为函数调用。调用processevent时,事件将被发布到队列中 下面是一个示例代码:
#include <iostream>
#include <queue>
#include <boost/sml.hpp>
int main() {
namespace sml = boost::sml;
struct my_event {};
struct other_event {};
struct table {
auto operator()() const noexcept {
using namespace sml;
return make_transition_table(
*"s1"_s + event<my_event> /
// In order to post event in the action,
// you need to define the action handler as follows.
// The key is the second parameter.
// `sml::back::process` is posting event callable type.
// You need to pass all event types that are posted in the action handler
// as the template argument.
// Unfortunately you cannot write `sml::back::process<_>` for all events.
[](auto const& /*ev*/, sml::back::process<other_event /*, my_event*/ > process) {
std::cout << "before post" << std::endl;
process(other_event{});
std::cout << "after post" << std::endl;
} = "s2"_s,
"s2"_s + event<other_event> = X,
// entry exit log
"s1"_s + sml::on_entry<_> / [] { std::cout << "s1 on entry" << std::endl; },
"s1"_s + sml::on_exit<_> / [] { std::cout << "s1 on exit" << std::endl; },
"s2"_s + sml::on_entry<_> / [] { std::cout << "s2 on entry" << std::endl; },
"s2"_s + sml::on_exit<_> / [] { std::cout << "s2 on exit" << std::endl; }
);
};
};
// sml::process_queue is for post event
sml::sm<table, sml::process_queue<std::queue>> sm;
sm.process_event(my_event{});
}
现场演示:这是可能的。你需要做两件事。 一种是将boost::sml::queue模板参数设置为boost::sml::sm 参数进程是可调用的。所以您可以将进程作为函数调用。调用processevent时,事件将被发布到队列中 下面是一个示例代码:
#include <iostream>
#include <queue>
#include <boost/sml.hpp>
int main() {
namespace sml = boost::sml;
struct my_event {};
struct other_event {};
struct table {
auto operator()() const noexcept {
using namespace sml;
return make_transition_table(
*"s1"_s + event<my_event> /
// In order to post event in the action,
// you need to define the action handler as follows.
// The key is the second parameter.
// `sml::back::process` is posting event callable type.
// You need to pass all event types that are posted in the action handler
// as the template argument.
// Unfortunately you cannot write `sml::back::process<_>` for all events.
[](auto const& /*ev*/, sml::back::process<other_event /*, my_event*/ > process) {
std::cout << "before post" << std::endl;
process(other_event{});
std::cout << "after post" << std::endl;
} = "s2"_s,
"s2"_s + event<other_event> = X,
// entry exit log
"s1"_s + sml::on_entry<_> / [] { std::cout << "s1 on entry" << std::endl; },
"s1"_s + sml::on_exit<_> / [] { std::cout << "s1 on exit" << std::endl; },
"s2"_s + sml::on_entry<_> / [] { std::cout << "s2 on entry" << std::endl; },
"s2"_s + sml::on_exit<_> / [] { std::cout << "s2 on exit" << std::endl; }
);
};
};
// sml::process_queue is for post event
sml::sm<table, sml::process_queue<std::queue>> sm;
sm.process_event(my_event{});
}
现场演示:
#include <iostream>
#include <queue>
#include <boost/sml.hpp>
int main() {
namespace sml = boost::sml;
struct my_event {};
struct other_event {};
struct table {
auto operator()() const noexcept {
using namespace sml;
return make_transition_table(
*"s1"_s + event<my_event> /
// In order to post event in the action,
// you need to define the action handler as follows.
// The key is the second parameter.
// `sml::back::process` is posting event callable type.
// You need to pass all event types that are posted in the action handler
// as the template argument.
// Unfortunately you cannot write `sml::back::process<_>` for all events.
[](auto const& /*ev*/, sml::back::process<other_event /*, my_event*/ > process) {
std::cout << "before post" << std::endl;
process(other_event{});
std::cout << "after post" << std::endl;
} = "s2"_s,
"s2"_s + event<other_event> = X,
// entry exit log
"s1"_s + sml::on_entry<_> / [] { std::cout << "s1 on entry" << std::endl; },
"s1"_s + sml::on_exit<_> / [] { std::cout << "s1 on exit" << std::endl; },
"s2"_s + sml::on_entry<_> / [] { std::cout << "s2 on entry" << std::endl; },
"s2"_s + sml::on_exit<_> / [] { std::cout << "s2 on exit" << std::endl; }
);
};
};
// sml::process_queue is for post event
sml::sm<table, sml::process_queue<std::queue>> sm;
sm.process_event(my_event{});
}