• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>Number List - Printing Numbers From a std::vector</title>
5<link rel="stylesheet" href="../../../../../../../doc/src/boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../../../index.html" title="Spirit 2.5.8">
8<link rel="up" href="../tutorials.html" title="Tutorials">
9<link rel="prev" href="karma_adapted_complex.html" title="Complex - Fully Integrated">
10<link rel="next" href="num_matrix.html" title="Matrix of Numbers - Printing Numbers From a Matrix">
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="karma_adapted_complex.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.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="num_matrix.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h4 class="title">
27<a name="spirit.karma.tutorials.num_list"></a><a class="link" href="num_list.html" title="Number List - Printing Numbers From a std::vector">Number List - Printing
28        Numbers From a std::vector</a>
29</h4></div></div></div>
30<h6>
31<a name="spirit.karma.tutorials.num_list.h0"></a>
32          <span class="phrase"><a name="spirit.karma.tutorials.num_list.using_the_list_operator"></a></span><a class="link" href="num_list.html#spirit.karma.tutorials.num_list.using_the_list_operator">Using
33          the List Operator</a>
34        </h6>
35<p>
36          The C++ Standard library lacks an important feature, namely the support
37          for any formatted output of containers. Sure, it's fairly easy to write
38          a custom routine to output a specific container, but doing so over and
39          over again is tedious at best. In this section we will demonstrate some
40          more of the capabilities of <span class="emphasis"><em>Spirit.Karma</em></span> for generating
41          output from arbitrary STL containers. We will build on the example presented
42          in an earlier section (see <a class="link" href="warming_up.html" title="Warming up">Warming
43          Up</a>).
44        </p>
45<p>
46          The full source code of the example shown in this section can be found
47          here: <a href="../../../../../example/karma/num_list2.cpp" target="_top">num_list2.cpp</a>.
48        </p>
49<p>
50          This time we take advantage of Karma's <a class="link" href="../reference/operator/list.html" title="List Generator (a % b)">List
51          (<code class="computeroutput"><span class="special">%</span></code>)</a> operator. The semantics
52          of the list operator are fully equivalent to the semantics of the sequence
53          we used before. The generator expression
54        </p>
55<pre class="programlisting"><span class="identifier">double_</span> <span class="special">&lt;&lt;</span> <span class="special">*(</span><span class="char">','</span> <span class="special">&lt;&lt;</span> <span class="identifier">double_</span><span class="special">)</span>
56</pre>
57<p>
58          is semantically equivalent to the generator expression
59        </p>
60<pre class="programlisting"><span class="identifier">double_</span> <span class="special">%</span> <span class="char">','</span>
61</pre>
62<p>
63          simplifying the overall code. The list operator's attribute is compatible
64          with any STL container as well. For a change we use a <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span></code> instead of the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="keyword">double</span><span class="special">&gt;</span></code> we used before. Additionally, the routine
65          <code class="computeroutput"><span class="identifier">generate_numbers</span></code> takes
66          the container as a template parameter, so it will now work with any STL
67          container holding <code class="computeroutput"><span class="keyword">double</span></code> numbers.
68        </p>
69<p>
70</p>
71<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">OutputIterator</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Container</span><span class="special">&gt;</span>
72<span class="keyword">bool</span> <span class="identifier">generate_numbers</span><span class="special">(</span><span class="identifier">OutputIterator</span><span class="special">&amp;</span> <span class="identifier">sink</span><span class="special">,</span> <span class="identifier">Container</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">v</span><span class="special">)</span>
73<span class="special">{</span>
74    <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">double_</span><span class="special">;</span>
75    <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">generate_delimited</span><span class="special">;</span>
76    <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">ascii</span><span class="special">::</span><span class="identifier">space</span><span class="special">;</span>
77
78    <span class="keyword">bool</span> <span class="identifier">r</span> <span class="special">=</span> <span class="identifier">generate_delimited</span><span class="special">(</span>
79        <span class="identifier">sink</span><span class="special">,</span>                           <span class="comment">// destination: output iterator</span>
80        <span class="identifier">double_</span> <span class="special">%</span> <span class="char">','</span><span class="special">,</span>                  <span class="comment">// the generator</span>
81        <span class="identifier">space</span><span class="special">,</span>                          <span class="comment">// the delimiter-generator</span>
82        <span class="identifier">v</span>                               <span class="comment">// the data to output </span>
83    <span class="special">);</span>
84    <span class="keyword">return</span> <span class="identifier">r</span><span class="special">;</span>
85<span class="special">}</span>
86</pre>
87<p>
88        </p>
89<div class="note"><table border="0" summary="Note">
90<tr>
91<td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="../../../images/note.png"></td>
92<th align="left">Note</th>
93</tr>
94<tr><td align="left" valign="top"><p>
95            Despite the container being a template parameter, the <span class="emphasis"><em>Spirit.Karma</em></span>
96            formatting expression (<code class="computeroutput"><span class="identifier">double_</span>
97            <span class="special">%</span> <span class="char">','</span></code>)
98            does not depend on the actual type of the passed container. The only
99            precondition to be met here is that the elements stored in the container
100            have to be convertible to <code class="computeroutput"><span class="keyword">double</span></code>.
101          </p></td></tr>
102</table></div>
103<h6>
104<a name="spirit.karma.tutorials.num_list.h1"></a>
105          <span class="phrase"><a name="spirit.karma.tutorials.num_list.generate_output_from_arbitrary_data"></a></span><a class="link" href="num_list.html#spirit.karma.tutorials.num_list.generate_output_from_arbitrary_data">Generate
106          Output from Arbitrary Data</a>
107        </h6>
108<p>
109          The output routine developed above is still not generically usable for
110          all types of STL containers and for arbitrary elements stored in them.
111          In order to be usable the items stored in the container still need to be
112          convertible to a <code class="computeroutput"><span class="keyword">double</span></code>. Fortunately
113          <span class="emphasis"><em>Spirit.Karma</em></span> is capable to output arbitrary data types
114          while using the same format description expression. It implements the
115          <a class="link" href="../reference/stream/stream.html" title="Stream Generators (stream, wstream, etc.)"><code class="computeroutput"><span class="identifier">stream</span></code></a>
116          generators which are able to consume any attribute type as long as a matching
117          standard streaming operator is defined. I.e. for any attribute type <code class="computeroutput"><span class="identifier">Attrib</span></code> a function:
118        </p>
119<pre class="programlisting"><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="keyword">operator</span><span class="special">&lt;&lt;</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;,</span> <span class="identifier">Attrib</span> <span class="keyword">const</span><span class="special">&amp;);</span>
120</pre>
121<p>
122          needs to be available. The <a class="link" href="../reference/stream/stream.html" title="Stream Generators (stream, wstream, etc.)"><code class="computeroutput"><span class="identifier">stream</span></code></a> generator will use the
123          standard streaming operator to generate the output.
124        </p>
125<p>
126          The following example modifies the code shown above to utilize the <a class="link" href="../reference/stream/stream.html" title="Stream Generators (stream, wstream, etc.)"><code class="computeroutput"><span class="identifier">stream</span></code></a>
127          operator, which makes it compatible with almost any data type. We implement
128          a custom data type <code class="computeroutput"><span class="identifier">complex</span></code>
129          to demonstrate this. The example shows how it is possible to integrate
130          this (or any other) custom data type into the <span class="emphasis"><em>Spirit.Karma</em></span>
131          generator framework.
132        </p>
133<p>
134          This is the custom data structure together with the required standard streaming
135          operator:
136        </p>
137<p>
138</p>
139<pre class="programlisting"><span class="comment">// a simple complex number representation z = a + bi</span>
140<span class="keyword">struct</span> <span class="identifier">complex</span>
141<span class="special">{</span>
142    <span class="identifier">complex</span> <span class="special">(</span><span class="keyword">double</span> <span class="identifier">a</span><span class="special">,</span> <span class="keyword">double</span> <span class="identifier">b</span> <span class="special">=</span> <span class="number">0.0</span><span class="special">)</span> <span class="special">:</span> <span class="identifier">a</span><span class="special">(</span><span class="identifier">a</span><span class="special">),</span> <span class="identifier">b</span><span class="special">(</span><span class="identifier">b</span><span class="special">)</span> <span class="special">{}</span>
143
144    <span class="keyword">double</span> <span class="identifier">a</span><span class="special">;</span>
145    <span class="keyword">double</span> <span class="identifier">b</span><span class="special">;</span>
146<span class="special">};</span>
147
148<span class="comment">// the streaming operator for the type complex</span>
149<span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span>
150<span class="keyword">operator</span><span class="special">&lt;&lt;</span> <span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream</span><span class="special">&amp;</span> <span class="identifier">os</span><span class="special">,</span> <span class="identifier">complex</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">z</span><span class="special">)</span>
151<span class="special">{</span>
152    <span class="identifier">os</span> <span class="special">&lt;&lt;</span> <span class="string">"{"</span> <span class="special">&lt;&lt;</span> <span class="identifier">z</span><span class="special">.</span><span class="identifier">a</span> <span class="special">&lt;&lt;</span> <span class="string">","</span> <span class="special">&lt;&lt;</span> <span class="identifier">z</span><span class="special">.</span><span class="identifier">b</span> <span class="special">&lt;&lt;</span> <span class="string">"}"</span><span class="special">;</span>
153    <span class="keyword">return</span> <span class="identifier">os</span><span class="special">;</span>
154<span class="special">}</span>
155</pre>
156<p>
157        </p>
158<p>
159          And this is the actual call to generate the output from a vector of those.
160          This time we interleave the generated output with newline breaks (see
161          <a class="link" href="../reference/auxiliary/eol.html" title="End of Line Generator (eol)"><code class="computeroutput"><span class="identifier">eol</span></code></a>),
162          putting each complex number onto a separate line:
163        </p>
164<p>
165</p>
166<pre class="programlisting"><span class="keyword">template</span> <span class="special">&lt;</span><span class="keyword">typename</span> <span class="identifier">OutputIterator</span><span class="special">,</span> <span class="keyword">typename</span> <span class="identifier">Container</span><span class="special">&gt;</span>
167<span class="keyword">bool</span> <span class="identifier">generate_numbers</span><span class="special">(</span><span class="identifier">OutputIterator</span><span class="special">&amp;</span> <span class="identifier">sink</span><span class="special">,</span> <span class="identifier">Container</span> <span class="keyword">const</span><span class="special">&amp;</span> <span class="identifier">v</span><span class="special">)</span>
168<span class="special">{</span>
169    <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">stream</span><span class="special">;</span>
170    <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">generate</span><span class="special">;</span>
171    <span class="keyword">using</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">spirit</span><span class="special">::</span><span class="identifier">karma</span><span class="special">::</span><span class="identifier">eol</span><span class="special">;</span>
172
173    <span class="keyword">bool</span> <span class="identifier">r</span> <span class="special">=</span> <span class="identifier">generate</span><span class="special">(</span>
174        <span class="identifier">sink</span><span class="special">,</span>                           <span class="comment">// destination: output iterator</span>
175        <span class="identifier">stream</span> <span class="special">%</span> <span class="identifier">eol</span><span class="special">,</span>                   <span class="comment">// the generator</span>
176        <span class="identifier">v</span>                               <span class="comment">// the data to output </span>
177    <span class="special">);</span>
178    <span class="keyword">return</span> <span class="identifier">r</span><span class="special">;</span>
179<span class="special">}</span>
180</pre>
181<p>
182        </p>
183<p>
184          The code shown is fully generic and can be used with any STL container
185          as long as the data items stored in that container implement the standard
186          streaming operator.
187        </p>
188<p>
189          The full source code of the example presented in this section can be found
190          here: <a href="../../../../../example/karma/num_list3.cpp" target="_top">num_list3.cpp</a>.
191        </p>
192</div>
193<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
194<td align="left"></td>
195<td align="right"><div class="copyright-footer">Copyright © 2001-2011 Joel de Guzman, Hartmut Kaiser<p>
196        Distributed under the Boost Software License, Version 1.0. (See accompanying
197        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>)
198      </p>
199</div></td>
200</tr></table>
201<hr>
202<div class="spirit-nav">
203<a accesskey="p" href="karma_adapted_complex.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../tutorials.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="num_matrix.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
204</div>
205</body>
206</html>
207