From a4e02930cc278823dd122509ba42aa897e74a50c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20R=C3=B6yhy?= Date: Fri, 1 Dec 2017 21:25:05 +0200 Subject: [PATCH] Make trigger function more synchronous with new parameter immidiate. Default is true so triger function is backwards compatiple --- Fsm.cpp | 20 +++++++++++++++++--- Fsm.h | 3 ++- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Fsm.cpp b/Fsm.cpp index 9b932ca..e406fff 100644 --- a/Fsm.cpp +++ b/Fsm.cpp @@ -91,7 +91,11 @@ Fsm::Transition Fsm::create_transition(State* state_from, State* state_to, return t; } -void Fsm::trigger(int event) +/* param immidiate = true will make state change while calling this function + param immediate=false will schedule state chage for next run_machine call + default is immidiate=true for backwards compability +*/ +void Fsm::trigger(int event, bool immidiate) { if (m_initialized) { @@ -101,7 +105,12 @@ void Fsm::trigger(int event) if (m_transitions[i].state_from == m_current_state && m_transitions[i].event == event) { - Fsm::make_transition(&(m_transitions[i])); + if(immidiate){ + Fsm::make_transition(&(m_transitions[i])); + }else{ + // queue state cha + Fsm::m_synchronous_transition = &(m_transitions[i]); + } return; } } @@ -143,7 +152,12 @@ void Fsm::run_machine() if (m_current_state->on_state != NULL) m_current_state->on_state(); - + + if(Fsm::m_synchronous_transition != NULL){ + Fsm::make_transition(m_synchronous_transition); + m_synchronous_transition = NULL; + } + Fsm::check_timed_transitions(); } diff --git a/Fsm.h b/Fsm.h index afbb3dd..70b9a19 100644 --- a/Fsm.h +++ b/Fsm.h @@ -47,7 +47,7 @@ class Fsm void check_timed_transitions(); - void trigger(int event); + void trigger(int event, bool immidiate=true); void run_machine(); private: @@ -73,6 +73,7 @@ class Fsm private: State* m_current_state; + Transition* m_synchronous_transition; Transition* m_transitions; int m_num_transitions;