]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - untracked/docs/manual/html/sect-signals-with-pars.html
Import libsigc++ 2.10.8 sources
[ext/sigc++-2.0.git] / untracked / docs / manual / html / sect-signals-with-pars.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>Signals with parameters</title>
5 <meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
6 <link rel="home" href="index.html" title="libsigc++">
7 <link rel="up" href="chapter-connecting.html" title="Chapter 2. Connecting your code to signals">
8 <link rel="prev" href="sect-using-mem-func.html" title="Using a member function">
9 <link rel="next" href="sect-disconnecting.html" title="Disconnecting">
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">Signals with parameters</th></tr>
15 <tr>
16 <td width="20%" align="left">
17 <a accesskey="p" href="sect-using-mem-func.html">Prev</a> </td>
18 <th width="60%" align="center">Chapter 2. Connecting your code to signals</th>
19 <td width="20%" align="right"> <a accesskey="n" href="sect-disconnecting.html">Next</a>
20 </td>
21 </tr>
22 </table>
23 <hr>
24 </div>
25 <div class="section">
26 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
27 <a name="sect-signals-with-pars"></a>Signals with parameters</h2></div></div></div>
28
29
30         <p>Functions taking no parameters and returning void are quite useful,
31         especially when they're members of classes that can store unlimited amounts of
32         safely typed data, but they're not sufficient for everything.</p>
33
34         <p>What if aliens don't land in the carpark, but somewhere else? Let's modify
35         the example so that the callback function takes a <code class="literal">std::string</code> with the location
36         in which aliens were detected.</p>
37
38         <p>I change my class to:</p>
39
40 <pre class="programlisting">
41 class AlienDetector
42 {
43 public:
44     AlienDetector();
45
46     void run();
47
48     sigc::signal&lt;void(std::string)&gt; signal_detected;      // changed
49 };
50 </pre>
51
52         <p>The only line I had to change was the signal line (in <code class="literal">run()</code> I need to change
53         my code to supply the argument when I emit the signal too, but that's not shown
54         here).</p>
55
56         <p>The name of the type is '<code class="literal">sigc::signal</code>'.
57         The template parameters are the return type, then the argument types in parentheses.
58         (libsigc++2 also accepts a different syntax, with a comma between the return type
59         and the parameter types. That syntax is deprecated, though.)</p>
60
61         <p>The types in the function signature are in the same order as the template
62         parameters, eg:</p>
63
64 <pre class="programlisting">
65 sigc::signal&lt;void(std::string)&gt;
66     void function(std::string foo);
67 </pre>
68
69                 <p>So now you can update your alerter (for simplicity, lets go back to the
70                 free-standing function version):</p>
71
72 <pre class="programlisting">
73 void warn_people(std::string where)
74 {
75     std::cout &lt;&lt; "There are aliens in " &lt;&lt; where &lt;&lt; "!" &lt;&lt; std::endl;
76 }
77
78 int main()
79 {
80     AlienDetector mydetector;
81     mydetector.signal_detected.connect( sigc::ptr_fun(warn_people) );
82
83     mydetector.run();
84
85     return 0;
86 }
87 </pre>
88
89         <p>Easy.</p>
90 </div>
91 <div class="navfooter">
92 <hr>
93 <table width="100%" summary="Navigation footer">
94 <tr>
95 <td width="40%" align="left">
96 <a accesskey="p" href="sect-using-mem-func.html">Prev</a> </td>
97 <td width="20%" align="center"><a accesskey="u" href="chapter-connecting.html">Up</a></td>
98 <td width="40%" align="right"> <a accesskey="n" href="sect-disconnecting.html">Next</a>
99 </td>
100 </tr>
101 <tr>
102 <td width="40%" align="left" valign="top">Using a member function </td>
103 <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
104 <td width="40%" align="right" valign="top"> Disconnecting</td>
105 </tr>
106 </table>
107 </div>
108 </body>
109 </html>