Class SmStateMachine
Represents a state machine that manages and transitions between multiple states implementing the ISmState interface.
Inherited Members
Namespace: Fwt.Core.StateMachines
Assembly: fwt.core.dll
Syntax
public class SmStateMachine : SmState, ISmState, IStateMachine
Remarks
SmStateMachine provides mechanisms to add, retrieve, and switch between states, supporting both index-based and type-based access. The current state can be switched with or without context, and the state machine ensures that appropriate lifecycle methods are called during transitions. This class is intended for scenarios where stateful behavior needs to be managed and coordinated, such as workflow engines or game logic controllers. SmStateMachine is not thread-safe; external synchronization is required if accessed concurrently.
Properties
CurrentState
Gets or sets the current state of the state machine.
Declaration
public virtual ISmState CurrentState { get; set; }
Property Value
| Type | Description |
|---|---|
| ISmState |
Remarks
Changing this property updates the state machine's active state. The value should be set to a valid state instance that implements ISmState.
SortedStates
Gets or sets the collection of state objects, indexed by their type, in sorted order.
Declaration
public virtual Dictionary<Type, ISmState> SortedStates { get; set; }
Property Value
| Type | Description |
|---|---|
| Dictionary<Type, ISmState> |
Remarks
The dictionary maps each state type to its corresponding state instance. Modifying this collection affects the available states managed by the state machine.
States
Gets or sets the collection of states managed by the state machine.
Declaration
public virtual List<ISmState> States { get; set; }
Property Value
| Type | Description |
|---|---|
| List<ISmState> |
Remarks
The order of states in the collection may affect state transitions, depending on the implementation. Modifying this collection at runtime can impact the behavior of the state machine.
Methods
AddState(ISmState)
Adds the specified state to the state machine's collection of states.
Declaration
public virtual void AddState(ISmState state)
Parameters
| Type | Name | Description |
|---|---|---|
| ISmState | state | The state to add. Cannot be null. |
Remarks
If the state is not null, it is also added to the sorted state collection, keyed by its type. This allows for efficient retrieval of states by type.
AddState<TState>(TState)
Adds a new state to the state machine using the specified state instance.
Declaration
public virtual void AddState<TState>(TState state) where TState : ISmState
Parameters
| Type | Name | Description |
|---|---|---|
| TState | state | The state instance to add to the state machine. Cannot be null. |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of the state to add. Must implement ISmState. |
GetState(int)
Retrieves the state at the specified index within the state collection.
Declaration
public virtual 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
public virtual 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
public virtual 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. |
PrepareToRun()
Performs any necessary initialization or setup required before executing the main operation. Called by state machine once when state becomes active. If state is disabled and then enabled again, this method will be called again.
Declaration
public override void PrepareToRun()
Overrides
Remarks
Override this method to implement preparation logic specific to the derived class. This method should be called prior to running the core functionality to ensure all prerequisites are met.
PrepareToStop()
Performs any necessary actions to prepare the object for stopping or shutdown. Called when the state is about to be deactivated.
Declaration
public override void PrepareToStop()
Overrides
Remarks
Implementations should ensure that all resources are released and any ongoing operations are safely terminated before the object is stopped. This method is typically called prior to disposing or shutting down the object to ensure a clean transition.
SetState<TState>(int, TState, bool)
Sets the state at the specified index, optionally disposing the existing state before replacement.
Declaration
public virtual void SetState<TState>(int stateIndex, TState state, bool isDisposeExisting) where TState : ISmState
Parameters
| Type | Name | Description |
|---|---|---|
| int | stateIndex | The zero-based index at which to set the state. If less than zero, the method does nothing. |
| TState | state | The new state to assign at the specified index. If not null, the state is also tracked by its type. |
| bool | isDisposeExisting | Indicates whether to dispose the existing state at the specified index before replacing it. Set to true to dispose the existing state if it implements IDisposable; otherwise, false. |
Type Parameters
| Name | Description |
|---|---|
| TState | The type of state to set. Must implement ISmState. |
Remarks
If the specified index is greater than the current number of states, the collection is expanded and intermediate entries are set to their default values. The new state is also registered in the sorted state collection by its type if it is not null.
SwitchState(ISmState)
Transitions the state machine to the specified new state.
Declaration
public virtual 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
public virtual 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
public virtual 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
public virtual 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
public virtual 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
public override void Tick()
Overrides
Remarks
Call this method periodically to update the state machine's behavior. If no current state is assigned, this method has no effect.