]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - untracked/docs/manual/html/chapter-writing.html
Import libsigc++ 2.10.8 sources
[ext/sigc++-2.0.git] / untracked / docs / manual / html / chapter-writing.html
1 <html>
2 <head>
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?">
10 </head>
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>
15 <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>
20 </td>
21 </tr>
22 </table>
23 <hr>
24 </div>
25 <div class="chapter">
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>
28 <div class="toc">
29 <p><b>Table of Contents</b></p>
30 <ul class="toc">
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>
33 </ul>
34 </div>
35
36
37 <div class="section">
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>
40
41
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>
44
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>
48
49         <p>We've already covered the way the types of signals are made up, but lets
50         recap:</p>
51
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>
56
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>
62
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>
65
66 <pre class="programlisting">
67 void AlienDetector::run()
68 {
69     sleep(3); // wait for aliens
70     signal_detected.emit(); // panic!
71 }
72 </pre>
73
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
76         example version:</p>
77
78 <pre class="programlisting">
79 void AlienDetector::run()
80 {
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.
84 }
85 </pre>
86 </div>
87
88
89 </div>
90 <div class="navfooter">
91 <hr>
92 <table width="100%" summary="Navigation footer">
93 <tr>
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>
98 </td>
99 </tr>
100 <tr>
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>
104 </tr>
105 </table>
106 </div>
107 </body>
108 </html>