Skip to content

Commit acf1b0e

Browse files
committed
Added state specific default event handler.
Added routines to allow removal of events from both event queues.
1 parent dcaa46b commit acf1b0e

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

FSM/FiniteStateMachine.hh

+56-1
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,17 @@ namespace Core
141141
, exit_function()
142142
, dispatch_table()
143143
, single_dispatch()
144+
, default_handler()
145+
, transition_table()
146+
, forwarded_events()
147+
, event_store()
148+
, storeforward_table()
144149
{ }
145150

146151

152+
virtual ~State() = default;
153+
154+
147155
/**
148156
* Sets the function the FSM calls when the state is entered.
149157
*
@@ -300,6 +308,21 @@ namespace Core
300308
}
301309

302310

311+
/**
312+
* Sets the default event handler function of the state to use when an
313+
* event does not have a specific handler.
314+
*
315+
* @param handler The handler function object to set.
316+
*/
317+
virtual State & SetDefaultEventHandler(
318+
const EventHandler & handler
319+
)
320+
{
321+
default_handler = handler;
322+
return *this;
323+
}
324+
325+
303326
/**
304327
* Replaces a potentially existing event handler function with another.
305328
* This is a 'fancy' capability that should be used with caution.
@@ -381,7 +404,7 @@ namespace Core
381404
const Event & event
382405
) const
383406
{
384-
EventHandler handler{};
407+
EventHandler handler{ default_handler };
385408
auto dit = dispatch_table.find( event );
386409
if ( dit != dispatch_table.end() )
387410
{
@@ -400,6 +423,17 @@ namespace Core
400423
}
401424

402425

426+
/**
427+
* Gets the state specific default event handler function.
428+
*
429+
* @return The default handler function object.
430+
*/
431+
virtual EventHandler DefaultEventHandler()
432+
{
433+
return default_handler;
434+
}
435+
436+
403437
/**
404438
* Called by the FiniteStateMachine when a state transition is
405439
* taking place and the state is becoming the new current state.
@@ -515,6 +549,7 @@ namespace Core
515549
ExitFunction exit_function;
516550
mutable DispatchTable dispatch_table;
517551
mutable SingleDispatch single_dispatch;
552+
EventHandler default_handler; // state specific default handler
518553
TransitionTable transition_table;
519554
ForwardedEvents forwarded_events;
520555
Events event_store;
@@ -821,6 +856,26 @@ namespace Core
821856
}
822857

823858

859+
/**
860+
* Removes the specific event from the external event queue.
861+
*/
862+
inline void RemoveEvent( const Event & event )
863+
{
864+
auto it = std::find( events.begin(), events.end(), (int) event );
865+
if ( it != events.end() ) { events.erase( it ); }
866+
}
867+
868+
869+
/**
870+
* Removes the specific event from the external event queue.
871+
*/
872+
inline void RemoveInternalEvent( const Event & event )
873+
{
874+
auto it = std::find( internal_events.begin(), internal_events.end(), (int) event );
875+
if ( it != internal_events.end() ) { internal_events.erase( it ); }
876+
}
877+
878+
824879
private :
825880
StateID previous_state; /// The state prior to the current one.
826881
StateID current_state; /// The index into the state table.

0 commit comments

Comments
 (0)