I'm currently trying to implement an event-system following a broadcast/observer scheme. I have events/notifications that contain information about what is happening:
class Event
{
public:
Event(const std::string& _name="event");
~Event() = default;
protected:
std::string name;
};
/* I want to flexibly extend my Event base-class to accommodate for more involved behavior */
// example
class InputEvent : public Event
{
public:
InputEvent(const std::string& _name="input_event");
~InputEvent() = default;
uint keycode; // or whatever...
};
and event handlers that manage to whom to broadcast these events and what to do if they themselves receive one:
class EventHandler
{
public:
EventHandler() = default;
~EventHandler() = default;
/* React to incoming events (can/shall be overloaded for different subtypes of Event) */
virtual void event_callback(const std::shared_ptr<Event> event);
/* Broadcast events to recipients */
template<typename EventType>
void event_dispatch(const std::shared_ptr<EventType> event);
protected:
// Addresses of all event recipients
std::set<std::shared_ptr<EventHandler>> event_recipients;
// Possible (???) implementation of event queue
std::queue<std::shared_ptr<Event>> event_queue;
};
The event_queue
acts as a buffer to store received events until they're scheduled to be processed by event_callback
. However with my current implementation all of my events stored in this buffer will be upcasted into base-class Event
pointers and thus render the overloads that different EventHandler
derivatives may posses useless.
Is there a better container than a Queue or should I consider dynamic casting (and if so how)? Or do you have an altogether superior design in mind?