我试图了解该库,但对某些事件类型的高级概念感到困惑。我一直在这里阅读教程指南: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有时会发生。
最佳答案
现在这已经很老了,但是如果有人偶然发现了这个以寻找答案:
通用处理程序
在撰写本文时,在documentation中,您可以通过意外的事件处理程序来实现您想要的目标。这些将执行操作,您可以自由地转换到另一个状态,但是您不需要这样做。
_(下划线)
我非常不愿意透露任何信息,因为这完全是我自己的解释,我希望听到那些在代码上花费更多时间并且对代码理解得更好的人的看法。
根据我在上面的链接和实验中看到的内容,可以将其用于“匹配”尚未使用特定处理程序声明的所有事件。
进入/退出事件
#include <boost/sml.hpp>
#include <iostream>
struct state_machine {
public:
// Transition table
auto operator() () const {
using namespace boost::sml;
return make_transition_table(
*"state_a"_s + event<event_1> = "state_b"_s,
"state_a"_s + event<event_2> = "state_b"_s,
"state_b"_s + on_entry<event_1> / ActionOne{},
"state_b"_s + on_entry<_> / ActionTwo{}
);
}
// Events
struct event_1 {};
struct event_2 {};
// Actions
struct ActionOne {
void operator()() {
std::cout << "Transition due to event_1" << std::endl;
};
};
struct ActionTwo {
void operator()() {
std::cout << "Transition due to event_2" << std::endl;
};
};
};
int main () {
boost::sml::sm<state_machine> fsm_one, fsm_two;
// Will invoke ActionOne
fsm_one.process_event(state_machine::event_1{});
// Will invoke ActionTwo
fsm_two.process_event(state_machine::event_2{});
return 0;
}
意外事件
我相信上面文档的链接中的代码已经很清楚了,我不需要发布一个有效的示例。
关于c++ - C++ Boost SML库,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50166843/