infinite_state_machine 1.0.0
Infinite State Machine Library
Loading...
Searching...
No Matches
Functions
infinite_state_machine.c File Reference

Stack-based hierarchical (potentially unbounded logical) state machine implementation. More...

#include "infinite_state_machine.h"
#include <string.h>
#include <errno.h>
Include dependency graph for infinite_state_machine.c:

Go to the source code of this file.

Functions

void infinite_state_machine_init (struct infinite_state_machine *machine)
 Initialises the infinite state machine. The machine is reset to its initial state. It has an initial depth of 0. All its states are cleared to NULL for safety.
 
void infinite_state_machine_goto (struct infinite_state_machine *machine, struct infinite_state *state)
 Goes to a state in the infinite state machine.
 
void infinite_state_machine_jump (struct infinite_state_machine *machine, struct infinite_state *state)
 Jumps to a state in the infinite state machine.
 
int infinite_state_machine_in (struct infinite_state_machine *machine, struct infinite_state *state)
 Checks if a state is currently active in the infinite state machine.
 
struct infinite_stateinfinite_state_machine_top (const struct infinite_state_machine *machine)
 Gets the top state of the infinite state machine.
 

Detailed Description

Stack-based hierarchical (potentially unbounded logical) state machine implementation.

Provides push and pop (enter and exit) semantics plus goto that performs least–common–ancestor optimisation: only differing tail states are exited or entered.

Invariants:

Notes:

Definition in file infinite_state_machine.c.

Function Documentation

◆ infinite_state_machine_goto()

void infinite_state_machine_goto ( struct infinite_state_machine machine,
struct infinite_state state 
)

Goes to a state in the infinite state machine.

Parameters
machineThe infinite state machine.
stateThe state to enter.

Going to a state in the infinite state machine transitions the machine to the new state. This will push the current state onto the stack and transition to the new state. If the new state is the same as the current state, no action is taken; likewise, if the new state is a NULL pointer, no action is taken. Otherwise, all the exit actions for the current state are run, and the new state is entered by running all enter actions.

Note
O(n) time complexity applies, where n is the depth of the state machine.

Definition at line 66 of file infinite_state_machine.c.

◆ infinite_state_machine_in()

int infinite_state_machine_in ( struct infinite_state_machine machine,
struct infinite_state state 
)

Checks if a state is currently active in the infinite state machine.

Parameters
machineThe infinite state machine.
stateThe state to check.
Returns
1 if the state is active, 0 if it is not, or a negative error code on failure.

Definition at line 99 of file infinite_state_machine.c.

◆ infinite_state_machine_init()

void infinite_state_machine_init ( struct infinite_state_machine machine)

Initialises the infinite state machine. The machine is reset to its initial state. It has an initial depth of 0. All its states are cleared to NULL for safety.

Parameters
machineThe infinite state machine to initialise.

Definition at line 60 of file infinite_state_machine.c.

◆ infinite_state_machine_jump()

void infinite_state_machine_jump ( struct infinite_state_machine machine,
struct infinite_state state 
)

Jumps to a state in the infinite state machine.

Parameters
machineThe infinite state machine.
stateThe state to jump to.

Jumping to a state in the infinite state machine resets the machine to its initial state and then transitions to the specified state. This is useful for resetting the machine to a known state without going through the normal entry and exit actions.

Note
O(n) time complexity applies, where n is the depth of the state machine.

Definition at line 93 of file infinite_state_machine.c.

◆ infinite_state_machine_top()

struct infinite_state * infinite_state_machine_top ( const struct infinite_state_machine machine)

Gets the top state of the infinite state machine.

Parameters
machineThe infinite state machine.
Returns
The top state, or NULL if the machine is empty.

Definition at line 157 of file infinite_state_machine.c.