cuestate
所属分类:collect
开发工具:C++
文件大小:0KB
下载次数:0
上传日期:2020-12-22 12:41:09
上 传 者:
sh-1993
说明: C++模板元编程FSM,
(C++ template metaprogramming FSM,)
文件列表:
LICENSE (11357, 2020-12-22)
cuestate.hpp (9080, 2020-12-22)
main.cpp (2949, 2020-12-22)
# cuestate
通过状态表生成状态机代码。`需要依赖支持C++20的编译器`。
```cpp
#include
#include
#include "cuestate.hpp"
using namespace cue::state;
// states
struct closed {};
struct opened {};
struct walking {};
// events
struct close {};
struct open {};
struct walk {
bool is_ready_;
int distance_;
};
struct stop {};
// actions
const auto do_open = [](https://github.com/xcyl/cuestate/blob/master/const open&) { std::cout << "open" << std::endl; };
const auto do_close = [](https://github.com/xcyl/cuestate/blob/master/const close&) { std::cout << "close" << std::endl; };
const auto do_stop = [](https://github.com/xcyl/cuestate/blob/master/const stop&) { std::cout << "stop" << std::endl; };
const auto do_walk = [](https://github.com/xcyl/cuestate/blob/master/const walk& w) { std::cout << "walking " << w.distance_ << "m" << std::endl; };
// guards
const auto is_ready = [](https://github.com/xcyl/cuestate/blob/master/const walk& w) {
std::cout << "robot is ready" << std::endl;
return w.is_ready_;
};
struct light_robot {
using initial_state = closed;
using transition_table = table<
// +---------+----------+----------+---------------+---------------+
// | current | event | target | action |guard(optional)|
// +---------+----------+----------+---------------+---------------+
transition< closed , open , opened , do_open >,
transition< opened , close , closed , do_close >,
transition< opened , walk , walking , do_walk , is_ready >,
transition< walking , stop , opened , do_stop >,
transition< walking , close , closed , do_close >
>;
};
int main(int argc, char** argv) {
machine robot;
assert(robot.is(closed{}));
robot.on(open{});
assert(robot.is(opened{}));
robot.on(close{});
assert(robot.is(closed{}));
robot.on(open{});
assert(robot.is(opened{}));
robot.on(walk{.is_ready_ = true, .distance_ = 5});
assert(robot.is(walking{}));
robot.on(stop{});
assert(robot.is(opened{}));
robot.on(close{});
assert(robot.is(closed{}));
robot.on(stop{});
assert(robot.is(closed{}));
return 0;
}
```
近期下载者:
相关文件:
收藏者: