[C++] Looking for feedback on my event system

Started by
44 comments, last by _the_phantom_ 12 years, 6 months ago
You could always create a wrapper object that allows you do uniquely identify the callbacks:

struct EventCallback
{
int id;
std::function callback;
};

However then you'd probably use a handle pattern, where the dispatcher allocates a unique ID that gets returned on registration, and the user holds on to that ID and uses it later for unregistration.Not necessarily ideal, but it does work without adding too much complexity to the implementation.
Advertisement

I'm now switched over to a design very similar to the one posted by phantom, with my own modifications here and there, but have run into a problem: unregistering listeners. Before it was as simple as iterating through a vector of listeners and removing the object that was the same as the one passed into the function. Now that is not possible because std::functions aren't comparable. I've a few ideas on how to solve this, but they add some annoying layer of complexity that I really don't want. Any ideas?

http://www.codeproject.com/KB/cpp/FastDelegate.aspx

Those are comparable if that's of any help... I've always had great success with them.

[font="arial, verdana, tahoma, sans-serif"] [/font]
[font="arial, verdana, tahoma, sans-serif"]You could always create a wrapper object that allows you do uniquely identify the callbacks:[/font]
[font=arial, verdana, tahoma, sans-serif]

[/font]
[font=arial, verdana, tahoma, sans-serif]

struct EventCallback[/font]
[font=arial, verdana, tahoma, sans-serif]

{[/font]
int id;
std::function callback;
};


However then you'd probably use a handle pattern, where the dispatcher allocates a unique ID that gets returned on registration, and the user holds on to that ID and uses it later for unregistration.Not necessarily ideal, but it does work without adding too much complexity to the implementation.

Yeah, I thought of something similar and it is definitely not ideal. I think this is where the observer pattern using interfaces has the upper hand; It's nice to be able to just pass the object in to unregister.



http://www.codeproje...stDelegate.aspx

Those are comparable if that's of any help... I've always had great success with them.

I've seen this, but only because I saw this first and it is a response to that. I would probably use the "Impossibly Fast Delegates" if I were going to forgo std::function.
Well, those aren't comparable wink.gif You may be surprised how well it works!
Thanks for all the input everyone. I think my event system is at a good place now. It is integrating smoothly into my applications. This is a result of messing around with my currently implemented event system. smile.gif
No worries; was an intresting discussion and gave me some stuff to think about/test with regards to my own Awesome Plans™ in this area :D

Intresting sidenote; I was watching a recent //Build presentation on C++ and WinRT and it seems that MS have come up with the same 'solution' as I did when it comes to binding events in C++. When they add a C++ delegate to an WinRT event you get given back a token and you use that token to unregister your delegate later. Much the same as I proposed both here and the other thread on Event systems :D

This topic is closed to new replies.

Advertisement