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">
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>
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>
26 <div class="titlepage"><div><div><h1 class="title">
27 <a name="chapter-advanced"></a>Chapter 4. Advanced topics</h1></div></div></div>
29 <p><b>Table of Contents</b></p>
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>
38 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
39 <a name="sect-rebinding"></a>Rebinding</h2></div></div></div>
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>
47 <p>Just trying to connect it with:</p>
49 <pre class="programlisting">
50 myaliendetector.signal_detected.connect(sigc::ptr_fun(warn_people));
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 -
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>
63 <pre class="programlisting">
64 void warn_people_wrapper() // note this is the signature that 'signal_detected' expects
66 warn_people("the carpark");
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>
73 <pre class="programlisting">
74 sigc::bind(slot, arg);
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>
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" ) );
85 <p>If the input slot has multiple args, the rightmost one is bound.</p>
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>
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>
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>
96 <pre class="programlisting">
97 myaliendetector.signal_detected.connect( sigc::hide<std::string>( sigc::ptr_fun(warn_people) ) );
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>
103 <p><code class="literal">sigc::hide_return</code> effectively makes the return type void.</p>
108 <div class="navfooter">
110 <table width="100%" summary="Navigation footer">
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>
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>