34#error THIS HEADER IS A GENERATOR. DO NOT INCLUDE.
63#ifndef ETL_FSM_INCLUDED
64#define ETL_FSM_INCLUDED
72#include "message_router.h"
86#if !defined(ETL_FSM_STATE_ID_TYPE)
95#if ETL_USING_CPP17 && !defined(ETL_FSM_FORCE_CPP03_IMPLEMENTATION)
121 fsm_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
122 :
etl::exception(reason_, file_name_, line_number_)
134 fsm_null_state_exception(string_type file_name_, numeric_type line_number_)
135 :
etl::fsm_exception(ETL_ERROR_TEXT(
"fsm:null state", ETL_FSM_FILE_ID
"A"), file_name_, line_number_)
147 fsm_state_id_exception(string_type file_name_, numeric_type line_number_)
148 :
etl::fsm_exception(ETL_ERROR_TEXT(
"fsm:state id", ETL_FSM_FILE_ID
"B"), file_name_, line_number_)
160 fsm_state_list_exception(string_type file_name_, numeric_type line_number_)
161 :
etl::fsm_exception(ETL_ERROR_TEXT(
"fsm:state list", ETL_FSM_FILE_ID
"C"), file_name_, line_number_)
173 fsm_state_list_order_exception(string_type file_name_, numeric_type line_number_)
174 :
etl::fsm_exception(ETL_ERROR_TEXT(
"fsm:state list order", ETL_FSM_FILE_ID
"D"), file_name_, line_number_)
185 fsm_state_composite_state_change_forbidden(string_type file_name_, numeric_type line_number_)
186 :
etl::fsm_exception(ETL_ERROR_TEXT(
"fsm:change in composite state forbidden", ETL_FSM_FILE_ID
"E"), file_name_, line_number_)
191 namespace private_fsm
193 template <
typename T =
void>
194 class ifsm_state_helper
203 static ETL_CONSTANT
fsm_state_id_t Pass_To_Parent = No_State_Change - 1U;
206 template <
typename T>
207 ETL_CONSTANT
fsm_state_id_t ifsm_state_helper<T>::No_State_Change;
209 template <
typename T>
216 class ifsm_state :
public private_fsm::ifsm_state_helper<>
224 using private_fsm::ifsm_state_helper<>::No_State_Change;
225 using private_fsm::ifsm_state_helper<>::Pass_To_Parent;
227#if ETL_USING_CPP17 && !defined(ETL_FSM_FORCE_CPP03_IMPLEMENTATION)
229 friend class fsm_state;
261 state.p_parent =
this;
263 if (p_default_child == ETL_NULLPTR)
265 p_active_child = &state;
266 p_default_child = &state;
274 template <
typename TSize>
277 p_active_child = ETL_NULLPTR;
278 p_default_child = ETL_NULLPTR;
283 add_child_state(*state_list[
i]);
294 p_context(ETL_NULLPTR),
295 p_parent(ETL_NULLPTR),
296 p_active_child(ETL_NULLPTR),
297 p_default_child(ETL_NULLPTR)
318 virtual fsm_state_id_t on_enter_state() {
return No_State_Change; }
319 virtual void on_exit_state() {}
322 void set_fsm_context(
etl::fsm& context)
324 p_context = &context;
334 ifsm_state* p_parent;
337 ifsm_state* p_active_child;
340 ifsm_state* p_default_child;
343 ifsm_state(
const ifsm_state&);
344 ifsm_state& operator =(
const ifsm_state&);
355 using imessage_router::receive;
360 fsm(etl::message_router_id_t
id)
362 , p_state(ETL_NULLPTR)
363 , state_list(ETL_NULLPTR)
364 , number_of_states(0U)
371 template <
typename TSize>
384 state_list[
i]->set_fsm_context(*
this);
397 if (p_state == ETL_NULLPTR)
399 p_state = state_list[0];
410 next_state_id = p_state->on_enter_state();
411 if (next_state_id != ifsm_state::No_State_Change)
414 p_state = state_list[next_state_id];
428 if (have_changed_state(next_state_id))
435 p_state->on_exit_state();
438 next_state_id = p_state->on_enter_state();
440 if (have_changed_state(next_state_id))
449 using imessage_router::accepts;
466 return p_state->get_state_id();
492 return p_state != ETL_NULLPTR;
503 p_state->on_exit_state();
506 p_state = ETL_NULLPTR;
510 ETL_DEPRECATED
bool is_null_router()
const ETL_OVERRIDE
516 bool is_producer() const ETL_OVERRIDE
522 bool is_consumer() const ETL_OVERRIDE
532 return (next_state_id != p_state->get_state_id()) &&
533 (next_state_id != ifsm_state::No_State_Change);
544#if ETL_USING_CPP17 && !defined(ETL_FSM_FORCE_CPP03_IMPLEMENTATION)
548 template <
typename TContext,
typename TDerived,
etl::fsm_state_id_t STATE_ID_,
typename... TMessageTypes>
549 class fsm_state :
public ifsm_state
566 TContext& get_fsm_context()
const
568 return static_cast<TContext&
>(ifsm_state::get_fsm_context());
585 const bool was_handled = (process_event_type<TMessageTypes>(message, new_state_id) || ...);
587 if (!was_handled || (new_state_id == Pass_To_Parent))
589 new_state_id = (p_parent !=
nullptr) ? p_parent->process_event(message) :
static_cast<TDerived*
>(
this)->on_event_unknown(message);
596 template <
typename TMessage>
599 if (TMessage::ID == msg.get_message_id())
601 state_id =
static_cast<TDerived*
>(
this)->on_event(
static_cast<const TMessage&
>(msg));
612 template <
typename TContext,
typename TDerived,
etl::fsm_state_id_t STATE_ID_,
typename... TMessageTypes>
613 ETL_CONSTANT
etl::fsm_state_id_t fsm_state<TContext, TDerived, STATE_ID_, TMessageTypes...>::STATE_ID;
Base exception class for FSM.
Definition fsm.h:99
Exception for null state pointer.
Definition fsm.h:112
Exception for invalid state id.
Definition fsm.h:125
Exception for incompatible state list.
Definition fsm.h:138
Exception for incompatible order state list.
Definition fsm.h:151
The FSM class.
Definition fsm.h:325
etl::fsm_state_id_t get_state_id() const
Gets the current state id.
Definition fsm_generator.h:463
void receive(const etl::imessage &message) ETL_OVERRIDE
Top level message handler for the FSM.
Definition fsm_generator.h:424
virtual void start(bool call_on_enter_state=true)
Starts the FSM. Can only be called once. Subsequent calls will do nothing.
Definition fsm_generator.h:394
fsm(etl::message_router_id_t id)
Constructor.
Definition fsm_generator.h:360
virtual void reset(bool call_on_exit_state=false)
Reset the FSM to pre-started state.
Definition fsm_generator.h:499
bool accepts(etl::message_id_t) const ETL_OVERRIDE
Does this FSM accept the message id? Yes, it accepts everything!
Definition fsm_generator.h:455
void set_states(etl::ifsm_state **p_states, TSize size)
Set the states for the FSM.
Definition fsm_generator.h:372
const ifsm_state & get_state() const
Gets a const reference to the current state interface.
Definition fsm_generator.h:481
ifsm_state & get_state()
Gets a reference to the current state interface.
Definition fsm_generator.h:472
bool is_started() const
Checks if the FSM has been started.
Definition fsm_generator.h:490
Interface class for FSM states.
Definition fsm.h:198
void add_child_state(etl::ifsm_state &state)
Adds a child to this state. Only of use when part of an HFSM.
Definition fsm_generator.h:258
void set_child_states(etl::ifsm_state **state_list, TSize size)
Adds a list of child states. Only of use when part of an HFSM.
Definition fsm_generator.h:275
etl::fsm_state_id_t get_state_id() const
Gets the id for this state.
Definition fsm_generator.h:249
ifsm_state(etl::fsm_state_id_t state_id_)
Constructor.
Definition fsm_generator.h:292
virtual ~ifsm_state()
Destructor.
Definition fsm_generator.h:304
This is the base of all message routers.
Definition message_router_generator.h:121
#define ETL_ASSERT(b, e)
Definition error_handler.h:316
Definition exception.h:47
Definition integral_limits.h:468
Defines a type that is as larger or larger than the specified type. Will return the specified type is...
Definition largest_generator.h:352
bitset_ext
Definition absolute.h:38
uint_least8_t message_id_t
Allow alternative type for message id.
Definition message_types.h:40
uint_least8_t fsm_state_id_t
Allow alternative type for state id.
Definition fsm.h:75
pair holds two objects of arbitrary type
Definition utility.h:164