PageRenderTime 39ms CodeModel.GetById 12ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

/Src/Dependencies/Boost/libs/phoenix/doc/html/phoenix/starter_kit/lazy_operators.html

http://hadesmem.googlecode.com/
HTML | 143 lines | 143 code | 0 blank | 0 comment | 0 complexity | 4d19af0e2531e9a68aa887e14ab5147a MD5 | raw file
  1<html>
  2<head>
  3<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4<title>Lazy Operators</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="../starter_kit.html" title="Starter Kit">
  9<link rel="prev" href="arguments.html" title="Arguments">
 10<link rel="next" href="lazy_statements.html" title="Lazy Statements">
 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="arguments.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../starter_kit.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="lazy_statements.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.starter_kit.lazy_operators"></a><a class="link" href="lazy_operators.html" title="Lazy Operators">Lazy Operators</a>
 28</h3></div></div></div>
 29<p>
 30        You can use the usual set of operators to form expressions. Examples:
 31      </p>
 32<pre class="programlisting"><span class="identifier">arg1</span> <span class="special">*</span> <span class="identifier">arg1</span>
 33<span class="identifier">ref</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">arg1</span> <span class="special">+</span> <span class="identifier">ref</span><span class="special">(</span><span class="identifier">z</span><span class="special">)</span>
 34<span class="identifier">arg1</span> <span class="special">=</span> <span class="identifier">arg2</span> <span class="special">+</span> <span class="special">(</span><span class="number">3</span> <span class="special">*</span> <span class="identifier">arg3</span><span class="special">)</span>
 35<span class="identifier">ref</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">=</span> <span class="identifier">arg1</span><span class="special">[</span><span class="identifier">arg2</span><span class="special">]</span> <span class="comment">// assuming arg1 is indexable and arg2 is a valid index
 36</span></pre>
 37<p>
 38        Note the expression: <code class="computeroutput"><span class="number">3</span> <span class="special">*</span>
 39        <span class="identifier">arg3</span></code>. This expression is actually
 40        a short-hand equivalent to: <code class="computeroutput"><span class="identifier">val</span><span class="special">(</span><span class="number">3</span><span class="special">)</span>
 41        <span class="special">*</span> <span class="identifier">arg3</span></code>.
 42        In most cases, like above, you can get away with it. But in some cases, you
 43        will have to explicitly wrap your values in <code class="computeroutput"><span class="identifier">val</span></code>.
 44        Rules of thumb:
 45      </p>
 46<div class="itemizedlist"><ul class="itemizedlist" type="disc">
 47<li class="listitem">
 48            In a binary expression (e.g. <code class="computeroutput"><span class="number">3</span>
 49            <span class="special">*</span> <span class="identifier">arg3</span></code>),
 50            at least one of the operands must be a phoenix primitive or expression.
 51          </li>
 52<li class="listitem">
 53            In a unary expression (e.g. <code class="computeroutput"><span class="identifier">arg1</span><span class="special">++</span></code>), the single operand must be a phoenix
 54            primitive or expression.
 55          </li>
 56</ul></div>
 57<p>
 58        If these basic rules are not followed, the result is either an error, or
 59        is immediately evaluated. Some examples:
 60      </p>
 61<pre class="programlisting"><span class="identifier">ref</span><span class="special">(</span><span class="identifier">x</span><span class="special">)</span> <span class="special">=</span> <span class="number">123</span>    <span class="comment">// lazy
 62</span><span class="identifier">x</span> <span class="special">=</span> <span class="number">123</span>         <span class="comment">// immediate
 63</span>
 64<span class="identifier">ref</span><span class="special">(</span><span class="identifier">x</span><span class="special">)[</span><span class="number">0</span><span class="special">]</span>       <span class="comment">// lazy
 65</span><span class="identifier">x</span><span class="special">[</span><span class="number">0</span><span class="special">]</span>            <span class="comment">// immediate
 66</span>
 67<span class="identifier">ref</span><span class="special">(</span><span class="identifier">x</span><span class="special">)[</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">i</span><span class="special">)]</span>  <span class="comment">// lazy
 68</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">x</span><span class="special">)[</span><span class="identifier">i</span><span class="special">]</span>       <span class="comment">// lazy (equivalent to ref(x)[val(i)])
 69</span><span class="identifier">x</span><span class="special">[</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">i</span><span class="special">)]</span>       <span class="comment">// illegal (x is not a phoenix primitive or expression)
 70</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">x</span><span class="special">[</span><span class="identifier">ref</span><span class="special">(</span><span class="identifier">i</span><span class="special">)])</span>  <span class="comment">// illegal (x is not a phoenix primitive or expression)
 71</span></pre>
 72<div class="sidebar">
 73<div class="titlepage"></div>
 74<p>
 75        <span class="inlinemediaobject"><img src="../../images/tip.png" alt="tip"></span> Learn more about operators <a class="link" href="../modules/operator.html" title="Operator">here.</a>
 76      </p>
 77</div>
 78<a name="phoenix.starter_kit.lazy_operators.first_practical_example"></a><h5>
 79<a name="id802885"></a>
 80        <a class="link" href="lazy_operators.html#phoenix.starter_kit.lazy_operators.first_practical_example">First
 81        Practical Example</a>
 82      </h5>
 83<p>
 84        We've covered enough ground to present a real world example. We want to find
 85        the first odd number in an STL container. Normally we use a functor (function
 86        object) or a function pointer and pass that in to STL's <code class="computeroutput"><span class="identifier">find_if</span></code>
 87        generic function:
 88      </p>
 89<p>
 90        Write a function:
 91      </p>
 92<pre class="programlisting"><span class="keyword">bool</span>
 93<span class="identifier">is_odd</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">arg1</span><span class="special">)</span>
 94<span class="special">{</span>
 95    <span class="keyword">return</span> <span class="identifier">arg1</span> <span class="special">%</span> <span class="number">2</span> <span class="special">==</span> <span class="number">1</span><span class="special">;</span>
 96<span class="special">}</span>
 97</pre>
 98<p>
 99        Pass a pointer to the function to STL's <code class="computeroutput"><span class="identifier">find_if</span></code>
100        algorithm:
101      </p>
102<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">find_if</span><span class="special">(</span><span class="identifier">c</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="special">&amp;</span><span class="identifier">is_odd</span><span class="special">)</span>
103</pre>
104<p>
105        Using Phoenix, the same can be achieved directly with a one-liner:
106      </p>
107<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">find_if</span><span class="special">(</span><span class="identifier">c</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">c</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">arg1</span> <span class="special">%</span> <span class="number">2</span> <span class="special">==</span> <span class="number">1</span><span class="special">)</span>
108</pre>
109<p>
110        The expression <code class="computeroutput"><span class="identifier">arg1</span> <span class="special">%</span>
111        <span class="number">2</span> <span class="special">==</span> <span class="number">1</span></code> automagically creates a functor with the
112        expected behavior. In FP, this unnamed function is called a lambda function.
113        Unlike the function pointer version, which is monomorphic (expects and works
114        only with a fixed type int argument), the Phoenix version is fully polymorphic
115        and works with any container (of ints, of longs, of bignum, etc.) as long
116        as its elements can handle the <code class="computeroutput"><span class="identifier">arg1</span>
117        <span class="special">%</span> <span class="number">2</span> <span class="special">==</span> <span class="number">1</span></code> expression.
118      </p>
119<p>
120        (See <a href="../../../../example/find_if.cpp" target="_top">find_if.cpp</a>)
121      </p>
122<div class="sidebar">
123<div class="titlepage"></div>
124<p>
125        <span class="inlinemediaobject"><img src="../../images/tip.png" alt="tip"></span> ...<span class="bold"><strong>That's it, we're done</strong></span>.
126        Well if you wish to know a little bit more, read on...
127      </p>
128</div>
129</div>
130<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
131<td align="left"></td>
132<td align="right"><div class="copyright-footer">Copyright &#169; 2002-2005, 2010 Joel de Guzman, Dan Marsden, Thomas Heller<p>
133        Distributed under the Boost Software License, Version 1.0. (See accompanying
134        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>)
135      </p>
136</div></td>
137</tr></table>
138<hr>
139<div class="spirit-nav">
140<a accesskey="p" href="arguments.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../starter_kit.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="lazy_statements.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a>
141</div>
142</body>
143</html>