PageRenderTime 21ms CodeModel.GetById 1ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/libs/phoenix/doc/html/phoenix/modules/function.html

http://hadesmem.googlecode.com/
HTML | 150 lines | 148 code | 2 blank | 0 comment | 0 complexity | 126a6b8e815421a0e5460f8ea3abd84f MD5 | raw file
  1<html>
  2<head>
  3<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4<title>Function</title>
  5<link rel="stylesheet" href="../../../../../../doc/src/boostbook.css" type="text/css">
  6<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
  7<link rel="home" href="../../index.html" title="Chapter&#160;1.&#160;Phoenix 3.0">
  8<link rel="up" href="../modules.html" title="Modules">
  9<link rel="prev" href="core/nothing.html" title="Nothing">
 10<link rel="next" href="function/adapting_functions.html" title="Adapting Functions">
 11</head>
 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
 13<table cellpadding="2" width="100%"><tr>
 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td>
 15<td align="center"><a href="../../../../../../index.html">Home</a></td>
 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td>
 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td>
 20</tr></table>
 21<hr>
 22<div class="spirit-nav">
 23<a accesskey="p" href="core/nothing.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../modules.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="function/adapting_functions.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
 24</div>
 25<div class="section">
 26<div class="titlepage"><div><div><h3 class="title">
 27<a name="phoenix.modules.function"></a><a class="link" href="function.html" title="Function">Function</a>
 28</h3></div></div></div>
 29<div class="toc"><dl><dt><span class="section"><a href="function/adapting_functions.html">Adapting
 30        Functions</a></span></dt></dl></div>
 31<p>
 32        The <code class="computeroutput"><span class="identifier">function</span></code> class template
 33        provides a mechanism for implementing lazily evaluated functions. Syntactically,
 34        a lazy function looks like an ordinary C/C++ function. The function call
 35        looks familiar and feels the same as ordinary C++ functions. However, unlike
 36        ordinary functions, the actual function execution is deferred.
 37      </p>
 38<pre class="programlisting"><span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">phoenix</span><span class="special">/</span><span class="identifier">function</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
 39</pre>
 40<p>
 41        Unlike ordinary function pointers or functor objects that need to be explicitly
 42        bound through the bind function (see <a class="link" href="bind.html" title="Bind">Bind</a>),
 43        the argument types of these functions are automatically lazily bound.
 44      </p>
 45<p>
 46        In order to create a lazy function, we need to implement a model of the
 47        <a href="http://www.boost.org/doc/libs/release/libs/fusion/doc/html/fusion/functional/concepts/poly.html" target="_top">Polymorphic
 48        Function Object</a> concept. For a function that takes <code class="computeroutput"><span class="identifier">N</span></code> arguments, a model of <a href="http://www.boost.org/doc/libs/release/libs/fusion/doc/html/fusion/functional/concepts/poly.html" target="_top">Polymorphic
 49        Function Object</a> must provide:
 50      </p>
 51<div class="itemizedlist"><ul class="itemizedlist" type="disc">
 52<li class="listitem">
 53            An <code class="computeroutput"><span class="keyword">operator</span><span class="special">()</span></code>
 54            that takes <code class="computeroutput"><span class="identifier">N</span></code> arguments,
 55            and implements the function logic. This is also true for ordinary function
 56            pointers.
 57          </li>
 58<li class="listitem">
 59            A nested metafunction <code class="computeroutput"><span class="identifier">result</span><span class="special">&lt;</span><span class="identifier">Signature</span><span class="special">&gt;</span></code> or nested typedef <code class="computeroutput"><span class="identifier">result_type</span></code>,
 60            following the <a href="http://www.boost.org/doc/libs/release/libs/utility/utility.htm#result_of" target="_top">Boost.Result
 61            Of</a> Protocol
 62          </li>
 63</ul></div>
 64<p>
 65        For example, the following type implements the FunctionEval concept, in order
 66        to provide a lazy factorial function:
 67      </p>
 68<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">factorial_impl</span>
 69<span class="special">{</span>
 70    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Sig</span><span class="special">&gt;</span>
 71    <span class="keyword">struct</span> <span class="identifier">result</span><span class="special">;</span>
 72    
 73    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">This</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Arg</span><span class="special">&gt;</span>
 74    <span class="keyword">struct</span> <span class="identifier">result</span><span class="special">&lt;</span><span class="identifier">This</span><span class="special">(</span><span class="identifier">Arg</span> <span class="keyword">const</span> <span class="special">&amp;)&gt;</span>
 75    <span class="special">{</span>
 76        <span class="keyword">typedef</span> <span class="identifier">Arg</span> <span class="identifier">type</span><span class="special">;</span>
 77    <span class="special">};</span>
 78
 79    <span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">Arg</span><span class="special">&gt;</span>
 80    <span class="identifier">Arg</span> <span class="keyword">operator</span><span class="special">()(</span><span class="identifier">Arg</span> <span class="keyword">const</span> <span class="special">&amp;</span> <span class="identifier">n</span><span class="special">)</span> <span class="keyword">const</span>
 81    <span class="special">{</span>
 82        <span class="keyword">return</span> <span class="special">(</span><span class="identifier">n</span> <span class="special">&lt;=</span> <span class="number">0</span><span class="special">)</span> <span class="special">?</span> <span class="number">1</span> <span class="special">:</span> <span class="identifier">n</span> <span class="special">*</span> <span class="special">(*</span><span class="keyword">this</span><span class="special">)(</span><span class="identifier">n</span><span class="special">-</span><span class="number">1</span><span class="special">);</span>
 83    <span class="special">}</span>
 84<span class="special">};</span>
 85</pre>
 86<p>
 87        (See <a href="../../../../example/factorial.cpp" target="_top">factorial.cpp</a>)
 88      </p>
 89<p>
 90        Having implemented the <code class="computeroutput"><span class="identifier">factorial_impl</span></code>
 91        type, we can declare and instantiate a lazy <code class="computeroutput"><span class="identifier">factorial</span></code>
 92        function this way:
 93      </p>
 94<pre class="programlisting"><span class="identifier">function</span><span class="special">&lt;</span><span class="identifier">factorial_impl</span><span class="special">&gt;</span> <span class="identifier">factorial</span><span class="special">;</span>
 95</pre>
 96<p>
 97        Invoking a lazy function such as <code class="computeroutput"><span class="identifier">factorial</span></code>
 98        does not immediately execute the function object <code class="computeroutput"><span class="identifier">factorial_impl</span></code>.
 99        Instead, an <a class="link" href="../actor.html" title="Actor">actor</a> object is created
