Chapter 3. Writing your own signals

Table of Contents

Quick recap

If all you want to do is use gtkmm, and connect your functionality to its signals, you can probably stop reading here.

You might benefit from reading on anyway though, as this section is going to be quite simple, and the 'Rebinding' technique from the next section is occasionally useful.

We've already covered the way the types of signals are made up, but lets recap:

A signal is an instance of a template, named sigc::signal. The template arguments are the types, in the order they appear in the function signature that can be connected to that signal; that is the return type, then the argument types in parentheses.

To provide a signal for people to connect to, you must make available an instance of that sigc::signal. In AlienDetector this was done with a public data member. That's not considered good practice usually, so you might want to consider making a member function that returns the signal by reference. (This is what gtkmm does.)

Once you've done this, all you have to do is emit the signal when you're ready. Look at the code for AlienDetector::run():

void AlienDetector::run()
{
    sleep(3); // wait for aliens
    signal_detected.emit(); // panic!
}

As a shortcut, sigc::signal defines operator() as a synonym for emit(), so you could just write signal_detected(); as in the second example version:

void AlienDetector::run()
{
    sleep(3);                // wait for aliens
    signal_detected("the carpark"); // this is the std::string version, looks like
                             // they landed in the carpark after all.
}