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