Skip to content

Commit

Permalink
Make trigger function more synchronous with new parameter immidiate. …
Browse files Browse the repository at this point in the history
…Default is true so triger function is backwards compatiple
  • Loading branch information
Troyhy committed Dec 1, 2017
1 parent 55a1971 commit a4e0293
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
20 changes: 17 additions & 3 deletions Fsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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();
}

Expand Down
3 changes: 2 additions & 1 deletion Fsm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -73,6 +73,7 @@ class Fsm

private:
State* m_current_state;
Transition* m_synchronous_transition;
Transition* m_transitions;
int m_num_transitions;

Expand Down

0 comments on commit a4e0293

Please sign in to comment.