-
Notifications
You must be signed in to change notification settings - Fork 1
HierarchicalStateMachine isInState()
Checks if the state machine is currently in the specified state.
template<class TContext>
bool isInState(HierarchicalStateMachine<TContext>::State& state) const-
TContext: The type of the context, which holds data or behavior relevant to the state machine.
-
state: The state to be compared.
true if the state is the current state, otherwise false.
This function allows querying whether the current state matches the provided state.
Highly useful in hierarchical state machines, if you only want to execute a part of your code on that certain callback.
struct FooContext
{
static HierarchicalStateMachine<FooContext>::State state1;
static HierarchicalStateMachine<FooContext>::State state2;
static HierarchicalStateMachine<FooContext>::State state3;
int count = 0;
HierarchicalStateMachine<FooContext>* hsm = nullptr;
};
auto ctx = FooContext();
auto hsm = HierarchicalStateMachine<FooContext>(&ctx, true);
ctx.hsm = &hsm;
HierarchicalStateMachine<FooContext>::State FooContext::state1(nullptr, state1_enter, state1_update, state1_exit);
HierarchicalStateMachine<FooContext>::State FooContext::state2(&FooContext::state1, state2_enter, state2_update, nullptr);
HierarchicalStateMachine<FooContext>::State FooContext::state3(&FooContext::state1, nullptr, state3_update, state3_exit);
...
static void state1_enter(FooContext* const ctx)
{
ctx->count = 0;
// prints state1 enter
if (ctx->hsm->isInState(FooContext::state1))
{
// prints state1 is in state (enter)
}
}
static void state2_enter(FooContext* const ctx)
{
// prints state2 enter
if (ctx->hsm->isInState(FooContext::state2))
{
// prints state2 is in state (enter)
}
}
static void state1_update(FooContext* const ctx)
{
// prints state1 update
ctx->count++;
if (ctx->count == 2)
{
ctx->hsm->transitionTo(FooContext::state2);
}
if (ctx->hsm->isInState(FooContext::state1))
{
// prints state1 is in state (update)
}
}
static void state2_update(FooContext* const ctx)
{
// prints state2 update
if (ctx->hsm->isInState(FooContext::state2))
{
// prints state2 is in state (update)
}
}
static void state3_update(FooContext* const ctx)
{
// prints state3 update
if (ctx->hsm->isInState(FooContext::state3))
{
// prints state3 is in state (update)
ctx->hsm->transitionTo(FooContext::state1);
}
}
the above code will result to: (assuming S3 is the initial state)
S1 ENTER
S2 ENTER
S3 UPDATE
S3 IS IN STATE (UPDATE)
S3 EXIT
S2 EXIT
S1 EXIT
S1 ENTER
S1 IS IN STATE (ENTER)
S1 UPDATE
S1 IS IN STATE (UPDATE)
S1 UPDATE
S1 IS IN STATE (UPDATE)
S2 ENTER
S2 IS IN STATE (ENTER)
S1 UPDATE
S2 UPDATE
S2 IS IN STATE (UPDATE)
Common Types
→ StateStatus (enum)
→ StateCallback (alias)
Finite State Machine (FSM)
→ State
→ ctor()
→ isInState()
→ transitionTo()
→ update()
Hierarchical State Machine (HSM)
→ State
→ ctor()
→ isInState()
→ transitionTo()
→ update()
State Base
→ ctor()
→ is()
→ enter()
→ update()
→ exit()
State Machine Handler
→ ctor()
→ getNextState()
→ getActiveState()
→ isInState()
→ setNextState()
→ queueTransition()
→ beginTransitionQueue()
→ endTransitionQueue()
→ execute()