Skip to content

Instantly share code, notes, and snippets.

@tkemp
Created October 10, 2012 16:07
Show Gist options
  • Save tkemp/3866595 to your computer and use it in GitHub Desktop.
Save tkemp/3866595 to your computer and use it in GitHub Desktop.
Simple C++ State machine header
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