Sophie

Sophie

distrib > Fedora > 14 > x86_64 > by-pkgid > 65cedb422b5b18cbc6c81220baa7524d > files > 52

libsigc++-devel-1.2.7-8.fc12.i686.rpm

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>Chapter 4. Advanced topics</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><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><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" title="Chapter 4. Advanced topics"><div class="titlepage"><div><div><h2 class="title"><a id="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#id3047308">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" title="Rebinding"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id3047308"></a>Rebinding</h2></div></div></div><p>Suppose you already have a function that you want to be called back 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.detected.connect(SigC::slot(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 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 '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.detected.connect(SigC::bind( SigC::slot(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">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.detected.connect( SigC::hide&lt;std::string&gt;( SigC::slot(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">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>