File: ch04.html

package info (click to toggle)
libsigc%2B%2B-2.0 2.0.10-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 20,220 kB
  • ctags: 6,376
  • sloc: sh: 8,402; cpp: 1,717; xml: 368; makefile: 364; perl: 51; ansic: 45
file content (28 lines) | stat: -rw-r--r-- 4,601 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Chapter4.Advanced topics</title><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="LibSigC++"><link rel="up" href="index.html" title="LibSigC++"><link rel="previous" 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">Chapter4.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>Chapter4.Advanced topics</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch04.html#id2451369">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="id2451369"></a>Rebinding</h2></div></div><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 <tt class="literal">warn_people(std::string)</tt> 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
	<tt class="literal">warn_people</tt> 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 <tt class="literal">sigc::bind_return(slot, returnvalue);</tt> though
	this is not so commonly useful.</p><p>So if we can attach the new <tt class="literal">warn_people()</tt> to the old detector, can we attach
	the old <tt class="literal">warn_people</tt> (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
	<tt class="literal">sigc::hide</tt>, 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><tt class="literal">sigc::hide_return</tt> 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"><a accesskey="u" href="index.html">Up</a></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>