Sophie

Sophie

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

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 2. Connecting your code to signals</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="ch01.html" title="Chapter 1. Introduction" /><link rel="next" href="ch02s02.html" title="Using a member function" /></head><body><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter 2. Connecting your code to signals</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch01.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <a accesskey="n" href="ch02s02.html">Next</a></td></tr></table><hr /></div><div class="chapter" title="Chapter 2. Connecting your code to signals"><div class="titlepage"><div><div><h2 class="title"><a id="sec-connecting"></a>Chapter 2. Connecting your code to signals</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch02.html#id3046727">A simple example</a></span></dt><dt><span class="sect1"><a href="ch02s02.html">Using a member function</a></span></dt><dt><span class="sect1"><a href="ch02s03.html">Signals with parameters</a></span></dt><dt><span class="sect1"><a href="ch02s04.html">Disconnecting</a></span></dt></dl></div><div class="sect1" title="A simple example"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a id="id3046727"></a>A simple example</h2></div></div></div><p>The terminology for signals and slots has come under some
	criticism for not being as intuitive as it maybe could have been, but with a little
	experience it won't cause a problem. Honest.</p><p>So to get some experience, lets look at a simple example...</p><p>Lets say you and I are writing an application which informs the user when
	aliens land in the car park. To keep the design nice and clean, and allow for
	maximum portability to different interfaces, we decide to use LibSigC++ to
	split the project in two parts.</p><p>I will write the <code class="literal">AlienDetector</code> class, and you will write the code to inform
	the user. (Well, OK, I'll write both, but we're pretending, remember?)</p><p>Here's my class:</p><pre class="programlisting">
class AlienDetector
{
public:
    AlienDetector();

    void run();

    SigC::Signal0&lt;void&gt; detected;
};
</pre><p>(I'll explain the type of detected later.)</p><p>Here's your code that uses it:</p><pre class="programlisting">
void warn_people()
{
    cout &lt;&lt; "There are aliens in the carpark!" &lt;&lt; endl;
}

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

    mydetector.run();

    return 0;
}
</pre><p>Pretty simple really - you call the <code class="literal">connect()</code> method on the signal to
	connect your function. <code class="literal">connect()</code> takes a <code class="literal">slot</code> parameter (remember slots
	are capable of holding any type of callback), so you convert your
	<code class="literal">warn_people()</code> function to a slot using the <code class="literal">slot()</code> function.</p><p>To compile this example from the downloadable example code, use:</p><pre class="programlisting">g++ example1.cc -o eg1 `pkg-config --cflags --libs sigc++-1.2`</pre><p>Note that those `` characters are backticks, not single quotes. Run it with</p><pre class="programlisting">./eg1</pre><p>(Try not to panic when the aliens land!)</p></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch01.html">Prev</a> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <a accesskey="n" href="ch02s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 1. Introduction </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Using a member function</td></tr></table></div></body></html>