状态基类

[English]

公共头文件: #include "brookesia/lib_utils/state_base.hpp"

概述

state_base 定义状态机的基础状态抽象,提供进入、退出、更新等生命周期回调, 并支持状态超时与更新间隔配置。

特性

  • 标准化状态生命周期回调:on_enter / on_exit / on_update

  • 支持超时动作与更新间隔设置

  • 便于派生自定义状态类,统一状态行为管理

API 参考

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.

参数

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.

参数
  • 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.

返回

true to allow the transition, or false to reject entry.

inline virtual bool on_exit(const std::string &to_state = "", const std::string &action = "")

Hook invoked before the state is left.

参数
  • to_state – Name of the next state, or an empty string when unspecified.

  • action – Transition action name, or an empty string when not specified.

返回

true to allow the transition, or false to reject exit.

inline virtual void on_update()

Hook invoked periodically while the state remains active.

备注

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.

参数
  • ms – Timeout duration in milliseconds. A value of 0 disables 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.

参数

interval_ms – Update period in milliseconds. A value of 0 disables on_update() scheduling.

inline const std::string &get_name() const

Get the state name.

返回

State name.

inline uint32_t get_timeout_ms() const

Get the configured timeout duration.

返回

Timeout duration in milliseconds, or 0 when no timeout is configured.

inline const std::string &get_timeout_action() const

Get the action triggered when the timeout expires.

返回

Configured timeout action name.

inline uint32_t get_update_interval() const

Get the configured periodic update interval.

返回

Update interval in milliseconds, or 0 when periodic updates are disabled.