Interface IStateMachine
Defines the contract for a state machine that manages transitions and access to states within a state-driven system.
Namespace: Fwt.Core.StateMachines
Assembly: fwt.core.dll
Syntax
public interface IStateMachine
Remarks
Implementations of this interface allow querying the current state, retrieving states by index or type, and switching between states, optionally with context. This interface is typically used in scenarios where behavior changes dynamically based on the current state, such as workflow engines, game logic, or protocol handlers. Thread safety and state persistence are implementation-specific and should be considered when using concrete state machine types.
Methods
GetState(int)
Retrieves the state at the specified index within the state collection.
Declaration
ISmState GetState(int stateIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | stateIndex | The zero-based index of the state to retrieve. Must be greater than or equal to 0 and less than the total number of states. |
Returns
| Type | Description |
|---|---|
| ISmState | The state at the specified index if it exists; otherwise, |
Remarks
If the state collection is null or the specified index is out of range, the
method returns null.
GetState(Type)
Retrieves the state instance associated with the specified type.
Declaration
ISmState GetState(Type type)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type for which to retrieve the corresponding state. Cannot be null. |
Returns
| Type | Description |
|---|---|
| ISmState | The state instance associated with the specified type, or |
GetState<TState>(Type)
Retrieves the state object of the specified type and attempts to cast it to the requested state interface.
Declaration
TState GetState<TState>(Type type) where TState : ISmState
Parameters
| Type | Name | Description |
|---|---|---|
| Type | type | The type of the state object to retrieve. |
Returns
| Type | Description |
|---|---|
| TState | An instance of |
Type Parameters
| Name | Description |
|---|---|
| TState | The state interface type to return. Must implement ISmState. |
SwitchState(ISmState)
Transitions the state machine to the specified new state.
Declaration
void SwitchState(ISmState newState)
Parameters
| Type | Name | Description |
|---|---|---|
| ISmState | newState | The state to switch to. If |
Remarks
This method prepares the current state to stop before switching, and prepares the new state to run after the transition. If the new state is null, the state machine will have no active state after the call.
SwitchState(int)
Transitions the state machine to the state at the specified index.
Declaration
ISmState SwitchState(int stateIndex)
Parameters
| Type | Name | Description |
|---|---|---|
| int | stateIndex | The zero-based index of the state to switch to. Must correspond to a valid state within the state machine. |
Returns
| Type | Description |
|---|---|
| ISmState | The state object representing the new active state after the transition. |
SwitchState<TState>()
Switches the state machine to the specified state type and returns the new state instance if successful.
Declaration
TState SwitchState<TState>() where TState : ISmState
Returns
| Type | Description |
|---|---|
| TState | An instance of |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of state to switch to. Must implement ISmState. |
Remarks
If the requested state type is not available or cannot be switched to, the method
returns the default value for TState. This method is typically used to transition the
state machine to a specific state and obtain a strongly-typed reference to it.
SwitchState<TState, TContext>(TState, TContext)
Transitions the state machine to the specified new state, providing the associated context for the state.
Declaration
void SwitchState<TState, TContext>(TState newState, TContext context) where TState : ISmStateWithContext<TContext>
Parameters
| Type | Name | Description |
|---|---|---|
| TState | newState | The state to transition to. If equal to the current state, no transition occurs. |
| TContext | context | The context object to associate with the new state. This is set on the state before it is started. |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of the state to transition to. Must implement ISmStateWithContext<TContext>. |
| TContext | The type of the context object to be associated with the new state. |
Remarks
If the current state is not null, it is prepared to stop before the transition. The new state's context is set before it is prepared to run. No action is taken if the new state is the same as the current state.
SwitchState<TState, TContext>(TContext)
Declaration
TState SwitchState<TState, TContext>(TContext context) where TState : ISmStateWithContext<TContext>
Parameters
| Type | Name | Description |
|---|---|---|
| TContext | context |
Returns
| Type | Description |
|---|---|
| TState |
Type Parameters
| Name | Description |
|---|---|
| TState | |
| TContext |
Tick()
Advances the state machine by invoking the tick operation on the current state, if one is set.
Declaration
void Tick()
Remarks
Call this method periodically to update the state machine's behavior. If no current state is assigned, this method has no effect.