Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <iostream>
- #define STATE_AMOUNT 5
- using namespace std;
- enum State {
- Off,
- Stopped,
- Walking,
- Running,
- Error
- };
- /** The interface of the different states */
- class state_interface { /* Abstract Class */
- public:
- virtual State turnOn () = 0;
- virtual State turnOff () = 0;
- virtual State speedUp () = 0;
- virtual State speedDown () = 0;
- };
- /** The different states */
- class off_State: public state_interface {
- public:
- State turnOn () {
- cout << "Off --[turnOn]--> Stopped \n";
- return Stopped;
- };
- State turnOff () {
- cout << "Off --[turnOff]--> Off \n";
- return Off;
- };
- State speedUp () {
- cout << "Off --[speedUp]--> Off \n";
- return Off;
- };
- State speedDown () {
- cout << "Off --[speedDown]--> Off \n";
- return Off;
- };
- };
- class stopped_State: public state_interface {
- public:
- State turnOn () {
- cout << "Stopped --[turnOn]--> Stopped \n";
- return Stopped;
- };
- State turnOff () {
- cout << "Stopped --[turnOff]--> Off \n";
- return Off;
- };
- State speedUp () {
- cout << "Stopped --[speedUp]--> Walking \n";
- return Walking;
- };
- State speedDown () {
- cout << "Stopped --[speedDown]--> Error \n";
- return Error;
- };
- };
- class walking_State: public state_interface {
- public:
- State turnOn () {
- cout << "Walking --[turnOn]--> Walking \n";
- return Walking;
- };
- State turnOff () {
- cout << "Walking --[turnOff]--> Off \n";
- return Off;
- };
- State speedUp () {
- cout << "Walking --[speedUp]--> Running \n";
- return Running;
- };
- State speedDown () {
- cout << "Walking --[speedDown]--> Stopped \n";
- return Stopped;
- };
- };
- class running_State: public state_interface {
- public:
- State turnOn () {
- cout << "Running --[turnOn]--> Running \n";
- return Running;
- };
- State turnOff () {
- cout << "Running --[turnOff]--> Off \n";
- return Off;
- };
- State speedUp () {
- cout << "Running --[speedUp]--> Error \n";
- return Error;
- };
- State speedDown () {
- cout << "Running --[speedDown]--> Walking \n";
- return Walking;
- };
- };
- class error_State: public state_interface {
- public:
- State turnOn () {
- cout << "Error --[turnOn]--> Error \n";
- return Error;
- };
- State turnOff () {
- cout << "Error --[turnOff]--> Off \n";
- return Off;
- };
- State speedUp () {
- cout << "Error --[speedUp]--> Error \n";
- return Error;
- };
- State speedDown () {
- cout << "Error --[speedDown]--> Error \n";
- return Error;
- };
- };
- class FSM { /* Singleton */
- private:
- state_interface * current;
- state_interface * states [STATE_AMOUNT];
- public:
- void initialize () {
- states[Off] = new (off_State);
- states[Stopped] = new (stopped_State);
- states[Walking] = new (walking_State);
- states[Running] = new (running_State);
- states[Error] = new (error_State);
- current = states[Off];
- };
- void cleanUp () {
- int i;
- i = 0;
- for (i; i<STATE_AMOUNT; i++) {
- delete (states[i]);
- states[i] = 0;
- }
- current = 0;
- };
- void live () {
- int input = 0;
- while (input != 4) {
- cout << "Input stimulus: 0_On, 1_Off, 2_SpUp, 3_SpDn, 4_Exit \n";
- cin >> input;
- switch(input){
- State new_state;
- case(0):
- new_state = current -> turnOn();
- current = states[new_state];
- break;
- case(1):
- new_state = current -> turnOff();
- current = states[new_state];
- break;
- case(2):
- new_state = current -> speedUp();
- current = states[new_state];
- break;
- case(3):
- new_state = current -> speedDown();
- current = states[new_state];
- break;
- default:
- break;
- }
- }
- };
- };
- int main (void) {
- FSM * the_machine;
- the_machine = new (FSM);
- the_machine -> initialize();
- the_machine -> live();
- the_machine -> cleanUp();
- delete (the_machine);
- return 0;
- }
Add Comment
Please, Sign In to add comment