digint / tinyfsm

A simple C++ finite state machine library
https://digint.ch/tinyfsm
MIT License
931 stars 173 forks source link

Events double dispatching support #17

Closed nameofuser1 closed 2 years ago

nameofuser1 commented 5 years ago

What do you think about making Events to be double dispatch-able? For now it is necessary to explicitly specify event type when dispatching event, however since it is c++ library it can be useful to take advantage of polymorphic events.

digint commented 5 years ago

TinyFSM does not limit Fsm::dispatch() to any type, this simply calls State::react() of the current state with the given type (which does not necessarily needs to be derived from tinyfsm::Event).

I'm not sure what you mean by "double dispatchable", maybe you can elaborate this with a short example?

nameofuser1 commented 5 years ago

I meant simple visitor patter.

Imagine class hierarchy:

                                   tinyfsm::Event
                                        |
                               _________|_________
                              |                   |
                            Event1              Event2

Right now is not possible to dispatch Event1 by passing pointer to tinyfsm::Event into dispatch function. This issue visitor pattern can solve.

I met the necessity of this feature when was trying to pass events via queue to another thread and actually this can be a common approach in embedded systems. However after an event is extracted from the queue its' actual type is unknown and for now it is required to explicitly determine event type.

But as you said react function is not binded to tinyfsm::Event, and now I see that visitor is not applicable here )

digint commented 2 years ago

answered, added "wontfix" label (as there is nothing to fix ;-)