Jump to content
  • Advertisement
Sign in to follow this  
Bad Ham

Event System Questions

This topic is 4075 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!