<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 3. Writing your own signals</title><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><link rel="home" href="index.html" title="libsigc++"><link rel="up" href="index.html" title="libsigc++"><link rel="prev" href="ch02s04.html" title="Disconnecting"><link rel="next" href="ch03s02.html" title="What about return values?"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 3. Writing your own signals</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02s04.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch03s02.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="sec-writing"></a>Chapter 3. Writing your own signals</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="sect1"><a href="ch03.html#idm45600057306336">Quick recap</a></span></dt><dt><span class="sect1"><a href="ch03s02.html">What about return values?</a></span></dt></dl></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="idm45600057306336"></a>Quick recap</h2></div></div></div><p>If all you want to do is use gtkmm, and connect your functionality to its
signals, you can probably stop reading here.</p><p>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.</p><p>We've already covered the way the types of signals are made up, but lets
recap:</p><p>A signal is an instance of a template, named <code class="literal">sigc::signal</code>.
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.</p><p>To provide a signal for people to connect to, you must make available an
instance of that <code class="literal">sigc::signal</code>. In <code class="literal">AlienDetector</code> 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.)</p><p>Once you've done this, all you have to do is emit the signal when you're
ready. Look at the code for <code class="literal">AlienDetector::run()</code>:</p><pre class="programlisting">
void AlienDetector::run()
{
sleep(3); // wait for aliens
signal_detected.emit(); // panic!
}
</pre><p>As a shortcut, <code class="literal">sigc::signal</code> defines <code class="literal">operator()</code> as a synonym for
<code class="literal">emit()</code>, so you could just write <code class="literal">signal_detected();</code> as in the second
example version:</p><pre class="programlisting">
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 afterall.
}
</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02s04.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch03s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Disconnecting </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> What about return values?</td></tr></table></div></body></html>