-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
#include <cassert> | ||
|
||
#include "fsm.h" | ||
|
||
#if __cplusplus >= 201703L | ||
|
||
int value = 0; | ||
|
||
// global actions | ||
|
||
void store(int i) { value = i; } | ||
void clear() { value = 0; } | ||
|
||
// global guards | ||
|
||
bool is1(int i) { return i == 1; } | ||
|
||
// fsm | ||
|
||
class state_machine: public fsmlite::fsm<state_machine> { | ||
friend class fsm; // base class needs access to transition_table | ||
|
||
public: | ||
enum states { Init, Running, Exit }; | ||
|
||
using event = int; | ||
|
||
public: | ||
|
||
static void store2() { value = 2; } | ||
|
||
static bool is2(int i) { return i == 2; } | ||
|
||
void store3() { value = 3; } | ||
|
||
bool is3(int i) const { return i == 3; } | ||
|
||
private: | ||
using m = state_machine; | ||
|
||
using transition_table = table< | ||
// Start Event Target Action Guard | ||
// ----+--------+------+--------+-----------+-------+- | ||
row< Init, event, Running, &store >, | ||
row< Running, event, Running, &store, &is1 >, | ||
row< Running, event, Running, &m::store2, &m::is2 >, | ||
row< Running, event, Running, &m::store3, &m::is3 >, | ||
row< Running, event, Exit, &clear >, | ||
row< Exit, event, Exit > | ||
// ----+--------+------+--------+-----------+-------+- | ||
>; | ||
}; | ||
|
||
#endif | ||
|
||
int main() | ||
{ | ||
#if __cplusplus >= 201703L | ||
state_machine m; | ||
assert(m.current_state() == state_machine::Init); | ||
assert(value == 0); | ||
|
||
m.process_event(42); | ||
assert(m.current_state() == state_machine::Running); | ||
assert(value == 42); | ||
|
||
m.process_event(1); | ||
assert(m.current_state() == state_machine::Running); | ||
assert(value == 1); | ||
|
||
m.process_event(2); | ||
assert(m.current_state() == state_machine::Running); | ||
assert(value == 2); | ||
|
||
m.process_event(3); | ||
assert(m.current_state() == state_machine::Running); | ||
assert(value == 3); | ||
|
||
m.process_event(42); | ||
assert(m.current_state() == state_machine::Exit); | ||
assert(value == 0); | ||
|
||
m.process_event(42); | ||
assert(m.current_state() == state_machine::Exit); | ||
assert(value == 0); | ||
#endif | ||
return 0; | ||
} |