View difference between Paste ID: Kba8Fn13 and Xb0F9eg8
SHOW: | | - or go back to the newest paste.
1
#include <cstdlib>
2
#include <iostream>
3
4
#include "gtest/gtest.h"
5
#include "gmock/gmock.h"
6
7
#define STATE_AMOUNT 5
8-
enum State {
8+
9
using namespace std;
10
11
enum State 
12
{
13
	Off,
14
	Stopped,
15
	Walking,
16
	Running,
17
	Error
18-
class state_interface {			/* Abstract Class */
18+
19
20
/** The interface of the different states */
21
22
class state_interface					/* Abstract Class */
23
{
24
	public:
25
	virtual State turnOn () = 0;
26
	virtual State turnOff () = 0;
27-
class off_State: public state_interface {
27+
28
	virtual State speedDown () = 0;
29
};
30
31
/** The different states */
32
class off_State: public state_interface 
33
{
34
	public:
35
	State turnOn () {
36
		cout << "Off --[turnOn]--> Stopped \n";
37
		return Stopped;
38-
		cout << "Off --[speedUp]--> Off \n";
38+
39
	State turnOff () {
40
		cout << "Off --[turnOff]--> Off \n";
41
		return Off;
42-
		cout << "Off --[speedDown]--> Off \n";
42+
43
	State speedUp () {
44
		cout << "Off --[speedUp]--> Error \n";
45
		return Error;
46
	};
47-
class stopped_State: public state_interface {
47+
48
		cout << "Off --[speedDown]--> Error \n";
49
		return Error;
50-
		cout << "Stopped --[turnOn]--> Stopped \n";
50+
51
};
52
53
class stopped_State: public state_interface 
54
{
55
	public:
56
	State turnOn () {
57
		cout << "Stopped --[turnOn]--> Error \n";
58
		return Stopped;
59
	};
60
	State turnOff () {
61
		cout << "Stopped --[turnOff]--> Off \n";
62
		return Off;
63
	};
64
	State speedUp () {
65
		cout << "Stopped --[speedUp]--> Walking \n";
66
		return Walking;
67-
class walking_State: public state_interface {
67+
68
	State speedDown () {
69
		cout << "Stopped --[speedDown]--> Error \n";
70
		return Error;
71
	};
72
};
73
74
class walking_State: public state_interface 
75
{
76
	public:
77
	State turnOn () {
78
		cout << "Walking --[turnOn]--> Walking \n";
79
		return Walking;
80
	};
81
	State turnOff () {
82
		cout << "Walking --[turnOff]--> Off \n";
83
		return Off;
84
	};
85
	State speedUp () {
86
		cout << "Walking --[speedUp]--> Running \n";
87-
class running_State: public state_interface {
87+
88
	};
89
	State speedDown () {
90
		cout << "Walking --[speedDown]--> Stopped \n";
91
		return Stopped;
92
	};
93
};
94
95
class running_State: public state_interface 
96
{
97
	public:
98
	State turnOn () {
99
		cout << "Running --[turnOn]--> Running \n";
100
		return Running;
101
	};
102
	State turnOff () {
103
		cout << "Running --[turnOff]--> Off \n";
104
		return Off;
105
	};
106
	State speedUp () {
107-
class error_State: public state_interface {
107+
108
		return Error;
109
	};
110
	State speedDown () {
111
		cout << "Running --[speedDown]--> Walking \n";
112
		return Walking;
113
	};
114
};
115
116
class error_State: public state_interface 
117
{
118
	public:
119
	State turnOn () {
120
		cout << "Error --[turnOn]--> Error \n";
121
		return Error;
122
	};
123
	State turnOff () {
124
		cout << "Error --[turnOff]--> Off \n";
125
		return Off;
126
	};
127
	State speedUp () {
128-
class FSM {			/* Singleton */
128+
129
		return Error;
130
	};
131
	State speedDown () {
132
		cout << "Error --[speedDown]--> Error \n";
133-
	void initialize () {
133+
134
	};
135
};
136
137
138
class FSM						/* Singleton */
139
{
140
	private:
141
	state_interface * current;
142-
	void cleanUp () {
142+
143
	public:
144-
		i = 0;
144+
	FSM() 
145-
		for (i; i<STATE_AMOUNT; i++) {
145+
	{
146
		states[Off] = new (off_State);
147
		states[Stopped] = new (stopped_State);
148
		states[Walking] = new (walking_State);
149
		states[Running] = new (running_State);
150
		states[Error] = new (error_State);
151
		current = states[Off];
152-
	void live () {
152+
153
	
154
	void cleanUp () 
155-
		while (input != 4) {
155+
	{
156
		int i;
157
158
		for (i=0; i<STATE_AMOUNT; i++) 
159-
			switch(input){
159+
		{
160
			delete (states[i]);
161
			states[i] = 0;
162
		}
163
		current = 0;
164
	};
165
	
166
	void live () 
167
	{
168
		int input = 0;
169
	
170
		while (input != 4) 
171
		{
172
			cout << "Input stimulus: 0_On, 1_Off, 2_SpUp, 3_SpDn, 4_Exit \n";
173
			cin >> input;
174
175
			switch(input)
176
			{
177
				State new_state;
178
				case(0):
179
					new_state = current -> turnOn();
180
					current = states[new_state];
181
					break;
182
				case(1):
183
					new_state = current -> turnOff();
184
					current = states[new_state];
185
					break;
186-
int main (void) {
186+
187
					new_state = current -> speedUp();
188-
	FSM * the_machine;
188+
189
					break;
190-
	the_machine = new (FSM);
190+
191
					new_state = current -> speedDown();
192-
	the_machine -> initialize();
192+
193-
	the_machine -> live();
193+
194-
	the_machine -> cleanUp();
194+
				case(4):
195
					cleanUp ();
196-
	delete (the_machine);
196+
197
				default:
198-
	return 0;
198+
199
			}
200
			
201
		}
202
	};
203
};
204
205
int main(int argc, char** argv) 
206
{
207
	FSM the_machine;
208
	the_machine.live();
209
	return EXIT_SUCCESS;
210
}