diff --git a/include/etl/state_chart.h b/include/etl/state_chart.h index 7907a545..4aa7432b 100644 --- a/include/etl/state_chart.h +++ b/include/etl/state_chart.h @@ -164,8 +164,27 @@ namespace etl typedef state_chart_traits::state_id_t state_id_t; typedef state_chart_traits::event_id_t event_id_t; + istate_chart(state_id_t initial_state_id) + : current_state_id(initial_state_id) + { + } + + virtual void start(bool on_entry_initial = true) = 0; virtual void process_event(event_id_t, parameter_t) = 0; virtual ~istate_chart() {} + + //************************************************************************* + /// Gets the current state id. + /// \return The current state id. + //************************************************************************* + state_id_t get_state_id() const + { + return current_state_id; + } + + protected: + + state_id_t current_state_id; ///< The current state id. }; //*************************************************************************** @@ -180,8 +199,27 @@ namespace etl typedef state_chart_traits::state_id_t state_id_t; typedef state_chart_traits::event_id_t event_id_t; + istate_chart(state_id_t initial_state_id) + : current_state_id(initial_state_id) + { + } + virtual void process_event(event_id_t) = 0; + virtual void start(bool on_entry_initial = true) = 0; virtual ~istate_chart() {} + + //************************************************************************* + /// Gets the current state id. + /// \return The current state id. + //************************************************************************* + state_id_t get_state_id() const + { + return current_state_id; + } + + protected: + + state_id_t current_state_id; ///< The current state id. }; //*************************************************************************** @@ -210,7 +248,7 @@ namespace etl /// Constructor. //************************************************************************* ETL_CONSTEXPR state_chart_ct() - : current_state_id(Initial_State) + : istate_chart(Initial_State) , started(false) { } @@ -245,14 +283,14 @@ namespace etl //************************************************************************* /// Start the state chart. //************************************************************************* - void start(const bool on_entry_initial = true) + virtual void start(bool on_entry_initial = true) ETL_OVERRIDE { if (!started) { if (on_entry_initial) { // See if we have a state item for the initial state. - const state* s = find_state(current_state_id); + const state* s = find_state(this->current_state_id); // If the initial state has an 'on_entry' then call it. if ((s != (State_Table_Begin + State_Table_Size)) && (s->on_entry != ETL_NULLPTR)) @@ -281,7 +319,7 @@ namespace etl while (t != (Transition_Table_Begin + Transition_Table_Size)) { // Scan the transition table from the latest position. - t = etl::find_if(t, (Transition_Table_Begin + Transition_Table_Size), is_transition(event_id, current_state_id)); + t = etl::find_if(t, (Transition_Table_Begin + Transition_Table_Size), is_transition(event_id, this->current_state_id)); // Found an entry? if (t != (Transition_Table_Begin + Transition_Table_Size)) @@ -296,12 +334,12 @@ namespace etl } // Changing state? - if (current_state_id != t->next_state_id) + if (this->current_state_id != t->next_state_id) { const state* s; // See if we have a state item for the current state. - s = find_state(current_state_id); + s = find_state(this->current_state_id); // If the current state has an 'on_exit' then call it. if ((s != (State_Table_Begin + State_Table_Size)) && (s->on_exit != ETL_NULLPTR)) @@ -309,10 +347,10 @@ namespace etl (TObject_Ref.*(s->on_exit))(); } - current_state_id = t->next_state_id; + this->current_state_id = t->next_state_id; // See if we have a state item for the new state. - s = find_state(current_state_id); + s = find_state(this->current_state_id); // If the new state has an 'on_entry' then call it. if ((s != (State_Table_Begin + State_Table_Size)) && (s->on_entry != ETL_NULLPTR)) @@ -333,15 +371,6 @@ namespace etl } } - //************************************************************************* - /// Gets the current state id. - /// \return The current state id. - //************************************************************************* - state_id_t get_state_id() const - { - return current_state_id; - } - private: //************************************************************************* @@ -382,8 +411,7 @@ namespace etl state_chart_ct(const state_chart_ct&) ETL_DELETE; state_chart_ct& operator =(const state_chart_ct&) ETL_DELETE; - state_id_t current_state_id; ///< The current state id. - bool started; ///< Set if the state chart has been started. + bool started; ///< Set if the state chart has been started. }; //*************************************************************************** @@ -413,7 +441,7 @@ namespace etl /// Constructor. //************************************************************************* ETL_CONSTEXPR state_chart_ctp() - : current_state_id(Initial_State) + : istate_chart(Initial_State) , started(false) { } @@ -448,14 +476,14 @@ namespace etl //************************************************************************* /// Start the state chart. //************************************************************************* - void start(const bool on_entry_initial = true) + virtual void start(bool on_entry_initial = true) ETL_OVERRIDE { if (!started) { if (on_entry_initial) { // See if we have a state item for the initial state. - const state* s = find_state(current_state_id); + const state* s = find_state(this->current_state_id); // If the initial state has an 'on_entry' then call it. if ((s != (State_Table_Begin + State_Table_Size)) && (s->on_entry != ETL_NULLPTR)) @@ -484,7 +512,7 @@ namespace etl while (t != (Transition_Table_Begin + Transition_Table_Size)) { // Scan the transition table from the latest position. - t = etl::find_if(t, (Transition_Table_Begin + Transition_Table_Size), is_transition(event_id, current_state_id)); + t = etl::find_if(t, (Transition_Table_Begin + Transition_Table_Size), is_transition(event_id, this->current_state_id)); // Found an entry? if (t != (Transition_Table_Begin + Transition_Table_Size)) @@ -503,12 +531,12 @@ namespace etl } // Changing state? - if (current_state_id != t->next_state_id) + if (this->current_state_id != t->next_state_id) { const state* s; // See if we have a state item for the current state. - s = find_state(current_state_id); + s = find_state(this->current_state_id); // If the current state has an 'on_exit' then call it. if ((s != (State_Table_Begin + State_Table_Size)) && (s->on_exit != ETL_NULLPTR)) @@ -516,10 +544,10 @@ namespace etl (TObject_Ref.*(s->on_exit))(); } - current_state_id = t->next_state_id; + this->current_state_id = t->next_state_id; // See if we have a state item for the new state. - s = find_state(current_state_id); + s = find_state(this->current_state_id); // If the new state has an 'on_entry' then call it. if ((s != (State_Table_Begin + State_Table_Size)) && (s->on_entry != ETL_NULLPTR)) @@ -540,15 +568,6 @@ namespace etl } } - //************************************************************************* - /// Gets the current state id. - /// \return The current state id. - //************************************************************************* - state_id_t get_state_id() const - { - return current_state_id; - } - private: //************************************************************************* @@ -589,8 +608,7 @@ namespace etl state_chart_ctp(const state_chart_ctp&) ETL_DELETE; state_chart_ctp& operator =(const state_chart_ctp&) ETL_DELETE; - state_id_t current_state_id; ///< The current state id. - bool started; ///< Set if the state chart has been started. + bool started; ///< Set if the state chart has been started. }; //*************************************************************************** @@ -624,12 +642,12 @@ namespace etl const state* state_table_begin_, const state* state_table_end_, const state_id_t state_id_) - : object(object_) + : istate_chart(state_id_) + , object(object_) , transition_table_begin(transition_table_begin_) , state_table_begin(state_table_begin_) , transition_table_size(transition_table_end_ - transition_table_begin_) , state_table_size(state_table_end_ - state_table_begin_) - , current_state_id(state_id_) , started(false) { } @@ -695,14 +713,14 @@ namespace etl //************************************************************************* /// Start the state chart. //************************************************************************* - void start(const bool on_entry_initial = true) + virtual void start(bool on_entry_initial = true) ETL_OVERRIDE { if (!started) { if (on_entry_initial) { // See if we have a state item for the initial state. - const state* s = find_state(current_state_id); + const state* s = find_state(this->current_state_id); // If the initial state has an 'on_entry' then call it. if ((s != state_table_end()) && (s->on_entry != ETL_NULLPTR)) @@ -731,7 +749,7 @@ namespace etl while (t != transition_table_end()) { // Scan the transition table from the latest position. - t = etl::find_if(t, transition_table_end(), is_transition(event_id, current_state_id)); + t = etl::find_if(t, transition_table_end(), is_transition(event_id, this->current_state_id)); // Found an entry? if (t != transition_table_end()) @@ -750,12 +768,12 @@ namespace etl } // Changing state? - if (current_state_id != t->next_state_id) + if (this->current_state_id != t->next_state_id) { const state* s; // See if we have a state item for the current state. - s = find_state(current_state_id); + s = find_state(this->current_state_id); // If the current state has an 'on_exit' then call it. if ((s != state_table_end()) && (s->on_exit != ETL_NULLPTR)) @@ -763,10 +781,10 @@ namespace etl (object.*(s->on_exit))(); } - current_state_id = t->next_state_id; + this->current_state_id = t->next_state_id; // See if we have a state item for the new state. - s = find_state(current_state_id); + s = find_state(this->current_state_id); // If the new state has an 'on_entry' then call it. if ((s != state_table_end()) && (s->on_entry != ETL_NULLPTR)) @@ -787,15 +805,6 @@ namespace etl } } - //************************************************************************* - /// Gets the current state id. - /// \return The current state id. - //************************************************************************* - state_id_t get_state_id() const - { - return current_state_id; - } - private: //************************************************************************* @@ -853,7 +862,6 @@ namespace etl const state* state_table_begin; ///< The start of the table of states. uint_least8_t transition_table_size; ///< The size of the table of transitions. uint_least8_t state_table_size; ///< The size of the table of states. - state_id_t current_state_id; ///< The current state id. bool started; ///< Set if the state chart has been started. }; @@ -888,12 +896,12 @@ namespace etl const state* state_table_begin_, const state* state_table_end_, const state_id_t state_id_) - : object(object_) + : istate_chart(state_id_) + , object(object_) , transition_table_begin(transition_table_begin_) , state_table_begin(state_table_begin_) , transition_table_size(transition_table_end_ - transition_table_begin_) , state_table_size(state_table_end_ - state_table_begin_) - , current_state_id(state_id_) , started(false) { } @@ -959,14 +967,14 @@ namespace etl //************************************************************************* /// Start the state chart. //************************************************************************* - void start(const bool on_entry_initial = true) + virtual void start(bool on_entry_initial = true) ETL_OVERRIDE { if (!started) { if (on_entry_initial) { // See if we have a state item for the initial state. - const state* s = find_state(current_state_id); + const state* s = find_state(this->current_state_id); // If the initial state has an 'on_entry' then call it. if ((s != state_table_end()) && (s->on_entry != ETL_NULLPTR)) @@ -995,7 +1003,7 @@ namespace etl while (t != transition_table_end()) { // Scan the transition table from the latest position. - t = etl::find_if(t, transition_table_end(), is_transition(event_id, current_state_id)); + t = etl::find_if(t, transition_table_end(), is_transition(event_id, this->current_state_id)); // Found an entry? if (t != transition_table_end()) @@ -1010,12 +1018,12 @@ namespace etl } // Changing state? - if (current_state_id != t->next_state_id) + if (this->current_state_id != t->next_state_id) { const state* s; // See if we have a state item for the current state. - s = find_state(current_state_id); + s = find_state(this->current_state_id); // If the current state has an 'on_exit' then call it. if ((s != state_table_end()) && (s->on_exit != ETL_NULLPTR)) @@ -1023,10 +1031,10 @@ namespace etl (object.*(s->on_exit))(); } - current_state_id = t->next_state_id; + this->current_state_id = t->next_state_id; // See if we have a state item for the new state. - s = find_state(current_state_id); + s = find_state(this->current_state_id); // If the new state has an 'on_entry' then call it. if ((s != state_table_end()) && (s->on_entry != ETL_NULLPTR)) @@ -1047,15 +1055,6 @@ namespace etl } } - //************************************************************************* - /// Gets the current state id. - /// \return The current state id. - //************************************************************************* - state_id_t get_state_id() const - { - return current_state_id; - } - private: //************************************************************************* @@ -1113,7 +1112,6 @@ namespace etl const state* state_table_begin; ///< The start of the table of states. uint_least8_t transition_table_size; ///< The size of the table of transitions. uint_least8_t state_table_size; ///< The size of the table of states. - state_id_t current_state_id; ///< The current state id. bool started; ///< Set if the state chart has been started. }; }