Last active
April 17, 2021 19:09
-
-
Save eNV25/c73f0327863bb31dbb0d11569690cff2 to your computer and use it in GitHub Desktop.
State machine where each state is represented by a function.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "state_machine.h" | |
#include <iostream> | |
using namespace std; | |
struct StateFn { | |
State f; | |
StateFn(State p) : f(p) {} | |
operator State(void) { return f; } | |
}; | |
using State = StateFn (*)(void); | |
StateFn EOF_state(void) { | |
cout << "EOF State" << endl; | |
return nullptr; | |
} | |
StateFn other_state(void) { | |
cout << "Other State" << endl; | |
return EOF_state; | |
} | |
StateFn normal_state(void) { | |
cout << "Normal State" << endl; | |
return other_state; | |
} | |
int main(void) { | |
State state; | |
state = normal_state(); | |
while (state != nullptr) { | |
state = state(); | |
} | |
cout << "FIN" << endl; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _STATE_MACHINE_H_ | |
#define _STATE_MACHINE_H_ | |
#pragma once | |
struct StateFn; | |
using State = StateFn (*)(void); | |
StateFn EOF_state(void); | |
StateFn other_state(void); | |
StateFn normal_state(void); | |
#endif // ! _STATE_MACHINE_H_ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment