forked from boostorg/fusion
adds documentation for functional module
[SVN r36959]
This commit is contained in:
@ -3,11 +3,11 @@
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
|
||||
<title>Extension</title>
|
||||
<link rel="stylesheet" href="../boostbook.css" type="text/css">
|
||||
<meta name="generator" content="DocBook XSL Stylesheets V1.66.1">
|
||||
<meta name="generator" content="DocBook XSL Stylesheets V1.68.1">
|
||||
<link rel="start" href="../index.html" title="Chapter<65>1.<2E>Fusion 2.0">
|
||||
<link rel="up" href="../index.html" title="Chapter<65>1.<2E>Fusion 2.0">
|
||||
<link rel="prev" href="tuples/pairs.html" title="Pairs">
|
||||
<link rel="next" href="notes.html" title="Notes">
|
||||
<link rel="next" href="functional.html" title="Functional">
|
||||
</head>
|
||||
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
|
||||
<table cellpadding="2" width="100%">
|
||||
@ -20,15 +20,15 @@
|
||||
</table>
|
||||
<hr>
|
||||
<div class="spirit-nav">
|
||||
<a accesskey="p" href="tuples/pairs.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="notes.html"><img src="../images/next.png" alt="Next"></a>
|
||||
<a accesskey="p" href="tuples/pairs.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="functional.html"><img src="../images/next.png" alt="Next"></a>
|
||||
</div>
|
||||
<div class="section" lang="en">
|
||||
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
|
||||
<a name="fusion.extension"></a><a href="extension.html" title="Extension">Extension</a></h2></div></div></div>
|
||||
<p>
|
||||
The Fusion library is designed to be extensible, new sequences types can easily
|
||||
be added. In fact, the library support for <tt class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span></tt>,
|
||||
<tt class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">array</span></tt> and <a href="http://www.boost.org/libs/mpl/index.html" target="_top">MPL</a>
|
||||
be added. In fact, the library support for <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span></code>,
|
||||
<code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">array</span></code> and <a href="http://www.boost.org/libs/mpl/index.html" target="_top">MPL</a>
|
||||
sequences is entirely provided using the extension mechanism.
|
||||
</p>
|
||||
<p>
|
||||
@ -48,7 +48,7 @@
|
||||
</li>
|
||||
</ol></div>
|
||||
<a name="fusion.extension.our_example"></a><h3>
|
||||
<a name="id609634"></a>
|
||||
<a name="id1181263"></a>
|
||||
<a href="extension.html#fusion.extension.our_example">Our example</a>
|
||||
</h3>
|
||||
<p>
|
||||
@ -73,14 +73,14 @@
|
||||
<p>
|
||||
We are going to pretend that this type has been provided by a 3rd party library,
|
||||
and therefore cannot be modified. We shall work through all the necessary steps
|
||||
to enable <tt class="computeroutput"><span class="identifier">example_struct</span></tt> to
|
||||
to enable <code class="computeroutput"><span class="identifier">example_struct</span></code> to
|
||||
serve as an <a href="sequences/concepts/associative_sequence.html" title="Associative
|
||||
Sequence">Associative
|
||||
Sequence</a> as described in the <a href="quick_start.html" title="Quick Start">Quick
|
||||
Start</a> guide.
|
||||
</p>
|
||||
<a name="fusion.extension.enabling_tag_dispatching"></a><h3>
|
||||
<a name="id609916"></a>
|
||||
<a name="id1181516"></a>
|
||||
<a href="extension.html#fusion.extension.enabling_tag_dispatching">Enabling Tag Dispatching</a>
|
||||
</h3>
|
||||
<p>
|
||||
@ -95,9 +95,9 @@
|
||||
</span><span class="special">}</span>
|
||||
</pre>
|
||||
<p>
|
||||
Next we need to enable the <tt class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">tag_of</span></tt>
|
||||
Next we need to enable the <code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">tag_of</span></code>
|
||||
metafunction to return our newly chosen tag type for operations involving our
|
||||
sequence. This is done by specializing <tt class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">tag_of</span></tt>
|
||||
sequence. This is done by specializing <code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">tag_of</span></code>
|
||||
for our sequence type.
|
||||
</p>
|
||||
<pre class="programlisting">
|
||||
@ -112,8 +112,8 @@
|
||||
<span class="special">}}}</span>
|
||||
</pre>
|
||||
<p>
|
||||
<tt class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">tag_of</span></tt> also has a second template argument,
|
||||
that can be used in conjuction with <tt class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">enable_if</span></tt>
|
||||
<code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">tag_of</span></code> also has a second template argument,
|
||||
that can be used in conjuction with <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">enable_if</span></code>
|
||||
to provide tag support for groups of related types. This feature is not necessary
|
||||
for our sequence, but for an example see the code in:
|
||||
</p>
|
||||
@ -121,7 +121,7 @@
|
||||
<span class="preprocessor">#include</span> <span class="special"><</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">fusion</span><span class="special">/</span><span class="identifier">sequence</span><span class="special">/</span><span class="identifier">adapted</span><span class="special">/</span><span class="identifier">mpl</span><span class="special">/</span><span class="identifier">tag_of</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">></span>
|
||||
</pre>
|
||||
<a name="fusion.extension.designing_a_suitable_iterator"></a><h3>
|
||||
<a name="id610409"></a>
|
||||
<a name="id1181956"></a>
|
||||
<a href="extension.html#fusion.extension.designing_a_suitable_iterator">Designing a
|
||||
suitable iterator</a>
|
||||
</h3>
|
||||
@ -131,9 +131,9 @@
|
||||
access iterator in our example.
|
||||
</p>
|
||||
<p>
|
||||
We will use a simple design, in which the 2 members of <tt class="computeroutput"><span class="identifier">example_struct</span></tt>
|
||||
are given numbered indices, 0 for <tt class="computeroutput"><span class="identifier">name</span></tt>
|
||||
and 1 for <tt class="computeroutput"><span class="identifier">age</span></tt> respectively.
|
||||
We will use a simple design, in which the 2 members of <code class="computeroutput"><span class="identifier">example_struct</span></code>
|
||||
are given numbered indices, 0 for <code class="computeroutput"><span class="identifier">name</span></code>
|
||||
and 1 for <code class="computeroutput"><span class="identifier">age</span></code> respectively.
|
||||
</p>
|
||||
<pre class="programlisting">
|
||||
<span class="keyword">template</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Struct</span><span class="special">,</span> <span class="keyword">int</span> <span class="identifier">Pos</span><span class="special">></span>
|
||||
@ -160,37 +160,37 @@
|
||||
of the current element.
|
||||
</li>
|
||||
<li>
|
||||
The typedefs <tt class="computeroutput"><span class="identifier">struct_type</span></tt>
|
||||
and <tt class="computeroutput"><span class="identifier">index</span></tt> provide convenient
|
||||
The typedefs <code class="computeroutput"><span class="identifier">struct_type</span></code>
|
||||
and <code class="computeroutput"><span class="identifier">index</span></code> provide convenient
|
||||
access to information we will need later in the implementation.
|
||||
</li>
|
||||
<li>
|
||||
The typedef <tt class="computeroutput"><span class="identifier">category</span></tt> allows
|
||||
the <tt class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><a href="support/category_of.html" title="category_of"><tt class="computeroutput"><span class="identifier">category_of</span></tt></a></tt>
|
||||
The typedef <code class="computeroutput"><span class="identifier">category</span></code> allows
|
||||
the <code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><a href="support/category_of.html" title="category_of"><code class="computeroutput"><span class="identifier">category_of</span></code></a></code>
|
||||
metafunction to establish the traversal category of the iterator.
|
||||
</li>
|
||||
<li>
|
||||
The constructor stores a reference to the <tt class="computeroutput"><span class="identifier">example_struct</span></tt>
|
||||
The constructor stores a reference to the <code class="computeroutput"><span class="identifier">example_struct</span></code>
|
||||
being iterated over.
|
||||
</li>
|
||||
</ol></div>
|
||||
<p>
|
||||
We also need to enable <a href="notes.html#fusion.notes.tag_dispatching"><span class="emphasis"><em>tag
|
||||
dispatching</em></span></a> for our iterator type, with another specialization
|
||||
of <tt class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">tag_of</span></tt>.
|
||||
of <code class="computeroutput"><span class="identifier">traits</span><span class="special">::</span><span class="identifier">tag_of</span></code>.
|
||||
</p>
|
||||
<p>
|
||||
In isolation, the iterator implementation is pretty dry. Things should become
|
||||
clearer as we add features to our implementation.
|
||||
</p>
|
||||
<a name="fusion.extension.a_first_couple_of_instructive_features"></a><h3>
|
||||
<a name="id611044"></a>
|
||||
<a name="id1182523"></a>
|
||||
<a href="extension.html#fusion.extension.a_first_couple_of_instructive_features">A first
|
||||
couple of instructive features</a>
|
||||
</h3>
|
||||
<p>
|
||||
To start with, we will get the <a href="iterators/metafunctions/value_of.html" title="value_of"><tt class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">value_of</span></tt></a> metafunction working. To
|
||||
do this, we provide a specialization of the <tt class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">extension</span><span class="special">::</span><span class="identifier">value_of_impl</span></tt>
|
||||
To start with, we will get the <a href="iterators/metafunctions/value_of.html" title="value_of"><code class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">value_of</span></code></a> metafunction working. To
|
||||
do this, we provide a specialization of the <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">fusion</span><span class="special">::</span><span class="identifier">extension</span><span class="special">::</span><span class="identifier">value_of_impl</span></code>
|
||||
template for our iterator's tag type.
|
||||
</p>
|
||||
<pre class="programlisting">
|
||||
@ -215,29 +215,30 @@
|
||||
</pre>
|
||||
<p>
|
||||
The implementation itself is pretty simple, it just uses 2 partial specializations
|
||||
to provide the type of the 2 different members of <tt class="computeroutput"><span class="identifier">example_struct</span></tt>,
|
||||
to provide the type of the 2 different members of <code class="computeroutput"><span class="identifier">example_struct</span></code>,
|
||||
based on the index of the iterator.
|
||||
</p>
|
||||
<p>
|
||||
To understand how <tt class="computeroutput"><span class="identifier">value_of_impl</span></tt>
|
||||
is used by the library we will look at the implementation of <a href="iterators/metafunctions/value_of.html" title="value_of"><tt class="computeroutput"><span class="identifier">value_of</span></tt></a>:
|
||||
To understand how <code class="computeroutput"><span class="identifier">value_of_impl</span></code>
|
||||
is used by the library we will look at the implementation of <a href="iterators/metafunctions/value_of.html" title="value_of"><code class="computeroutput"><span class="identifier">value_of</span></code></a>:
|
||||
</p>
|
||||
<pre class="programlisting">
|
||||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">></span>
|
||||
<span class="keyword">struct</span> <a href="iterators/metafunctions/value_of.html" title="value_of"><tt class="computeroutput"><span class="identifier">value_of</span></tt></a>
|
||||
<span class="keyword">struct</span> <a href="iterators/metafunctions/value_of.html" title="value_of"><code class="computeroutput"><span class="identifier">value_of</span></code></a>
|
||||
<span class="special">:</span> <span class="identifier">extension</span><span class="special">::</span><span class="identifier">value_of_impl</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">tag_of</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">>::</span><span class="identifier">type</span><span class="special">>::</span>
|
||||
<span class="keyword">template</span> <span class="identifier">apply</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">></span>
|
||||
<span class="special">{};</span>
|
||||
</pre>
|
||||
<p>
|
||||
So <a href="iterators/metafunctions/value_of.html" title="value_of"><tt class="computeroutput"><span class="identifier">value_of</span></tt></a>
|
||||
So <a href="iterators/metafunctions/value_of.html" title="value_of"><code class="computeroutput"><span class="identifier">value_of</span></code></a>
|
||||
uses <a href="notes.html#fusion.notes.tag_dispatching"><span class="emphasis"><em>tag dispatching</em></span></a>
|
||||
to select an MPL Metafunction Class to provide its functionality. You will
|
||||
notice this pattern throughout the implementation of Fusion.
|
||||
to select an <a href="http://www.boost.org/libs/mpl/doc/refmanual/metafunction-class.html" target="_top">MPL
|
||||
Metafunction Class</a> to provide its functionality. You will notice this
|
||||
pattern throughout the implementation of Fusion.
|
||||
</p>
|
||||
<p>
|
||||
Ok, lets enable dereferencing of our iterator. In this case we must provide
|
||||
a suitable specialization of <tt class="computeroutput"><span class="identifier">deref_impl</span></tt>.
|
||||
a suitable specialization of <code class="computeroutput"><span class="identifier">deref_impl</span></code>.
|
||||
</p>
|
||||
<pre class="programlisting">
|
||||
<span class="keyword">template</span><span class="special"><></span>
|
||||
@ -275,17 +276,17 @@
|
||||
<span class="special">}</span>
|
||||
</pre>
|
||||
<p>
|
||||
The use of <tt class="computeroutput"><span class="identifier">deref_impl</span></tt> is very
|
||||
similar to that of <tt class="computeroutput"><span class="identifier">value_of_impl</span></tt>,
|
||||
but it also provides some runtime functionality this time via the <tt class="computeroutput"><span class="identifier">call</span></tt> static member function. To see how
|
||||
<tt class="computeroutput"><span class="identifier">deref_impl</span></tt> is used, lets have
|
||||
a look at the implementation of <a href="iterators/functions/deref.html" title="deref"><tt class="computeroutput"><span class="identifier">deref</span></tt></a>:
|
||||
The use of <code class="computeroutput"><span class="identifier">deref_impl</span></code> is very
|
||||
similar to that of <code class="computeroutput"><span class="identifier">value_of_impl</span></code>,
|
||||
but it also provides some runtime functionality this time via the <code class="computeroutput"><span class="identifier">call</span></code> static member function. To see how
|
||||
<code class="computeroutput"><span class="identifier">deref_impl</span></code> is used, lets have
|
||||
a look at the implementation of <a href="iterators/functions/deref.html" title="deref"><code class="computeroutput"><span class="identifier">deref</span></code></a>:
|
||||
</p>
|
||||
<pre class="programlisting">
|
||||
<span class="keyword">namespace</span> <span class="identifier">result_of</span>
|
||||
<span class="special">{</span>
|
||||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">></span>
|
||||
<span class="keyword">struct</span> <a href="iterators/functions/deref.html" title="deref"><tt class="computeroutput"><span class="identifier">deref</span></tt></a>
|
||||
<span class="keyword">struct</span> <a href="iterators/functions/deref.html" title="deref"><code class="computeroutput"><span class="identifier">deref</span></code></a>
|
||||
<span class="special">:</span> <span class="identifier">extension</span><span class="special">::</span><span class="identifier">deref_impl</span><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">detail</span><span class="special">::</span><span class="identifier">tag_of</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">>::</span><span class="identifier">type</span><span class="special">>::</span>
|
||||
<span class="keyword">template</span> <span class="identifier">apply</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">></span>
|
||||
<span class="special">{};</span>
|
||||
@ -293,24 +294,24 @@
|
||||
|
||||
<span class="keyword">template</span> <span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Iterator</span><span class="special">></span>
|
||||
<span class="keyword">typename</span> <span class="identifier">result_of</span><span class="special">::</span><span class="identifier">deref</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">>::</span><span class="identifier">type</span>
|
||||
<a href="iterators/functions/deref.html" title="deref"><tt class="computeroutput"><span class="identifier">deref</span></tt></a><span class="special">(</span><span class="identifier">Iterator</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">i</span><span class="special">)</span>
|
||||
<a href="iterators/functions/deref.html" title="deref"><code class="computeroutput"><span class="identifier">deref</span></code></a><span class="special">(</span><span class="identifier">Iterator</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">i</span><span class="special">)</span>
|
||||
<span class="special">{</span>
|
||||
<span class="keyword">typedef</span> <span class="identifier">result_of</span><span class="special">::</span><span class="identifier">deref</span><span class="special"><</span><span class="identifier">Iterator</span><span class="special">></span> <span class="identifier">deref_meta</span><span class="special">;</span>
|
||||
<span class="keyword">return</span> <span class="identifier">deref_meta</span><span class="special">::</span><span class="identifier">call</span><span class="special">(</span><span class="identifier">i</span><span class="special">);</span>
|
||||
<span class="special">}</span>
|
||||
</pre>
|
||||
<p>
|
||||
So again <a href="iterators/metafunctions/deref.html" title="deref"><tt class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">deref</span></tt></a> uses <a href="notes.html#fusion.notes.tag_dispatching"><span class="emphasis"><em>tag
|
||||
dispatching</em></span></a> in exactly the same way as the <a href="iterators/metafunctions/value_of.html" title="value_of"><tt class="computeroutput"><span class="identifier">value_of</span></tt></a> implementation. The runtime
|
||||
functionality used by <a href="iterators/functions/deref.html" title="deref"><tt class="computeroutput"><span class="identifier">deref</span></tt></a> is provided by the <tt class="computeroutput"><span class="identifier">call</span></tt> static function of the selected MPL
|
||||
Metafunction Class.
|
||||
So again <a href="iterators/metafunctions/deref.html" title="deref"><code class="computeroutput"><span class="identifier">result_of</span><span class="special">::</span><span class="identifier">deref</span></code></a> uses <a href="notes.html#fusion.notes.tag_dispatching"><span class="emphasis"><em>tag
|
||||
dispatching</em></span></a> in exactly the same way as the <a href="iterators/metafunctions/value_of.html" title="value_of"><code class="computeroutput"><span class="identifier">value_of</span></code></a> implementation. The runtime
|
||||
functionality used by <a href="iterators/functions/deref.html" title="deref"><code class="computeroutput"><span class="identifier">deref</span></code></a> is provided by the <code class="computeroutput"><span class="identifier">call</span></code> static function of the selected <a href="http://www.boost.org/libs/mpl/doc/refmanual/metafunction-class.html" target="_top">MPL
|
||||
Metafunction Class</a>.
|
||||
</p>
|
||||
<p>
|
||||
The actual implementation of <tt class="computeroutput"><span class="identifier">deref_impl</span></tt>
|
||||
is slightly more complex than that of <tt class="computeroutput"><span class="identifier">value_of_impl</span></tt>.
|
||||
We also need to implement the <tt class="computeroutput"><span class="identifier">call</span></tt>
|
||||
The actual implementation of <code class="computeroutput"><span class="identifier">deref_impl</span></code>
|
||||
is slightly more complex than that of <code class="computeroutput"><span class="identifier">value_of_impl</span></code>.
|
||||
We also need to implement the <code class="computeroutput"><span class="identifier">call</span></code>
|
||||
function, which returns a reference to the appropriate member of the underlying
|
||||
sequence. We also require a little bit of metaprogramming to return <tt class="computeroutput"><span class="keyword">const</span></tt> references if the underlying sequence
|
||||
sequence. We also require a little bit of metaprogramming to return <code class="computeroutput"><span class="keyword">const</span></code> references if the underlying sequence
|
||||
is const.
|
||||
</p>
|
||||
<div class="note"><table border="0" summary="Note">
|
||||
@ -318,23 +319,23 @@
|
||||
<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../images/note.png"></td>
|
||||
<th align="left">Note</th>
|
||||
</tr>
|
||||
<tr><td colspan="2" align="left" valign="top"><p>
|
||||
<tr><td align="left" valign="top"><p>
|
||||
Although there is a fair amount of left to do to produce a fully fledged
|
||||
Fusion sequence, <a href="iterators/metafunctions/value_of.html" title="value_of"><tt class="computeroutput"><span class="identifier">value_of</span></tt></a> and <a href="iterators/functions/deref.html" title="deref"><tt class="computeroutput"><span class="identifier">deref</span></tt></a> illustrate all the signficant
|
||||
Fusion sequence, <a href="iterators/metafunctions/value_of.html" title="value_of"><code class="computeroutput"><span class="identifier">value_of</span></code></a> and <a href="iterators/functions/deref.html" title="deref"><code class="computeroutput"><span class="identifier">deref</span></code></a> illustrate all the signficant
|
||||
concepts required. The remainder of the process is very repetitive, simply
|
||||
requiring implementation of a suitable <tt class="computeroutput"><span class="identifier">xxxx_impl</span></tt>
|
||||
for each feature <tt class="computeroutput"><span class="identifier">xxxx</span></tt>.
|
||||
requiring implementation of a suitable <code class="computeroutput"><span class="identifier">xxxx_impl</span></code>
|
||||
for each feature <code class="computeroutput"><span class="identifier">xxxx</span></code>.
|
||||
</p></td></tr>
|
||||
</table></div>
|
||||
<a name="fusion.extension.implementing_the_remaining_iterator_functionality"></a><h3>
|
||||
<a name="id613285"></a>
|
||||
<a name="id1184528"></a>
|
||||
<a href="extension.html#fusion.extension.implementing_the_remaining_iterator_functionality">Implementing
|
||||
the remaining iterator functionality</a>
|
||||
</h3>
|
||||
<p>
|
||||
Ok, now we have seen the way <a href="iterators/metafunctions/value_of.html" title="value_of"><tt class="computeroutput"><span class="identifier">value_of</span></tt></a> and <a href="iterators/functions/deref.html" title="deref"><tt class="computeroutput"><span class="identifier">deref</span></tt></a> work, everything else will work
|
||||
Ok, now we have seen the way <a href="iterators/metafunctions/value_of.html" title="value_of"><code class="computeroutput"><span class="identifier">value_of</span></code></a> and <a href="iterators/functions/deref.html" title="deref"><code class="computeroutput"><span class="identifier">deref</span></code></a> work, everything else will work
|
||||
in pretty much the same way. Lets start with forward iteration, by providing
|
||||
a <tt class="computeroutput"><span class="identifier">next_impl</span></tt>:
|
||||
a <code class="computeroutput"><span class="identifier">next_impl</span></code>:
|
||||
</p>
|
||||
<pre class="programlisting">
|
||||
<span class="keyword">template</span><span class="special"><></span>
|
||||
@ -356,40 +357,40 @@
|
||||
<span class="special">};</span>
|
||||
</pre>
|
||||
<p>
|
||||
This should be very familiar from our <tt class="computeroutput"><span class="identifier">deref_impl</span></tt>
|
||||
This should be very familiar from our <code class="computeroutput"><span class="identifier">deref_impl</span></code>
|
||||
implementation, we will be using this approach again and again now. Our design
|
||||
is simply to increment the <tt class="computeroutput"><span class="identifier">index</span></tt>
|
||||
is simply to increment the <code class="computeroutput"><span class="identifier">index</span></code>
|
||||
counter to move on to the next element. The various other iterator manipulations
|
||||
we need to perform will all just involve simple calculations with the <tt class="computeroutput"><span class="identifier">index</span></tt> variables.
|
||||
we need to perform will all just involve simple calculations with the <code class="computeroutput"><span class="identifier">index</span></code> variables.
|
||||
</p>
|
||||
<p>
|
||||
We also need to provide a suitable <tt class="computeroutput"><span class="identifier">equal_to_impl</span></tt>
|
||||
We also need to provide a suitable <code class="computeroutput"><span class="identifier">equal_to_impl</span></code>
|
||||
so that iterators can be correctly compared. A <a href="iterators/concepts/bidirectional_iterator.html" title="Bidirectional
|
||||
Iterator">Bidirectional
|
||||
Iterator</a> will also need an implementation of <tt class="computeroutput"><span class="identifier">prior_impl</span></tt>.
|
||||
Iterator</a> will also need an implementation of <code class="computeroutput"><span class="identifier">prior_impl</span></code>.
|
||||
For a <a href="iterators/concepts/random_access_iterator.html" title="Random
|
||||
Access Iterator">Random
|
||||
Access Iterator</a> <tt class="computeroutput"><span class="identifier">distance_impl</span></tt>
|
||||
and <tt class="computeroutput"><span class="identifier">advance_impl</span></tt> also need
|
||||
Access Iterator</a> <code class="computeroutput"><span class="identifier">distance_impl</span></code>
|
||||
and <code class="computeroutput"><span class="identifier">advance_impl</span></code> also need
|
||||
to be provided in order to satisfy the necessary complexity guarantees. As
|
||||
our iterator is a <a href="iterators/concepts/random_access_iterator.html" title="Random
|
||||
Access Iterator">Random
|
||||
Access Iterator</a> we will have to implement all of these functions.
|
||||
</p>
|
||||
<p>
|
||||
Full implementations of <tt class="computeroutput"><span class="identifier">prior_impl</span></tt>,
|
||||
<tt class="computeroutput"><span class="identifier">advance_impl</span></tt>, <tt class="computeroutput"><span class="identifier">distance_impl</span></tt> and <tt class="computeroutput"><span class="identifier">equal_to_impl</span></tt>
|
||||
Full implementations of <code class="computeroutput"><span class="identifier">prior_impl</span></code>,
|
||||
<code class="computeroutput"><span class="identifier">advance_impl</span></code>, <code class="computeroutput"><span class="identifier">distance_impl</span></code> and <code class="computeroutput"><span class="identifier">equal_to_impl</span></code>
|
||||
are provided in the example code.
|
||||
</p>
|
||||
<a name="fusion.extension.implementing_the_intrinsic_functions_of_the_sequence"></a><h3>
|
||||
<a name="id613911"></a>
|
||||
<a name="id1185084"></a>
|
||||
<a href="extension.html#fusion.extension.implementing_the_intrinsic_functions_of_the_sequence">Implementing
|
||||
the intrinsic functions of the sequence</a>
|
||||
</h3>
|
||||
<p>
|
||||
In order that Fusion can correctly identify our sequence as a Fusion sequence,
|
||||
we need to enable <tt class="computeroutput"><span class="identifier">is_sequence</span></tt>
|
||||
for our sequence type. As usual we just create an <tt class="computeroutput"><span class="identifier">impl</span></tt>
|
||||
we need to enable <code class="computeroutput"><span class="identifier">is_sequence</span></code>
|
||||
for our sequence type. As usual we just create an <code class="computeroutput"><span class="identifier">impl</span></code>
|
||||
type specialized for our sequence tag:
|
||||
</p>
|
||||
<pre class="programlisting">
|
||||
@ -401,14 +402,14 @@
|
||||
<span class="special">};</span>
|
||||
</pre>
|
||||
<p>
|
||||
We've some similar formalities to complete, providing <tt class="computeroutput"><span class="identifier">category_of_impl</span></tt>
|
||||
so Fusion can correctly identify our sequence type, and <tt class="computeroutput"><span class="identifier">is_view_impl</span></tt>
|
||||
We've some similar formalities to complete, providing <code class="computeroutput"><span class="identifier">category_of_impl</span></code>
|
||||
so Fusion can correctly identify our sequence type, and <code class="computeroutput"><span class="identifier">is_view_impl</span></code>
|
||||
so Fusion can correctly identify our sequence as not being a <a href="sequences/views.html" title="Views">View</a>
|
||||
type. Implementations are provide in the example code.
|
||||
</p>
|
||||
<p>
|
||||
Now we've completed some formalities, on to more interesting features. Lets
|
||||
get <a href="sequences/intrinsics/functions/begin.html" title="begin"><tt class="computeroutput"><span class="identifier">begin</span></tt></a> working so that we can get an
|
||||
get <a href="sequences/intrinsics/functions/begin.html" title="begin"><code class="computeroutput"><span class="identifier">begin</span></code></a> working so that we can get an
|
||||
iterator to start accessing the data in our sequence.
|
||||
</p>
|
||||
<pre class="programlisting">
|
||||
@ -431,32 +432,32 @@
|
||||
<p>
|
||||
The implementation uses the same ideas we have applied throughout, in this
|
||||
case we are just creating one of the iterators we developed earlier, pointing
|
||||
to the first element in the sequence. The implementation of <a href="sequences/intrinsics/functions/end.html" title="end"><tt class="computeroutput"><span class="identifier">end</span></tt></a> is very similar, and is provided
|
||||
to the first element in the sequence. The implementation of <a href="sequences/intrinsics/functions/end.html" title="end"><code class="computeroutput"><span class="identifier">end</span></code></a> is very similar, and is provided
|
||||
in the example code.
|
||||
</p>
|
||||
<p>
|
||||
For our <a href="sequences/concepts/random_access_sequence.html" title="Random
|
||||
Access Sequence">Random
|
||||
Access Sequence</a> we will also need to implement <tt class="computeroutput"><span class="identifier">size_impl</span></tt>,
|
||||
<tt class="computeroutput"><span class="identifier">value_at_impl</span></tt> and <tt class="computeroutput"><span class="identifier">at_impl</span></tt>.
|
||||
Access Sequence</a> we will also need to implement <code class="computeroutput"><span class="identifier">size_impl</span></code>,
|
||||
<code class="computeroutput"><span class="identifier">value_at_impl</span></code> and <code class="computeroutput"><span class="identifier">at_impl</span></code>.
|
||||
</p>
|
||||
<a name="fusion.extension.enabling_our_type_as_an_associative_container"></a><h3>
|
||||
<a name="id614499"></a>
|
||||
<a name="id1185603"></a>
|
||||
<a href="extension.html#fusion.extension.enabling_our_type_as_an_associative_container">Enabling
|
||||
our type as an associative container</a>
|
||||
</h3>
|
||||
<p>
|
||||
In order for <tt class="computeroutput"><span class="identifier">example_struct</span></tt>
|
||||
In order for <code class="computeroutput"><span class="identifier">example_struct</span></code>
|
||||
to serve as an associative container, we need to enable 3 lookup features,
|
||||
<a href="sequences/intrinsics/functions/at_key.html" title="at_key"><tt class="computeroutput"><span class="identifier">at_key</span></tt></a>, <a href="sequences/intrinsics/metafunctions/value_at_key.html" title="value_at_key"><tt class="computeroutput"><span class="identifier">value_at_key</span></tt></a> and <a href="sequences/intrinsics/functions/has_key.html" title="has_key"><tt class="computeroutput"><span class="identifier">has_key</span></tt></a>. We also need to provide an
|
||||
implementation of the <tt class="computeroutput"><span class="identifier">is_associative</span></tt>
|
||||
<a href="sequences/intrinsics/functions/at_key.html" title="at_key"><code class="computeroutput"><span class="identifier">at_key</span></code></a>, <a href="sequences/intrinsics/metafunctions/value_at_key.html" title="value_at_key"><code class="computeroutput"><span class="identifier">value_at_key</span></code></a> and <a href="sequences/intrinsics/functions/has_key.html" title="has_key"><code class="computeroutput"><span class="identifier">has_key</span></code></a>. We also need to provide an
|
||||
implementation of the <code class="computeroutput"><span class="identifier">is_associative</span></code>
|
||||
trait so that our sequence can be correctly identified as an associative container.
|
||||
</p>
|
||||
<p>
|
||||
To implement <tt class="computeroutput"><span class="identifier">at_key_impl</span></tt> we
|
||||
need to associate the <tt class="computeroutput"><span class="identifier">fields</span><span class="special">::</span><span class="identifier">age</span></tt> and
|
||||
<tt class="computeroutput"><span class="identifier">fields</span><span class="special">::</span><span class="identifier">age</span></tt> types described in the <a href="quick_start.html" title="Quick Start">Quick
|
||||
Start</a> guide with the appropriate members of <tt class="computeroutput"><span class="identifier">example_struct</span></tt>.
|
||||
To implement <code class="computeroutput"><span class="identifier">at_key_impl</span></code> we
|
||||
need to associate the <code class="computeroutput"><span class="identifier">fields</span><span class="special">::</span><span class="identifier">age</span></code> and
|
||||
<code class="computeroutput"><span class="identifier">fields</span><span class="special">::</span><span class="identifier">age</span></code> types described in the <a href="quick_start.html" title="Quick Start">Quick
|
||||
Start</a> guide with the appropriate members of <code class="computeroutput"><span class="identifier">example_struct</span></code>.
|
||||
Our implementation is as follows:
|
||||
</p>
|
||||
<pre class="programlisting">
|
||||
@ -499,15 +500,15 @@
|
||||
</pre>
|
||||
<p>
|
||||
Its all very similar to the implementations we've seen previously, such as
|
||||
<tt class="computeroutput"><span class="identifier">deref_impl</span></tt> and <tt class="computeroutput"><span class="identifier">value_of_impl</span></tt>. Instead of identifying the
|
||||
members by index or position, we are now selecting them using the types <tt class="computeroutput"><span class="identifier">fields</span><span class="special">::</span><span class="identifier">name</span></tt> and <tt class="computeroutput"><span class="identifier">fields</span><span class="special">::</span><span class="identifier">age</span></tt>. The
|
||||
implementations of <tt class="computeroutput"><span class="identifier">value_at_key_impl</span></tt>
|
||||
and <tt class="computeroutput"><span class="identifier">has_key_impl</span></tt> are equally
|
||||
<code class="computeroutput"><span class="identifier">deref_impl</span></code> and <code class="computeroutput"><span class="identifier">value_of_impl</span></code>. Instead of identifying the
|
||||
members by index or position, we are now selecting them using the types <code class="computeroutput"><span class="identifier">fields</span><span class="special">::</span><span class="identifier">name</span></code> and <code class="computeroutput"><span class="identifier">fields</span><span class="special">::</span><span class="identifier">age</span></code>. The
|
||||
implementations of <code class="computeroutput"><span class="identifier">value_at_key_impl</span></code>
|
||||
and <code class="computeroutput"><span class="identifier">has_key_impl</span></code> are equally
|
||||
straightforward, and are provided in the example code, along with an implementation
|
||||
of <tt class="computeroutput"><span class="identifier">is_associative_impl</span></tt>.
|
||||
of <code class="computeroutput"><span class="identifier">is_associative_impl</span></code>.
|
||||
</p>
|
||||
<a name="fusion.extension.summary"></a><h3>
|
||||
<a name="id615481"></a>
|
||||
<a name="id1186467"></a>
|
||||
<a href="extension.html#fusion.extension.summary">Summary</a>
|
||||
</h3>
|
||||
<p>
|
||||
@ -517,8 +518,8 @@
|
||||
pattern.
|
||||
</p>
|
||||
<p>
|
||||
The support for <tt class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span></tt>, <a href="http://www.boost.org/libs/mpl/index.html" target="_top">MPL</a>
|
||||
sequences, and <tt class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">array</span></tt> all use the same approach, and provide
|
||||
The support for <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span></code>, <a href="http://www.boost.org/libs/mpl/index.html" target="_top">MPL</a>
|
||||
sequences, and <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">array</span></code> all use the same approach, and provide
|
||||
additional examples of the approach for a variety of types.
|
||||
</p>
|
||||
</div>
|
||||
@ -528,7 +529,7 @@
|
||||
</tr></table>
|
||||
<hr>
|
||||
<div class="spirit-nav">
|
||||
<a accesskey="p" href="tuples/pairs.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="notes.html"><img src="../images/next.png" alt="Next"></a>
|
||||
<a accesskey="p" href="tuples/pairs.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../index.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="functional.html"><img src="../images/next.png" alt="Next"></a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
|
Reference in New Issue
Block a user