C++ C++;boostSML库

C++ C++;boostSML库,c++,boost,state-machine,C++,Boost,State Machine,我试图理解这个库,但我对某些事件类型的高级概念感到困惑。我一直在这里阅读教程指南:。它经常使用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

我试图理解这个库,但我对某些事件类型的高级概念感到困惑。我一直在这里阅读教程指南:。它经常使用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.
struct{};//我不知道怎么用这个
模板
_entry上的struct:internal_event,entry_exit{//在实际事件之前运行的设置函数
// ...
模板
_exit上的struct:internal_事件,entry_exit{//将在事件完成后运行。
// ...
struct anonymous:internal_event{//不确定,我认为这适用于您尚未定义的任何未知类型。
我的最终目标是,我希望能够有一个通用事件处理程序。src_状态可能有一个针对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));
}
#包括
#包括
#包括
名称空间sml=boost::sml;
名称空间{
结构e1{};
结构e2{};
结构e3{};
结构e4{};
结构转换{
自动运算符(){
使用名称空间sml;
返回make_transition_表(

*“idle”_s/[]{std::cout这现在已经很老了,但万一有人无意中发现了这一点,想寻找答案:

通用处理程序 在撰写本文时,可以通过意外事件处理程序实现您想要实现的目标。这些处理程序将执行操作,您可以自由地导致转换到另一个状态,但您不需要这样做

(下划线) 我很不愿意对这件事说任何话,因为这都是我自己的解释,我更愿意听那些花了更多时间在代码中并更好地理解它的人说

从上面的链接和我的实验中可以看出,它可以用来“匹配”所有没有用特定处理程序声明的事件

进入/退出事件

#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;
}
#包括
#包括
结构状态机{
公众:
//过渡表
自动运算符()()常量{
使用名称空间boost::sml;
返回make_transition_表(
*“状态a”\u s+event=“状态b”\u s,
“状态a”\u s+event=“状态b”\u s,
“state_b”_s+on_entry/ActionOne{},
“state_b”_s+on_entry/ActionTwo{}
);
}
//事件
结构事件_1{};
结构事件_2{};
//行动
结构ActionOne{
void运算符()(){

库特似乎是图书馆的一个疏忽