File: ch03.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-- 3,935 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>Chapter3.Writing your own signals</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="ch02s04.html" title="Disconnecting"><link rel="next" href="ch03s02.html" title="What about return values?"></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">Chapter3.Writing your own signals</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch02s04.html">Prev</a></td><th width="60%" align="center"></th><td width="20%" align="right"><a accesskey="n" href="ch03s02.html">Next</a></td></tr></table><hr></div><div class="chapter" lang="en"><div class="titlepage"><div><div><h2 class="title"><a name="sec-writing"></a>Chapter3.Writing your own signals</h2></div></div><div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="ch03.html#id2447554">Quick recap</a></span></dt><dt><span class="sect1"><a href="ch03s02.html">What about return values?</a></span></dt></dl></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2447554"></a>Quick recap</h2></div></div><div></div></div><p>If all you want to do is use gtkmm, and connect your functionality to its
	signals, you can probably stop reading here.</p><p>You might benefit from reading on anyway though, as this section is going to
	be quite simple, and the 'Rebinding' technique from the next section is
	occasionally useful.</p><p>We've already covered the way the types of signals are made up, but lets
	recap:</p><p>A signal is an instance of a template, named <tt class="literal">sigc::signal</tt>. 
        The template arguments are the types,
	in the order they appear in the function signature that can be connected to that
	signal; that is the return type, then the argument types.</p><p>To provide a signal for people to connect to, you must make available an
	instance of that <tt class="literal">sigc::signal</tt>. In <tt class="literal">AlienDetector</tt> this was done
	with a public data member. That's not considered good practice usually, so you
	might want to consider making a member function that returns the signal by
	reference. (This is what gtkmm does.)</p><p>Once you've done this, all you have to do is emit the signal when you're
	ready. Look at the code for <tt class="literal">AlienDetector::run()</tt>:</p><pre class="programlisting">
void AlienDetector::run()
{
    sleep(3); // wait for aliens
    signal_detected.emit(); // panic!
}
</pre><p>As a shortcut, <tt class="literal">sigc::signal</tt> defines <tt class="literal">operator()</tt> as a synonym for
	<tt class="literal">emit()</tt>, so you could just write <tt class="literal">signal_detected();</tt> as in the second
	example version:</p><pre class="programlisting">
void AlienDetector::run()
{
    sleep(3);                // wait for aliens
    signal_detected("the carpark"); // this is the std::string version, looks like
       	                     // they landed in the carpark afterall.
}
</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch02s04.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="ch03s02.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Disconnecting</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">What about return values?</td></tr></table></div></body></html>