19template <
typename State,
typename Event>
21 if constexpr (
requires { s.onExit(e); }) {
23 }
else if constexpr (
requires { s.onExit(); }) {
28template <
typename State,
typename Event>
30 if constexpr (
requires { s.onEnter(e); }) {
32 }
else if constexpr (
requires { s.onEnter(); }) {
37template <
typename Guard,
typename State,
typename Event>
39 if constexpr (std::is_invocable_r_v<bool, Guard, const State&, const Event&>) {
41 }
else if constexpr (std::is_invocable_r_v<bool, Guard, const Event&>) {
43 }
else if constexpr (std::is_invocable_r_v<bool, Guard>) {
50template <
typename Action,
typename From,
typename Event,
typename To>
52 if constexpr (std::is_invocable_v<Action, From&, const Event&, To&>) {
54 }
else if constexpr (std::is_invocable_v<Action, From&, const Event&>) {
56 }
else if constexpr (std::is_invocable_v<Action, const Event&>) {
58 }
else if constexpr (std::is_invocable_v<Action>) {
65 static constexpr bool value =
false;
69 requires requires { { T::is_internal } -> std::convertible_to<bool>; }
71 static constexpr bool value = T::is_internal;
86 typename InitialState,
87 typename... OtherStates
95 : _state(InitialState{})
100 template <
typename State>
101 requires (std::is_constructible_v<StateVariant, State>)
103 : _state(std::forward<State>(initial))
105 std::visit([](
auto& s) {
111 template <
typename State>
112 [[nodiscard]]
constexpr bool is() const noexcept {
113 return std::holds_alternative<State>(_state);
117 template <
typename State>
118 [[nodiscard]]
constexpr State&
as() {
119 return std::get<State>(_state);
123 template <
typename State>
124 [[nodiscard]]
constexpr const State&
as()
const {
125 return std::get<State>(_state);
142 template <
typename Event>
144 return std::visit([
this, &event](
auto& currentState) ->
bool {
145 using CurrentStateType = std::decay_t<
decltype(currentState)>;
146 return this->try_transition<CurrentStateType, Event>(currentState, event, Table{});
151 template <
typename CurrentState,
typename Event,
typename... Transitions>
153 return (try_single_transition<CurrentState, Event, Transitions>(current, event, Transitions{}) || ...);
156 template <
typename CurrentState,
typename Event,
typename Trans>
157 bool try_single_transition(CurrentState& current,
const Event& event,
const Trans& trans) {
158 if constexpr (std::is_same_v<CurrentState, typename Trans::FromState> &&
159 std::is_same_v<std::decay_t<Event>,
typename Trans::EventType>) {
161 if constexpr (detail::is_internal_transition_v<Trans>) {
165 typename Trans::ToState nextState{};
167 _state = std::move(nextState);
Declarative compile-time transition rules, internal transitions, and action lists.
Zero-heap, compile-time Finite State Machine.
Definition StateMachine.hpp:89
Table TransitionTableType
Definition StateMachine.hpp:92
std::variant< InitialState, OtherStates... > StateVariant
Definition StateMachine.hpp:91
constexpr StateMachine()
Definition StateMachine.hpp:94
constexpr const State & as() const
Access const reference to current state as type State.
Definition StateMachine.hpp:124
constexpr StateMachine(State &&initial)
Definition StateMachine.hpp:102
bool process_event(const Event &event)
Process an incoming event through the state machine transition table.
Definition StateMachine.hpp:143
constexpr State & as()
Access reference to current state as type State (throws std::bad_variant_access if mismatch).
Definition StateMachine.hpp:118
constexpr StateVariant & state() noexcept
Access active state variant.
Definition StateMachine.hpp:134
constexpr bool is() const noexcept
Check if current active state matches type State.
Definition StateMachine.hpp:112
constexpr const StateVariant & state() const noexcept
Access active state variant.
Definition StateMachine.hpp:129
constexpr void call_on_enter(State &s, const Event &e)
Definition StateMachine.hpp:29
constexpr bool evaluate_guard(const Guard &guard, const State &s, const Event &e)
Definition StateMachine.hpp:38
constexpr bool is_internal_transition_v
Definition StateMachine.hpp:75
constexpr void call_on_exit(State &s, const Event &e)
Definition StateMachine.hpp:20
constexpr void execute_action(const Action &action, From &from, const Event &e, To &to)
Definition StateMachine.hpp:51
Definition HistoryState.hpp:9
DefaultEvents Event
Alias for DefaultEvents.
Definition Events.hpp:65
Compile-time table containing all valid state transitions.
Definition Transition.hpp:103
Definition StateMachine.hpp:64
static constexpr bool value
Definition StateMachine.hpp:65