Created
December 20, 2015 07:21
-
-
Save ahamez/c2ffd67edaef60216d7c to your computer and use it in GitHub Desktop.
Idea for simple FSM interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct s0 | |
{ | |
template <typename SM> | |
void | |
on_entry(SM&) | |
const | |
{} | |
template <typename SM> | |
void | |
on_exit(SM&) | |
const | |
{} | |
}; | |
struct s1{}; | |
struct s2{}; | |
struct evt0{}; | |
struct evt1{}; | |
struct evt2{}; | |
struct guard0 | |
{ | |
template <typename Event, typename SM> | |
bool | |
operator()(const Event&, const SM&) | |
const | |
{ | |
} | |
}; | |
struct guard1{}; | |
struct guard2{}; | |
struct context | |
{ | |
int x; | |
}; | |
struct action0 | |
{ | |
template <typename Event, typename SM> | |
bool | |
operator()(const context& cxt, &const Event&, const SM&) | |
const | |
{ | |
cxt.x = 1; | |
} | |
}; | |
struct action1 | |
{ | |
template <typename Event, typename SM> | |
bool | |
operator()(const context& cxt, &const Event&, const SM&) | |
const | |
{ | |
std::cout << cxt.x << '\n'; // "1" | |
} | |
}; | |
using mfsm; | |
struct state_machine_ | |
{ | |
// in a first step, to ease states numbering | |
using states = declare_states< | |
s0 | |
, s1 | |
, s2 | |
>; | |
// implicit and_ between guards | |
// actions are executed in order of declaration | |
using fsm = make_transition_table< | |
initial_state<s0> | |
, transition<from<s0>, to<s1>, on<evt0>> | |
, transition<from<s0>, to<s0>, on<evt1>, guard<guard0>, guard<guard1>> | |
, transition<from<s0>, to<s1>, on<evt1>, guard<guard2>, action_context<context>, | |
action<action0>, action<action1>> | |
>; | |
int data0; | |
int data1; | |
}; | |
using state_machine = make_fsm<state_machine_>; | |
int main () | |
{ | |
auto fsm = state_machine{}; | |
fsm.start(); | |
assert(fsm.process(evt2{}) == false); | |
assert(fsm.process(evt0{}) == true); | |
assert(fsm.current_state_id() == state_machine::get_state_id<s1>::value); | |
const auto& state = fsm.current_state<state0>(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment