Sophie

Sophie

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

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>Signals with parameters</title><meta name="generator" content="DocBook XSL Stylesheets V1.75.2" /><link rel="home" href="index.html" title="LibSigC++" /><link rel="up" href="ch02.html" title="Chapter 2. Connecting your code to signals" /><link rel="prev" href="ch02s02.html" title="Using a member function" /><link rel="next" href="ch02s04.html" title="Disconnecting" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Signals with parameters</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02s02.html">Prev</a> </td><th width="60%" align="center">Chapter 2. Connecting your code to signals</th><td width="20%" align="right"> <a accesskey="n" href="ch02s04.html">Next</a></td></tr></table><hr /></div><div class="sect1" title="Signals with parameters"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id3046932"></a>Signals with parameters</h2></div></div></div><p>Functions taking no parameters and returning void are quite useful,
	especially when they're members of classes that can store unlimited amounts of
	safely typed data, but they're not sufficient for everything.</p><p>What if aliens don't land in the carpark, but somewhere else? Let's modify
	the example so that the callback function takes a <code class="literal">std::string</code> with the location
	in which aliens were detected.</p><p>I change my class to:</p><pre class="programlisting">
class AlienDetector
{
public:
    AlienDetector();

    void run();

    SigC::Signal1&lt;void, std::string&gt; detected;	// changed
};
</pre><p>The only line I had to change was the signal line (in <code class="literal">run()</code> I need to change
	my code to supply the argument when I emit the signal too, but that's not shown
	here).</p><p>The name of the type is '<code class="literal">SignalN</code>', where N is the number of arguments that
	the slots should take. The template parameters are the return type, then the
	argument types.</p><p>Obviously LibSigC++ doesn't define an infinite number of <code class="literal">Signal</code> templates,
	but it does define <code class="literal">Signal0</code>-<code class="literal">Signal5</code>, which should be enough for most people. (If
	you know M4, you can tweak it to provide more if you really need to.)</p><p>The types in the function signature are in the same order as the template
	parameters, eg:</p><pre class="programlisting">
SigC::Signal1&lt;void,         std::string&gt;
              void function(std::string foo);
</pre><p>So now you can update your alerter (for simplicity, lets go back to the
		free-standing function version):</p><pre class="programlisting">
void warn_people(std::string where)
{
    cout &lt;&lt; "There are aliens in " &lt;&lt; where &lt;&lt; "!" &lt;&lt; endl;
}

int main()
{
    AlienDetector mydetector;
    mydetector.detected.connect( SigC::slot(warn_people) );

    mydetector.run();

    return 0;
}
</pre><p>Easy.</p></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02s02.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="ch02.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="ch02s04.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Using a member function </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Disconnecting</td></tr></table></div></body></html>