【发布时间】:2018-05-04 03:46:23
【问题描述】:
我正在尝试了解该库,但我对某些事件类型的高级概念感到困惑。我一直在这里阅读教程指南:Boost Experimental Documentation.。经常使用on_exit、on_entry、_等类型,我看不懂。
struct _ {}; // I don't understand how to use this
template <class T, class TEvent = T>
struct on_entry : internal_event, entry_exit { // A setup function that runs before the actual event
// ...
template <class T, class TEvent = T>
struct on_exit : internal_event, entry_exit { // Will run after the event has completed.
// ...
struct anonymous : internal_event { // Not sure, I think this is for any unknown type that you have not defined.
我的最终目标是我希望能够拥有一个通用的事件处理程序。 src_state 可能具有针对 E1 的特定处理程序,但对于 E2、E3 等,我希望有一个通用处理程序。我有下面的代码来列出我想要发生的事情,但显然它不起作用。
#include <boost/sml.hpp>
#include <cassert>
#include <iostream>
namespace sml = boost::sml;
namespace {
struct e1 {};
struct e2 {};
struct e3 {};
struct e4 {};
struct transitions {
auto operator()() const noexcept {
using namespace sml;
return make_transition_table(
*"idle"_s / [] { std::cout << "anonymous transition" << std::endl; } = "s1"_s
, "s1"_s + event<e1> / [] { std::cout << "internal s1 transition" << std::endl; }
, "s1"_s + event<e2> / [] { std::cout << "self transition" << std::endl; } = "s2"_s
, "s1"_s + event<_> / [] { std::cout << "s1 Handle all other events here" << std::endl; } = "s1"_s
, "s2"_s + event<e2> / [] {std::cout << "internal s2 transition" << std::endl; }
, "s2"_s + event<_> / [] { std::cout << "s2 Handle all other events here" << std::endl; } = "s2"_s
, "s2"_s + event<e3> / [] { std::cout << "external transition" << std::endl; } = X
);
}
};
}
int main() {
sml::sm<transitions> sm;
sm.process_event(e1{}); // Basic
sm.process_event(e3{}); // The underscore should handle the event now...
sm.process_event(e2{}); // Transition to s2
sm.process_event(e1{}); // The _ should handle this.
sm.process_event(e4{}); // The _ should handle this.
sm.process_event(e3{}); // X
assert(sm.is(sml::X));
}
是否有可能为所有事件(包括预期和意外事件)提供通用事件处理程序。状态机确实期望 e1/e2/e3/e4 有时会发生。
【问题讨论】:
-
似乎是图书馆的疏忽
标签: c++ boost state-machine