Intro to boost::signal

boost::signal is the C++ equivalent to a C# delegate. Actually, make sure you’re using boost::signal2, boost::signal is deprecated.

It’s a little confusing at first, but I use the following “template” code below whenever I need to use them. Using a signal requires you have a few things set up. 1) A function that is called by the signal. 2) The signal to call the function. 3) Registering the function with the signal (the slot). 4) Firing the signal. I will show you this through the interaction between two classes.

Register the callback signal handler function.

You can, of course, change the parameters and types to your liking. But you’ll have to figure out how to use boost::bind to do so. See below for the definition of “RegisterSignalHandler”.

Creating the signal.

SignalType is just something to make the code easier to read. We really need two functions, one to fire the signal (if we want to do it this way) and one to register the signal handler. “RegisterSignalHandler” is merely called when you want to register a signal handler to be called when the signal is fired.

Creating the function to fire the signal.

Really any function that has access to “signal_” can fire the signal. In this case, I fire the signal passing in true the first call, and false the second. This calls all functions that have been “registered” (in the order in which they were registered).

Creating the registration function.

Leave a Reply

You must be logged in to post a comment.