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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Marshallers</title><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="LibSigC++"><link rel="up" href="ch04.html" title="Chapter4.Advanced topics"><link rel="previous" href="ch04s02.html" title="Retyping"><link rel="next" href="ch05.html" title="Chapter5.Reference"></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">Marshallers</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="ch04s02.html">Prev</a></td><th width="60%" align="center">Chapter4.Advanced topics</th><td width="20%" align="right"><a accesskey="n" href="ch05.html">Next</a></td></tr></table><hr></div><div class="sect1" lang="en"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="id2491779"></a>Marshallers</h2></div></div><div></div></div><p>When I first mentioned return values, I said that more advanced handling of
multiple return values was possible with <tt class="literal">Marshallers</tt>.</p><p>A Marshaller is a class that gets fed all the return values as they're
returned. It can do a couple of things:
</p><div class="itemizedlist"><ul type="disc"><li>It can stop the emit process at any point, causing no further slots
to be called</li><li>It can return a value, of any type</li></ul></div><p>For example, if each <tt class="literal">slot</tt> returned an <tt class="literal">int</tt>, we could use a marshaller return
the average value as a <tt class="literal">double</tt>. Or we could return all values in a
<tt class="literal">std::vector<int></tt>, or maybe stop as soon as the first slot returns 5.</p><p>As an example, here's the averaging marshaller:</p><pre class="programlisting">
class Averager
{
public:
// we must typedef InType and OutType for the libsigc++ library
typedef double OutType;
typedef int InType;
Averager()
: total_(0), number_(0)
{}
OutType value() { return (double)total_/(double)number_; } // avoid integer division
static OutType default_value() { return 0; }
// This is the function called for each return value.
// If it returns 'true' it stops here.
bool marshal(InType newval)
{
total_ += newval; // total of values
++number_; // count of values
return false; // continue emittion process
};
private:
int total_;
int number_;
};
</pre><p>To use this, we pass the type as an extra template argument when defining
the <tt class="literal">Signal</tt>, eg.</p><pre class="programlisting">
sigc::signal<int, Averager> mysignal;
</pre><p>Now we can do:</p><pre class="programlisting">
double average_of_all_connected_slots = mysignal();
</pre><p>Each connected <tt class="literal">slot</tt> will be called, its value passed to an instance of
<tt class="literal">Averager</tt> and that <tt class="literal">Averager</tt>'s <tt class="literal">value()</tt> will be returned.</p><p>In the downloadable examples, this is example6.cc.</p></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="ch04s02.html">Prev</a></td><td width="20%" align="center"><a accesskey="u" href="ch04.html">Up</a></td><td width="40%" align="right"><a accesskey="n" href="ch05.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Retyping</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">Chapter5.Reference</td></tr></table></div></body></html>
|