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