1 <html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter 4. Advanced topics</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="libsigc++"><link rel="up" href="index.html" title="libsigc++"><link rel="prev" href="ch03s02.html" title="What about return values?"><link rel="next" href="ch04s02.html" title="Retyping"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 4. Advanced topics</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch03s02.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch04s02.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="sec-advanced"></a>Chapter 4. Advanced topics</h1></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="sect1"><a href="ch04.html#idp67843968">Rebinding</a></span></dt><dt><span class="sect1"><a href="ch04s02.html">Retyping</a></span></dt></dl></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="idp67843968"></a>Rebinding</h2></div></div></div><p>Suppose you already have a function that you want to be called when a
2 signal is emitted, but it takes the wrong argument types. For example, lets try
3 to attach the <code class="literal">warn_people(std::string)</code> function to the detected signal
4 from the first example, which didn't supply a location string.</p><p>Just trying to connect it with:</p><pre class="programlisting">
5 myaliendetector.signal_detected.connect(sigc::ptr_fun(warn_people));
6 </pre><p>results in a compile-time error, because the types don't match. This is good!
7 This is typesafety at work. In the C way of doing things, this would have just
8 died at runtime after trying to print a random bit of memory as the location -
9 ick!</p><p>We have to make up a location string, and bind it to the function, so that
10 when signal_detected is emitted with no arguments, something adds it in before
11 <code class="literal">warn_people</code> is actually called.</p><p>We could write it ourselves - it's not hard:</p><pre class="programlisting">
12 void warn_people_wrapper() // note this is the signature that 'signal_detected' expects
14 warn_people("the carpark");
16 </pre><p>but after our first million or so we might start looking for a better way. As
17 it happens, libsigc++ has one.</p><pre class="programlisting">
18 sigc::bind(slot, arg);
19 </pre><p>binds arg as the argument to slot, and returns a new slot of the same return
20 type, but with one fewer arguments.</p><p>Now we can write:</p><pre class="programlisting">
21 myaliendetector.signal_detected.connect(sigc::bind( sigc::ptr_fun(warn_people), "the carpark" ) );
22 </pre><p>If the input slot has multiple args, the rightmost one is bound.</p><p>The return type can also be bound with <code class="literal">sigc::bind_return(slot, returnvalue);</code> though
23 this is not so commonly useful.</p><p>So if we can attach the new <code class="literal">warn_people()</code> to the old detector, can we attach
24 the old <code class="literal">warn_people</code> (the one that didn't take an argument) to the new detector?</p><p>Of course, we just need to hide the extra argument. This can be done with
25 <code class="literal">sigc::hide</code>, eg.</p><pre class="programlisting">
26 myaliendetector.signal_detected.connect( sigc::hide<std::string>( sigc::ptr_fun(warn_people) ) );
27 </pre><p>The template arguments are the types to hide (from the right only - you can't
28 hide the first argument of 3, for example, only the last).</p><p><code class="literal">sigc::hide_return</code> effectively makes the return type void.</p></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch03s02.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch04s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">What about return values? </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Retyping</td></tr></table></div></body></html>