tobkit/libsigc++-2.2.3/docs/manual/html/ch04.html
Antonio Niño Díaz cc2d654195 Remove build artifacts of libsigc++-2.2.3
In practice, the whole folder has been removed and replaced by a fresh
copy of libsigc++-2.2.3 from here:

http://repository.timesys.com/buildsources/l/libsigc++/libsigc++-2.2.3/
2024-07-26 02:09:17 +01:00

29 lines
4.5 KiB
HTML
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<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.73.2"><link rel="start" 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" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="sec-advanced"></a>Chapter 4. Advanced topics</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch04.html#id2526253">Rebinding</a></span></dt><dt><span class="sect1"><a href="ch04s02.html">Retyping</a></span></dt><dt><span class="sect1"><a href="ch04s03.html">Marshallers</a></span></dt></dl></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2526253"></a>Rebinding</h2></div></div></div><p>Suppose you already have a function that you want to be called when a
signal is emitted, but it takes the wrong argument types. For example, lets try
to attach the <code class="literal">warn_people(std::string)</code> function to the detected signal
from the first example, which didn't supply a location string.</p><p>Just trying to connect it with:</p><pre class="programlisting">
myaliendetector.signal_detected.connect(sigc::ptr_fun(warn_people));
</pre><p>results in a compile-time error, because the types don't match. This is good!
This is typesafety at work. In the C way of doing things, this would have just
died at runtime after trying to print a random bit of memory as the location -
ick!</p><p>We have to make up a location string, and bind it to the function, so that
when signal_detected is emitted with no arguments, something adds it in before
<code class="literal">warn_people</code> is actually called.</p><p>We could write it ourselves - it's not hard:</p><pre class="programlisting">
void warn_people_wrapper() // note this is the signature that 'signal_detected' expects
{
warn_people("the carpark");
}
</pre><p>but after our first million or so we might start looking for a better way. As
it happens, LibSigC++ has one.</p><pre class="programlisting">
sigc::bind(slot, arg);
</pre><p>binds arg as the argument to slot, and returns a new slot of the same return
type, but with one fewer arguments.</p><p>Now we can write:</p><pre class="programlisting">
myaliendetector.signal_detected.connect(sigc::bind( sigc::ptr_fun(warn_people), "the carpark" ) );
</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
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
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
<code class="literal">sigc::hide</code>, eg.</p><pre class="programlisting">
myaliendetector.signal_detected.connect( sigc::hide&lt;std::string&gt;( sigc::ptr_fun(warn_people) ) );
</pre><p>The template arguments are the types to hide (from the right only - you can't
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>