What is the best data structure to implement FSM in C++. The FSM should have below attributes:-
- Capability to register new states and transition at run time
- Capability to register callbacks for transitions.
- State and Input must be generic i.e. template based.
transition example :- Transition(‘State-A’, ‘State-B’, “input-1”, callback-1); Machine will move from State-A to State-B if input-1 has been passed and callback-1 will get called.
My Approach :-
- Create a base class CState and have a pure virtual function in that i.e. GetHashCode() = 0;
- Create a base class CInput and have a pure virtual function in that i.e. GetHashCode() = 0;
- Maintain a hashtable for keeping states. key will be Hashcode returned from CState::GetHashCode function, and value will be the entire object of CState class.
- Create another hashtable for maintaining transactions. It will have composite key made up of hascode of CState and CInput class, and value will be HasCode of new state for the machine and callback function pointer.
pl advise if there is any flaw with the above design.
Aucun commentaire:
Enregistrer un commentaire