Here is my situation:
I have an event driven system, where all my handlers are derived from IHandler
class, and implement an onEvent(const Event &event)
method. Now, Event is a base class for all events and contains only the enumerated event type. All actual events are derived from it, including the EventKey
event, which has 2 fields: (uchar) keyCode
and (bool) isDown
.
Here's the interesting part: I generate an EventKey
event using the following syntax:
Event evt = EventKey(15, true);
and I ship it to the handlers:
EventDispatch::sendEvent(evt); // void EventDispatch::sendEvent(const Event &event);
(EventDispatch
contains a linked list of IHandlers
and calls their onEvent(const Event &event)
method with the parameter containing the sent event.
Now the actual question:
Say I want my handlers to poll the events in a queue of type Event
, how do I do that?
Dynamic pointers with reference counting sound like too big of a solution.
Making copies is more difficult than it sounds, since I'm only receiving a reference to a base type, therefore each time I would need to check the type of event, upcast to
EventKey
and then make a copy to store in a queue. Sounds like the only solution - but is unpleasant since I would need to know every single type of event and would have to check that for every event received - sounds like a bad plan.I could allocate the events dynamically and then send around pointers to those events, enqueue them in the array if wanted - but other than having reference counting - how would I be able to keep track of that memory? Do you know any way to implement a very light reference counter that wouldn't interfere with the user?
What do you think would be a good solution to this design?