State Base Class
Public header: #include "brookesia/lib_utils/state_base.hpp"
Overview
state_base defines the base state abstraction for state machines with enter, exit, and update callbacks, plus timeouts and update intervals.
Features
Standard lifecycle: on_enter / on_exit / on_update
Timeout actions and update intervals
Easy to subclass for custom states
API Reference
Header File
Classes
-
class StateBase
Base class for state machine states.
Provides lifecycle hooks (on_enter, on_exit, on_update) and configuration for timeout and periodic update intervals.
Public Functions
-
inline explicit StateBase(const std::string &name)
Constructor.
- Parameters
name – State name.
-
virtual ~StateBase() = default
Virtual destructor.
-
inline virtual bool on_enter(const std::string &from_state = "", const std::string &action = "")
Hook invoked before the state becomes active.
- Parameters
from_state – Name of the previous state, or an empty string for the initial state.
action – Transition action name, or an empty string when not specified.
- Returns
trueto allow the transition, orfalseto reject entry.
-
inline virtual bool on_exit(const std::string &to_state = "", const std::string &action = "")
Hook invoked before the state is left.
- Parameters
to_state – Name of the next state, or an empty string when unspecified.
action – Transition action name, or an empty string when not specified.
- Returns
trueto allow the transition, orfalseto reject exit.
-
inline virtual void on_update()
Hook invoked periodically while the state remains active.
Note
This callback is scheduled only when
set_update_interval()configures a non-zero interval.
-
inline void set_timeout(uint32_t ms, const std::string &action)
Configure an automatic timeout transition for this state.
- Parameters
ms – Timeout duration in milliseconds. A value of
0disables the timeout.action – Action name triggered when the timeout expires.
-
inline void set_update_interval(uint32_t interval_ms)
Configure the periodic update interval for this state.
- Parameters
interval_ms – Update period in milliseconds. A value of
0disableson_update()scheduling.
-
inline const std::string &get_name() const
Get the state name.
- Returns
State name.
-
inline uint32_t get_timeout_ms() const
Get the configured timeout duration.
- Returns
Timeout duration in milliseconds, or
0when no timeout is configured.
-
inline const std::string &get_timeout_action() const
Get the action triggered when the timeout expires.
- Returns
Configured timeout action name.
-
inline uint32_t get_update_interval() const
Get the configured periodic update interval.
- Returns
Update interval in milliseconds, or
0when periodic updates are disabled.
-
inline explicit StateBase(const std::string &name)