pad.mattdiesel.co.uk

Snippet - FSM Implementation C++ 2

FSM Implementation C++ 2 (C++)


Created 2015-02-20 15:46:22.888730 by Matt.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>


struct fsmOne
{
	int state = 0;

	bool operator()() {
		std::cout << "S1." << state << std::endl;

		switch (state) {
			case 0:
				state = 1;
				break;
			case 1:
				state = 5;
				break;
			case 5:
				return( true );
		}

		return( false );
	}
};

struct fsmTwo
{
	int state = 0;
	fsmOne one;

	bool operator()() {
		std::cout << "S2." << state << std::endl;

		switch (state) {
			case 0:
				state = 1;
				one.state = 0;
				break;
			case 1:
				if (one()) {
					state = 5;
				}
				break;
			case 5:
				return( true );
		}

		return( false );
	}
};


int main(int argc, char const *argv[])
{
	fsmTwo two;

	while (!two()) {}

	return 0;
}