100        and returned to the caller. Example:
101      </p>
102<pre class="programlisting"><span class="identifier">factorial</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">)</span>
103</pre>
104<p>
105        does nothing more than return an actor. A second function call will invoke
106        the actual factorial function. Example:
107      </p>
108<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">factorial</span><span class="special">(</span><span class="identifier">arg1</span><span class="special">)(</span><span class="number">4</span><span class="special">);</span>
109</pre>
110<p>
111        will print out "24".
112      </p>
113<p>
114        Take note that in certain cases (e.g. for function objects with state), an
115        instance of the model of <a href="http://www.boost.org/doc/libs/release/libs/fusion/doc/html/fusion/functional/concepts/poly.html" target="_top">Polymorphic
116        Function Object</a> may be passed on to the constructor. Example:
117      </p>
118<pre class="programlisting"><span class="identifier">function</span><span class="special">&lt;</span><span class="identifier">factorial_impl</span><span class="special">&gt;</span> <span class="identifier">factorial</span><span class="special">(</span><span class="identifier">ftor</span><span class="special">);</span>
119</pre>
120<p>
121        where ftor is an instance of factorial_impl (this is not necessary in this
122        case as <code class="computeroutput"><span class="identifier">factorial_impl</span></code> does
123        not require any state).
124      </p>
125<div class="important"><table border="0" summary="Important">
126<tr>
127<td rowspan="2" align="center" valign="top" width="25"><img alt="[Important]" src="../../../../../../doc/src/images/important.png"></td>
128<th align="left">Important</th>
129</tr>
130<tr><td align="left" valign="top"><p>
131          Take care though when using function objects with state because they are
132          often copied repeatedly, and state may change in one of the copies, rather
133          than the original.
134        </p></td></tr>
135</table></div>
136</div>
137<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
138<td align="left"></td>
139<td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2010 Joel de Guzman, Dan Marsden, Thomas Heller<p>
140        Distributed under the Boost Software License, Version 1.0. (See accompanying
141        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
142      </p>
143</div></td>
144</tr></table>
145<hr>
146<div class="spirit-nav">
147<a accesskey="p" href="core/nothing.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../modules.html"><img src="../../../../../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="../../index.html"><img src="../../../../../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="function/adapting_functions.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
148</div>
149</body>
150</html>