]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - untracked/docs/manual/html/chapter-advanced.html
Import libsigc++ 2.10.8 sources
[ext/sigc++-2.0.git] / untracked / docs / manual / html / chapter-advanced.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>Chapter 4. Advanced topics</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-return-values.html" title="What about return values?">
9 <link rel="next" href="sect-retyping.html" title="Retyping">
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 4. Advanced topics</th></tr>
15 <tr>
16 <td width="20%" align="left">
17 <a accesskey="p" href="sect-return-values.html">Prev</a> </td>
18 <th width="60%" align="center"> </th>
19 <td width="20%" align="right"> <a accesskey="n" href="sect-retyping.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-advanced"></a>Chapter 4. Advanced topics</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-advanced.html#sect-rebinding">Rebinding</a></span></li>
32 <li><span class="section"><a href="sect-retyping.html">Retyping</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-rebinding"></a>Rebinding</h2></div></div></div>
40
41
42         <p>Suppose you already have a function that you want to be called when a
43         signal is emitted, but it takes the wrong argument types. For example, lets try
44         to attach the <code class="literal">warn_people(std::string)</code> function to the detected signal
45         from the first example, which didn't supply a location string.</p>
46
47         <p>Just trying to connect it with:</p>
48
49 <pre class="programlisting">
50 myaliendetector.signal_detected.connect(sigc::ptr_fun(warn_people));
51 </pre>
52
53         <p>results in a compile-time error, because the types don't match. This is good!
54         This is typesafety at work. In the C way of doing things, this would have just
55         died at runtime after trying to print a random bit of memory as the location -
56         ick!</p>
57
58         <p>We have to make up a location string, and bind it to the function, so that
59         when signal_detected is emitted with no arguments, something adds it in before
60         <code class="literal">warn_people</code> is actually called.</p>
61         <p>We could write it ourselves - it's not hard:</p>
62
63 <pre class="programlisting">
64 void warn_people_wrapper() // note this is the signature that 'signal_detected' expects
65 {
66     warn_people("the carpark");
67 }
68 </pre>
69
70         <p>but after our first million or so we might start looking for a better way. As
71         it happens, libsigc++ has one.</p>
72
73 <pre class="programlisting">
74 sigc::bind(slot, arg);
75 </pre>
76
77         <p>binds arg as the argument to slot, and returns a new slot of the same return
78         type, but with one fewer arguments.</p>
79
80         <p>Now we can write:</p>
81 <pre class="programlisting">
82 myaliendetector.signal_detected.connect(sigc::bind( sigc::ptr_fun(warn_people), "the carpark" ) );
83 </pre>
84
85         <p>If the input slot has multiple args, the rightmost one is bound.</p>
86
87         <p>The return type can also be bound with <code class="literal">sigc::bind_return(slot, returnvalue);</code> though
88         this is not so commonly useful.</p>
89
90         <p>So if we can attach the new <code class="literal">warn_people()</code> to the old detector, can we attach
91         the old <code class="literal">warn_people</code> (the one that didn't take an argument) to the new detector?</p>
92
93         <p>Of course, we just need to hide the extra argument. This can be done with
94         <code class="literal">sigc::hide</code>, eg.</p>
95
96 <pre class="programlisting">
97 myaliendetector.signal_detected.connect( sigc::hide&lt;std::string&gt;( sigc::ptr_fun(warn_people) ) );
98 </pre>
99
100         <p>The template arguments are the types to hide (from the right only - you can't
101         hide the first argument of 3, for example, only the last).</p>
102
103         <p><code class="literal">sigc::hide_return</code> effectively makes the return type void.</p>
104 </div>
105
106
107 </div>
108 <div class="navfooter">
109 <hr>
110 <table width="100%" summary="Navigation footer">
111 <tr>
112 <td width="40%" align="left">
113 <a accesskey="p" href="sect-return-values.html">Prev</a> </td>
114 <td width="20%" align="center"> </td>
115 <td width="40%" align="right"> <a accesskey="n" href="sect-retyping.html">Next</a>
116 </td>
117 </tr>
118 <tr>
119 <td width="40%" align="left" valign="top">What about return values? </td>
120 <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
121 <td width="40%" align="right" valign="top"> Retyping</td>
122 </tr>
123 </table>
124 </div>
125 </body>
126 </html>