-
Notifications
You must be signed in to change notification settings - Fork 16
Features
hsmcpp allows to use hierarchical state machine (HSM) in your project without worrying about the mechanism itself and instead focus on the structure and logic. I will not cover basics of HSM and instead will focus on how to use the library. You can familiarize yourself with HSM concept and terminology here:
- Introduction to Hierarchical State Machines
- Wikipedia: UML state machine
- Hierarchical Finite State Machine for AI Acting Engine
Since Finite State Machines (FSM) are just a simple case of HSM, those could be defined too using hsmcpp.
Here is an example of a simple HSM which only contains states and transitions:
Events are defined as an enum:
enum class MyEvents
{
EVENT_1,
EVENT_2,
EVENT_3,
EVENT_4
};
They could be later used when registering transitions.
States are defined as an enum:
enum class MyStates
{
StateA,
StateB,
StateC
};
State callbacks are optional and include:
-
entering
- called right before changing a state
- transition is canceled if callback returns FALSE
-
state changed
- called when HSM already changed it's current state
-
exiting
- called for previous state before starting to transition to a new state
- transition is canceled if callback returns FALSE
Assuming we create HSM as a separate object, here are possible ways to register a state:
HierarchicalStateMachine<MyStates, MyEvents> hsm;
hsm.registerState(MyStates::StateA);
hsm.registerState(MyStates::StateA, this, &HandlerClass::on_state_changed_a);
hsm.registerState(MyStates::StateA, this, &HandlerClass::on_state_changed_a, &HandlerClass::on_entering_a);
hsm.registerState(MyStates::StateA, this, &HandlerClass::on_state_changed_a, &HandlerClass::on_entering_a, &HandlerClass::on_exiting_a);
hsm.registerState<HandlerClass>(MyStates::StateA, this, &HandlerClass::on_state_changed_a, nullptr, &HandlerClass::on_exiting_a);
Note that if you explicitly need to pass nullptr (as in the last example) you will need to provide class name as a template parameter.
TODO
TODO