I'm not sure how to look for this online... I think they might be called something different in C++
I want to have a simple event system, somthing like
event myCustomEvent; myCustomEvent.subscribe( void myHandler(string) ); myCustomEvent.fire("a custom argument"); // myHandler prints out the string passed in the first argument event myNewCustomEvent; myNewCustomEvent.subscribe( void myNewHandler(int) ); myNewCustomEvent.fire(10); // myHandler prints 10
I can do this pretty easily with a simple class -- but when i want to have an event that passes a different type or amount of arguments to the subscriber i have to write, and define an entirely new event class.. I figure there has to be some library, or maybe even something native in Visual C++ 2008 that will work something similar to this. It's basicly just an implementation of the Observer pattern, so it can't be too impossible to do in C++
This really makes me appreciate how nice it is in JavaScript not to have to worry about the arguments you are passing.
Tell me if this is a stupid question.
Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers.
Events are user actions such as key press, clicks, mouse movements, etc., or some occurrence such as system generated notifications. Applications need to respond to events when they occur. For example, interrupts.
An event, in a computing context, is an action or occurrence that can be identified by a program and has significance for system hardware or software. Events can be user-generated, such as keystrokes and mouse clicks, or system-generated, such as program loading, running out of memory and errors.
The event loop manages the queuing and execution of functions on the event loop's thread. This is in some ways a simpler concurrency model; your code never has to worry about simultaneous access by another thread because the code is organized into functions that the event loop calls serially, never in parallel.
Take a look at the boost signal library. Combined with the function and bind libraries, you can do exactly what you are looking for.
I use sigslot for exactly this purpose.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With