Class base_fsm
Defined in File base_fsm.hpp
Inheritance Relationships
Base Type
public rcppsw::er::client< base_fsm >
(Template Class client)
Derived Types
public rcppsw::multithread::mt_fsm
(Class mt_fsm)public rcppsw::patterns::fsm::hfsm
(Class hfsm)
Class Documentation
-
class base_fsm : public rcppsw::er::client<base_fsm>
Implements a software-based state machine.
The FSM paradigm uses MEMBER function pointers, so you always need to initialize the state map cleanly WITHOUT copy construction (even inside copy constructors), otherwise all copies of the object will use the other object’s state map (default behavior in default copy constructor). If the source object is destructed, then you will get a segfault due to dangling pointers or a “pure virtual function called” error.
Thus, all base_fsm derived classes MUST implement the copy constructor, or delete it in order to ensure proper operation in all cases.
Subclassed by rcppsw::multithread::mt_fsm, rcppsw::patterns::fsm::hfsm
Public Functions
-
base_fsm(const base_fsm &other)
Copy the FSM to initialize another. Event data and whether or not an event is present is not copied.
-
explicit base_fsm(uint8_t max_states, uint8_t initial_state = 0)
-
~base_fsm(void) override = default
-
inline uint8_t current_state(void) const
Get the current state of the state machine.
-
virtual void init(void)
Initialize/reset the state machine.
-
void inject_event(int signal, int type)
Injects a signal of the specified type into the state machine, causing the state machine to execute and process the signal in its current state. This is the main means of running an FSM. Suitable for handling signals from within FSM states, and allowing outside classes to send whatever signals they want to the FSM.
-
void inject_event(std::unique_ptr<event_data> event)
Injects the signal of the specified type from the event argument into the state machine. This variant of inject_event() is provided for use with event_data_hold(), to avoid the event data overwrite which occurs with the other version.
-
inline uint8_t last_state(void) const
Get the state the the state machine was in the last time the state machine was run.
If a state machine has been in state
A
for the last 3 timesteps, and stateB
before that, and the current state isA
, then this function will returnA
.
-
inline uint8_t max_states(void) const
Get the maximum number of states for the state machine.
-
base_fsm &operator=(const base_fsm &other)
Copy the FSM to initialize another. Event data and whether or not an event is present is not copied.
-
inline uint8_t previous_state(void) const
Get the previous state the the state machine was in that is different than the current one.
If a state machine has been in state
A
for the last 3 timesteps, and stateB
before that, and the current state isA
, then this function will returnB
.
Protected Functions
-
inline class event_data *event_data(void)
-
inline const class event_data *event_data(void) const
Get the data associated with an event injected into the state machine.
-
inline void event_data_hold(bool b)
Indicate that the current event data should NOT be reset after state_engine() returns (default).
-
inline bool event_data_hold(void) const
-
inline std::unique_ptr<class event_data> event_data_release(void)
-
inline void external_event(uint8_t new_state)
-
virtual void external_event(uint8_t new_state, std::unique_ptr<class event_data> data)
Generates an external event. The data is passed through the event chain without modification. The FSM owns the event data—states should not try to delete it.
- Parameters:
new_state – The state machine state to transition to.
data – The event data sent to the state.
-
inline void generated_event(bool b)
-
inline bool has_generated_event(void)
-
inline uint8_t initial_state(void) const
-
inline void internal_event(uint8_t new_state)
-
void internal_event(uint8_t new_state, std::unique_ptr<class event_data> data)
Generates an internal event. These events are generated while executing within a state machine state. Internal states can pass their own data to other states without worrying about deleting the existing data—the FSM owns it and will handle it.
- Parameters:
new_state – The state machine state to transition to.
data – The event data sent to the state.
-
inline void next_state(uint8_t next_state)
-
inline uint8_t next_state(void) const
-
void state_engine(void)
-
virtual void state_engine_step(const state_map_ex_row *c_row_ex)
Execute one step of the state machine using the extended state definitions.
-
virtual void state_engine_step(const state_map_row *c_row)
Execute one step of the state machine using the simple state definitions.
-
inline virtual const state_map_row *state_map(size_t)
Gets the state map as defined in the derived class.
A state machine only needs to return a state map using either state_map() or state_map_ex() but not both.
- Returns:
The row corresponding to the passed in state in the state map.
-
inline virtual const state_map_ex_row *state_map_ex(size_t)
Gets the extended state map as defined in the derived class.
A state machine only needs to return a state map using either state_map() or state_map_ex() but not both.
- Returns:
The row corresponding to the passed in state in the state map.
-
void update_state(uint8_t new_state)
-
base_fsm(const base_fsm &other)