]> git.tdb.fi Git - ext/sigc++-2.0.git/blob - untracked/docs/manual/html/chapter-connecting.html
Import libsigc++ 2.10.8 sources
[ext/sigc++-2.0.git] / untracked / docs / manual / html / chapter-connecting.html
1 <html>
2 <head>
3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4 <title>Chapter 2. Connecting your code to signals</title>
5 <meta name="generator" content="DocBook XSL Stylesheets Vsnapshot">
6 <link rel="home" href="index.html" title="libsigc++">
7 <link rel="up" href="index.html" title="libsigc++">
8 <link rel="prev" href="chapter-introduction.html" title="Chapter 1. Introduction">
9 <link rel="next" href="sect-using-mem-func.html" title="Using a member function">
10 </head>
11 <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
12 <div class="navheader">
13 <table width="100%" summary="Navigation header">
14 <tr><th colspan="3" align="center">Chapter 2. Connecting your code to signals</th></tr>
15 <tr>
16 <td width="20%" align="left">
17 <a accesskey="p" href="chapter-introduction.html">Prev</a> </td>
18 <th width="60%" align="center"> </th>
19 <td width="20%" align="right"> <a accesskey="n" href="sect-using-mem-func.html">Next</a>
20 </td>
21 </tr>
22 </table>
23 <hr>
24 </div>
25 <div class="chapter">
26 <div class="titlepage"><div><div><h1 class="title">
27 <a name="chapter-connecting"></a>Chapter 2. Connecting your code to signals</h1></div></div></div>
28 <div class="toc">
29 <p><b>Table of Contents</b></p>
30 <ul class="toc">
31 <li><span class="section"><a href="chapter-connecting.html#sect-simple-ex">A simple example</a></span></li>
32 <li><span class="section"><a href="sect-using-mem-func.html">Using a member function</a></span></li>
33 <li><span class="section"><a href="sect-signals-with-pars.html">Signals with parameters</a></span></li>
34 <li><span class="section"><a href="sect-disconnecting.html">Disconnecting</a></span></li>
35 </ul>
36 </div>
37
38
39 <div class="section">
40 <div class="titlepage"><div><div><h2 class="title" style="clear: both">
41 <a name="sect-simple-ex"></a>A simple example</h2></div></div></div>
42
43
44         <p>So to get some experience, lets look at a simple example...</p>
45
46         <p>Lets say you and I are writing an application which informs the user when
47         aliens land in the car park. To keep the design nice and clean, and allow for
48         maximum portability to different interfaces, we decide to use libsigc++ to
49         split the project in two parts.</p>
50
51         <p>I will write the <code class="literal">AlienDetector</code> class, and you will write the code to inform
52         the user. (Well, OK, I'll write both, but we're pretending, remember?)</p>
53
54         <p>Here's my class:</p>
55
56 <pre class="programlisting">
57 class AlienDetector
58 {
59 public:
60     AlienDetector();
61
62     void run();
63
64     sigc::signal&lt;void()&gt; signal_detected;
65 };
66 </pre>
67
68                 <p>(I'll explain the type of signal_detected later.)</p>
69
70                 <p>Here's your code that uses it:</p>
71
72 <pre class="programlisting">
73 void warn_people()
74 {
75     std::cout &lt;&lt; "There are aliens in the carpark!" &lt;&lt; std::endl;
76 }
77
78 int main()
79 {
80     AlienDetector mydetector;
81     mydetector.signal_detected.connect( sigc::ptr_fun(warn_people) );
82
83     mydetector.run();
84
85     return 0;
86 }
87 </pre>
88
89   <p>You can use a lambda expression instead of sigc::ptr_fun().</p>
90 <pre class="programlisting">
91     mydetector.signal_detected.connect( [](){ warn_people(); } );
92 </pre>
93
94         <p>Pretty simple really - you call the <code class="literal">connect()</code> method on the signal to
95         connect your function. <code class="literal">connect()</code> takes a <code class="literal">slot</code> parameter (remember slots
96         are capable of holding any type of callback), so you convert your
97         <code class="literal">warn_people()</code> function to a slot using the <code class="literal">slot()</code> function.</p>
98
99         <p>To compile this example, use:</p>
100         <pre class="programlisting">g++ example1.cc -o example1 `pkg-config --cflags --libs sigc++-2.0`</pre>
101         <p>Note that those `` characters are backticks, not single quotes. Run it with</p>
102         <pre class="programlisting">./example1</pre>
103         <p>(Try not to panic when the aliens land!)</p>
104
105 </div>
106
107
108
109
110
111
112 </div>
113 <div class="navfooter">
114 <hr>
115 <table width="100%" summary="Navigation footer">
116 <tr>
117 <td width="40%" align="left">
118 <a accesskey="p" href="chapter-introduction.html">Prev</a> </td>
119 <td width="20%" align="center"> </td>
120 <td width="40%" align="right"> <a accesskey="n" href="sect-using-mem-func.html">Next</a>
121 </td>
122 </tr>
123 <tr>
124 <td width="40%" align="left" valign="top">Chapter 1. Introduction </td>
125 <td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td>
126 <td width="40%" align="right" valign="top"> Using a member function</td>
127 </tr>
128 </table>
129 </div>
130 </body>
131 </html>