Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Events in C++

Tags:

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.

like image 859
Robert Avatar asked Oct 26 '08 22:10

Robert


People also ask

What do you mean by event in C?

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.

What is event in C# with example?

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.

What is an event in programming terms?

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.

What is an event loop in C?

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.


2 Answers

Take a look at the boost signal library. Combined with the function and bind libraries, you can do exactly what you are looking for.

like image 153
Jere.Jones Avatar answered Sep 26 '22 01:09

Jere.Jones


I use sigslot for exactly this purpose.

like image 41
Jim Buck Avatar answered Sep 27 '22 01:09

Jim Buck