3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>Chapter 3. Writing your own signals</title>
5 <meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
6 <link rel="home" href="index.html" title="libsigc++">
7 <link rel="up" href="index.html" title="libsigc++">
8 <link rel="prev" href="sect-disconnecting.html" title="Disconnecting">
9 <link rel="next" href="sect-return-values.html" title="What about return values?">
11 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
12 <div class="navheader">
13 <table width="100%" summary="Navigation header">
14 <tr><th colspan="3" align="center">Chapter 3. Writing your own signals</th></tr>
16 <td width="20%" align="left">
17 <a accesskey="p" href="sect-disconnecting.html">Prev</a> </td>
18 <th width="60%" align="center"> </th>
19 <td width="20%" align="right"> <a accesskey="n" href="sect-return-values.html">Next</a>
26 <div class="titlepage"><div><div><h1 class="title">
27 <a name="chapter-writing"></a>Chapter 3. Writing your own signals</h1></div></div></div>
29 <p><b>Table of Contents</b></p>
31 <li><span class="section"><a href="chapter-writing.html#sect-quick-recap">Quick recap</a></span></li>
32 <li><span class="section"><a href="sect-return-values.html">What about return values?</a></span></li>
38 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
39 <a name="sect-quick-recap"></a>Quick recap</h2></div></div></div>
42 <p>If all you want to do is use gtkmm, and connect your functionality to its
43 signals, you can probably stop reading here.</p>
45 <p>You might benefit from reading on anyway though, as this section is going to
46 be quite simple, and the 'Rebinding' technique from the next section is
47 occasionally useful.</p>
49 <p>We've already covered the way the types of signals are made up, but lets
52 <p>A signal is an instance of a template, named <code class="literal">sigc::signal</code>.
53 The template arguments are the types,
54 in the order they appear in the function signature that can be connected to that
55 signal; that is the return type, then the argument types in parentheses.</p>
57 <p>To provide a signal for people to connect to, you must make available an
58 instance of that <code class="literal">sigc::signal</code>. In <code class="literal">AlienDetector</code> this was done
59 with a public data member. That's not considered good practice usually, so you
60 might want to consider making a member function that returns the signal by
61 reference. (This is what gtkmm does.)</p>
63 <p>Once you've done this, all you have to do is emit the signal when you're
64 ready. Look at the code for <code class="literal">AlienDetector::run()</code>:</p>
66 <pre class="programlisting">
67 void AlienDetector::run()
69 sleep(3); // wait for aliens
70 signal_detected.emit(); // panic!
74 <p>As a shortcut, <code class="literal">sigc::signal</code> defines <code class="literal">operator()</code> as a synonym for
75 <code class="literal">emit()</code>, so you could just write <code class="literal">signal_detected();</code> as in the second
78 <pre class="programlisting">
79 void AlienDetector::run()
81 sleep(3); // wait for aliens
82 signal_detected("the carpark"); // this is the std::string version, looks like
83 // they landed in the carpark after all.
90 <div class="navfooter">
92 <table width="100%" summary="Navigation footer">
94 <td width="40%" align="left">
95 <a accesskey="p" href="sect-disconnecting.html">Prev</a> </td>
96 <td width="20%" align="center"> </td>
97 <td width="40%" align="right"> <a accesskey="n" href="sect-return-values.html">Next</a>
101 <td width="40%" align="left" valign="top">Disconnecting </td>
102 <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
103 <td width="40%" align="right" valign="top"> What about return values?</td>