Skip to content

Commit

Permalink
Added state specific default event handler.
Browse files Browse the repository at this point in the history
Added routines to allow removal of events from both event queues.
  • Loading branch information
laszlo-kiss committed Feb 10, 2017
1 parent dcaa46b commit acf1b0e
Showing 1 changed file with 56 additions and 1 deletion.
57 changes: 56 additions & 1 deletion FSM/FiniteStateMachine.hh
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,17 @@ namespace Core
, exit_function()
, dispatch_table()
, single_dispatch()
, default_handler()
, transition_table()
, forwarded_events()
, event_store()
, storeforward_table()
{ }


virtual ~State() = default;


/**
* Sets the function the FSM calls when the state is entered.
*
Expand Down Expand Up @@ -300,6 +308,21 @@ namespace Core
}


/**
* Sets the default event handler function of the state to use when an
* event does not have a specific handler.
*
* @param handler The handler function object to set.
*/
virtual State & SetDefaultEventHandler(
const EventHandler & handler
)
{
default_handler = handler;
return *this;
}


/**
* Replaces a potentially existing event handler function with another.
* This is a 'fancy' capability that should be used with caution.
Expand Down Expand Up @@ -381,7 +404,7 @@ namespace Core
const Event & event
) const
{
EventHandler handler{};
EventHandler handler{ default_handler };
auto dit = dispatch_table.find( event );
if ( dit != dispatch_table.end() )
{
Expand All @@ -400,6 +423,17 @@ namespace Core
}


/**
* Gets the state specific default event handler function.
*
* @return The default handler function object.
*/
virtual EventHandler DefaultEventHandler()
{
return default_handler;
}


/**
* Called by the FiniteStateMachine when a state transition is
* taking place and the state is becoming the new current state.
Expand Down Expand Up @@ -515,6 +549,7 @@ namespace Core
ExitFunction exit_function;
mutable DispatchTable dispatch_table;
mutable SingleDispatch single_dispatch;
EventHandler default_handler; // state specific default handler
TransitionTable transition_table;
ForwardedEvents forwarded_events;
Events event_store;
Expand Down Expand Up @@ -821,6 +856,26 @@ namespace Core
}


/**
* Removes the specific event from the external event queue.
*/
inline void RemoveEvent( const Event & event )
{
auto it = std::find( events.begin(), events.end(), (int) event );
if ( it != events.end() ) { events.erase( it ); }
}


/**
* Removes the specific event from the external event queue.
*/
inline void RemoveInternalEvent( const Event & event )
{
auto it = std::find( internal_events.begin(), internal_events.end(), (int) event );
if ( it != internal_events.end() ) { internal_events.erase( it ); }
}


private :
StateID previous_state; /// The state prior to the current one.
StateID current_state; /// The index into the state table.
Expand Down

0 comments on commit acf1b0e

Please sign in to comment.