Sign in to follow this  
Bad Ham

Event System Questions

Recommended Posts

Hi all! I'm working on the event system for my engine right now, and though I've been doing well up until now, I'm stuck on a bit of an architectural issue that I was hoping someone could comment on. I'm designing the EventManager, which will receive events into a queue, and upon calling process() will forward the data of those events to registered listeners. Right now the way this happens is thus: There is a hash_map using the event ID as the key, and holding a std::list of IEventListeners for each key. IEventListeners is an interface that defines another hash_map of event IDs to function pointers and a function (getHandler(ID)) to retrieve a function pointer based on ID. When implemented by a class that needs to listen for events, the hash_map is initialized in the constructor. So when processing an event, the EventManager will lookup the ID in the first hash table. For each one of the listeners returned, it will call getHandler(ID)(event data) to execute the handler. Essentially there are two hash_table lookups before a function call. I'm aware of another method that would remove the second hash_table, simply mapping IDs to a std::list of function pointers in the EventManager. This way the Manager would just perform a lookup in the hash_map by ID, and execute all the function pointers listed there. Now, my thoughts for doing it this way were that the concepts of message passing vs. event-handling would be more encapsulated. All the event-handling concepts would be inside the listener's definition, and nothing would be exposed outside of it. If you registered a listener, you wouldn't have to know about the underlying functions that handle each message, only the name of the listener. Does this make sense? Or am I just adding an extra layer of unnecessary complexity? I'd really appreciate any comments or suggestions.

Share this post


Link to post
Share on other sites
I did a bit of snooping around in the code...

I guess the method I mentioned only adds another step that is O(1) complexity since it's a hash lookup, so I imagine the only concern would be extra memory allocation.

Is having this extra allocation worth it to keep the handlers encapsulated within the listener subclass?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this