From acf1b0e20debee7484298415d793173288ccc9fe Mon Sep 17 00:00:00 2001 From: Laszlo Kiss Date: Fri, 10 Feb 2017 15:10:29 -0700 Subject: [PATCH] Added state specific default event handler. Added routines to allow removal of events from both event queues. --- FSM/FiniteStateMachine.hh | 57 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/FSM/FiniteStateMachine.hh b/FSM/FiniteStateMachine.hh index db84ecc..22f6347 100644 --- a/FSM/FiniteStateMachine.hh +++ b/FSM/FiniteStateMachine.hh @@ -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. * @@ -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. @@ -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() ) { @@ -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. @@ -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; @@ -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.