Created
October 10, 2012 16:07
-
-
Save tkemp/3866595 to your computer and use it in GitHub Desktop.
Simple C++ State machine header
This file contains hidden or 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
class MyClass; | |
typedef enum SMState { | |
SMStateStart, | |
SMStateOne, | |
SMStateTwo, | |
SMStateFinal, | |
SMStateAny | |
} SMState; | |
typedef enum SMEvent { | |
SMEventAlpha, | |
SMEventBravo, | |
SMEventCharlie, | |
SMEventNoEvent | |
} SMEvent; | |
typedef SMState (MyClass::*SMTransitionFunc)(); | |
typedef struct SMTransition { | |
SMState state; | |
SMEvent event; | |
SMTransitionFunc tFunc; | |
} SMTransition; | |
class MyClass { | |
public: | |
// Events - separate external and internal for clarity | |
void recEventA(); | |
void recEventB(); | |
void recEventC(); | |
// Tell the world where we are right now | |
SMState getState() { return curState; }; | |
private: | |
// Transition functions | |
SMState transToStart(); | |
SMState transToOne(); | |
SMState transToTwo(); | |
SMState transToFinal(); | |
SMState transDoNothing(); | |
// Transition table | |
static SMTransition transitionTable[]; | |
SMState curState; | |
SMEvent nextEvent; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment