• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>type_erased</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="Chapter 1. Range 2.0">
8<link rel="up" href="../reference.html" title="Reference">
9<link rel="prev" href="strided.html" title="strided">
10<link rel="next" href="tokenized.html" title="tokenized">
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="strided.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="tokenized.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
24</div>
25<div class="section">
26<div class="titlepage"><div><div><h5 class="title">
27<a name="range.reference.adaptors.reference.type_erased"></a><a class="link" href="type_erased.html" title="type_erased">type_erased</a>
28</h5></div></div></div>
29<div class="toc"><dl class="toc"><dt><span class="section"><a href="type_erased.html#range.reference.adaptors.reference.type_erased.type_erased_example">type-erased
30            example</a></span></dt></dl></div>
31<div class="informaltable"><table class="table">
32<colgroup>
33<col>
34<col>
35</colgroup>
36<thead><tr>
37<th>
38                    <p>
39                      Syntax
40                    </p>
41                  </th>
42<th>
43                    <p>
44                      Code
45                    </p>
46                  </th>
47</tr></thead>
48<tbody>
49<tr>
50<td>
51                    <p>
52                      Pipe
53                    </p>
54                  </td>
55<td>
56                    <p>
57                      <code class="computeroutput"><span class="identifier">rng</span> <span class="special">|</span>
58                      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">adaptors</span><span class="special">::</span><span class="identifier">type_erased</span><span class="special">&lt;</span><span class="identifier">Value</span><span class="special">,</span>
59                      <span class="identifier">Traversal</span><span class="special">,</span>
60                      <span class="identifier">Reference</span><span class="special">,</span>
61                      <span class="identifier">Difference</span><span class="special">,</span>
62                      <span class="identifier">Buffer</span><span class="special">&gt;()</span></code>
63                    </p>
64                  </td>
65</tr>
66<tr>
67<td>
68                    <p>
69                      Function
70                    </p>
71                  </td>
72<td>
73                    <p>
74                      <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">adaptors</span><span class="special">::</span><span class="identifier">type_erase</span><span class="special">(</span><span class="identifier">rng</span><span class="special">,</span>
75                      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">adaptors</span><span class="special">::</span><span class="identifier">type_erased</span><span class="special">&lt;</span><span class="identifier">Value</span><span class="special">,</span>
76                      <span class="identifier">Traversal</span><span class="special">,</span>
77                      <span class="identifier">Reference</span><span class="special">,</span>
78                      <span class="identifier">Difference</span><span class="special">,</span>
79                      <span class="identifier">Buffer</span><span class="special">&gt;)</span></code>
80                    </p>
81                  </td>
82</tr>
83</tbody>
84</table></div>
85<p>
86            Please note that it is frequently unnecessary to use the <code class="computeroutput"><span class="identifier">type_erased</span></code> adaptor. It is often better
87            to use the implicit conversion to <code class="computeroutput"><span class="identifier">any_range</span></code>.
88          </p>
89<p>
90            Let <code class="computeroutput"><span class="identifier">Rng</span></code> be the type of
91            <code class="computeroutput"><span class="identifier">rng</span></code>.
92          </p>
93<div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; ">
94<li class="listitem">
95                <span class="bold"><strong>Template parameters:</strong></span>
96                <div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: circle; ">
97<li class="listitem">
98                      <code class="computeroutput"><span class="identifier">Value</span></code> is the
99                      <code class="computeroutput"><span class="identifier">value_type</span></code>
100                      for the <code class="computeroutput"><span class="identifier">any_range</span></code>.
101                      If this is set to boost::use_default, <code class="computeroutput"><span class="identifier">Value</span></code>
102                      will be calculated from the range type when the adaptor is
103                      applied.
104                    </li>
105<li class="listitem">
106                      <code class="computeroutput"><span class="identifier">Traversal</span></code> is
107                      the tag used to identify the traversal of the resultant range.
108                      Frequently it is desirable to set a traversal category lower
109                      than the source container or range to maximize the number of
110                      ranges that can convert to the <code class="computeroutput"><span class="identifier">any_range</span></code>.
111                      If this is left as boost::use_default then <code class="computeroutput"><span class="identifier">Traversal</span></code>
112                      will be <code class="computeroutput"><span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">iterator_traversal</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_iterator</span><span class="special">&lt;</span><span class="identifier">Rng</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
113                    </li>
114<li class="listitem">
115                      <code class="computeroutput"><span class="identifier">Reference</span></code> is
116                      the <code class="computeroutput"><span class="identifier">reference</span></code>
117                      for the <code class="computeroutput"><span class="identifier">any_range</span></code>.
118                      <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">use_default</span></code> will equate to
119                      <code class="computeroutput"><span class="keyword">typename</span> <span class="identifier">range_reference</span><span class="special">&lt;</span><span class="identifier">Rng</span><span class="special">&gt;::</span><span class="identifier">type</span></code>.
120                    </li>
121<li class="listitem">
122                      <code class="computeroutput"><span class="identifier">Difference</span></code>
123                      is the <code class="computeroutput"><span class="identifier">difference_type</span></code>
124                      for the any_range. <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">use_default</span></code>
125                      will equate to <code class="computeroutput"><span class="keyword">typename</span>
126                      <span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_difference</span><span class="special">&lt;</span><span class="identifier">Rng</span><span class="special">&gt;::</span><span class="identifier">type</span></code>
127                    </li>
128<li class="listitem">
129                      <code class="computeroutput"><span class="identifier">Buffer</span></code> is the
130                      storage used to allocate the underlying iterator wrappers.
131                      This can typically be ignored, but is available as a template
132                      parameter for customization. Buffer must be a model of the
133                      <code class="computeroutput"><span class="identifier">AnyIteratorBufferConcept</span></code>.
134                    </li>
135</ul></div>
136              </li>
137<li class="listitem">
138                <span class="bold"><strong>Precondition:</strong></span> <code class="computeroutput"><span class="identifier">Traversal</span></code>
139                is one of <code class="computeroutput"><span class="special">{</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">use_default</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">single_pass_traversal_tag</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">forward_traversal_tag</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">bidirectional_traversal_tag</span><span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">random_access_traversal_tag</span>
140                <span class="special">}</span></code>
141              </li>
142<li class="listitem">
143                <span class="bold"><strong>Returns:</strong></span> The returned value is the
144                same as <code class="computeroutput"><span class="keyword">typename</span> <span class="identifier">any_range_type_generator</span><span class="special">&lt;</span> <span class="identifier">Rng</span><span class="special">,</span> <span class="identifier">Value</span><span class="special">,</span> <span class="identifier">Traversal</span><span class="special">,</span> <span class="identifier">Reference</span><span class="special">,</span> <span class="identifier">Difference</span><span class="special">,</span> <span class="identifier">Buffer</span>
145                <span class="special">&gt;</span></code> that represents <code class="computeroutput"><span class="identifier">rng</span></code> in a type-erased manner.
146              </li>
147<li class="listitem">
148                <span class="bold"><strong>Range Category:</strong></span> <a class="link" href="../../../concepts/single_pass_range.html" title="Single Pass Range">Single
149                Pass Range</a>
150              </li>
151<li class="listitem">
152                <span class="bold"><strong>Returned Range Category:</strong></span> if <code class="computeroutput"><span class="identifier">Traversal</span></code> was specified as <code class="computeroutput"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">use_default</span></code> then <code class="computeroutput"><span class="keyword">typename</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">iterator_traversal</span><span class="special">&lt;</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">range_iterator</span><span class="special">&lt;</span><span class="identifier">Rng</span><span class="special">&gt;::</span><span class="identifier">type</span><span class="special">&gt;::</span><span class="identifier">type</span></code>,
153                otherwise <code class="computeroutput"><span class="identifier">Traversal</span></code>.
154              </li>
155</ul></div>
156<h6>
157<a name="range.reference.adaptors.reference.type_erased.h0"></a>
158            <span class="phrase"><a name="range.reference.adaptors.reference.type_erased.anyiteratorbufferconcept"></a></span><a class="link" href="type_erased.html#range.reference.adaptors.reference.type_erased.anyiteratorbufferconcept">AnyIteratorBufferConcept</a>
159          </h6>
160<p>
161</p>
162<pre class="programlisting"><span class="keyword">class</span> <span class="identifier">AnyIteratorBufferConcept</span>
163<span class="special">{</span>
164<span class="keyword">public</span><span class="special">:</span>
165    <span class="identifier">AnyIteratorBufferConcept</span><span class="special">();</span>
166    <span class="special">~</span><span class="identifier">AnyIteratorBufferConcept</span><span class="special">();</span>
167
168    <span class="comment">// bytes is the requested size to allocate. This function</span>
169    <span class="comment">// must return a pointer to an adequate area of memory.</span>
170    <span class="comment">// throws: bad_alloc</span>
171    <span class="comment">//</span>
172    <span class="comment">// The buffer will only ever have zero or one</span>
173    <span class="comment">// outstanding memory allocations.</span>
174    <span class="keyword">void</span><span class="special">*</span> <span class="identifier">allocate</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">bytes</span><span class="special">);</span>
175
176    <span class="comment">// deallocate this buffer</span>
177    <span class="keyword">void</span> <span class="identifier">deallocate</span><span class="special">();</span>
178<span class="special">};</span>
179</pre>
180<p>
181          </p>
182<div class="section">
183<div class="titlepage"><div><div><h6 class="title">
184<a name="range.reference.adaptors.reference.type_erased.type_erased_example"></a><a class="link" href="type_erased.html#range.reference.adaptors.reference.type_erased.type_erased_example" title="type-erased example">type-erased
185            example</a>
186</h6></div></div></div>
187<p>
188</p>
189<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">range</span><span class="special">/</span><span class="identifier">adaptor</span><span class="special">/</span><span class="identifier">type_erased</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
190<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">range</span><span class="special">/</span><span class="identifier">algorithm</span><span class="special">/</span><span class="identifier">copy</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
191<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">assign</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
192<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">boost</span><span class="special">/</span><span class="identifier">foreach</span><span class="special">.</span><span class="identifier">hpp</span><span class="special">&gt;</span>
193<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iterator</span><span class="special">&gt;</span>
194<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">iostream</span><span class="special">&gt;</span>
195<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">list</span><span class="special">&gt;</span>
196<span class="preprocessor">#include</span> <span class="special">&lt;</span><span class="identifier">vector</span><span class="special">&gt;</span>
197
198<span class="comment">// The client interface from an OO perspective merely requires a sequence</span>
199<span class="comment">// of integers that can be forward traversed</span>
200<span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">any_range</span><span class="special">&lt;</span>
201    <span class="keyword">int</span>
202  <span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">forward_traversal_tag</span>
203  <span class="special">,</span> <span class="keyword">int</span>
204  <span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">ptrdiff_t</span>
205<span class="special">&gt;</span> <span class="identifier">integer_range</span><span class="special">;</span>
206
207<span class="keyword">namespace</span> <span class="identifier">server</span>
208<span class="special">{</span>
209    <span class="keyword">void</span> <span class="identifier">display_integers</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">integer_range</span><span class="special">&amp;</span> <span class="identifier">rng</span><span class="special">)</span>
210    <span class="special">{</span>
211        <span class="identifier">boost</span><span class="special">::</span><span class="identifier">copy</span><span class="special">(</span><span class="identifier">rng</span><span class="special">,</span>
212                    <span class="identifier">std</span><span class="special">::</span><span class="identifier">ostream_iterator</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special">,</span> <span class="string">","</span><span class="special">));</span>
213
214        <span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special">&lt;&lt;</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">endl</span><span class="special">;</span>
215    <span class="special">}</span>
216<span class="special">}</span>
217
218<span class="keyword">namespace</span> <span class="identifier">client</span>
219<span class="special">{</span>
220    <span class="keyword">void</span> <span class="identifier">run</span><span class="special">()</span>
221    <span class="special">{</span>
222        <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">assign</span><span class="special">;</span>
223        <span class="keyword">using</span> <span class="keyword">namespace</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">adaptors</span><span class="special">;</span>
224
225        <span class="comment">// Under most conditions one would simply use an appropriate</span>
226        <span class="comment">// any_range as a function parameter. The type_erased adaptor</span>
227        <span class="comment">// is often superfluous. However because the type_erased</span>
228        <span class="comment">// adaptor is applied to a range, we can use default template</span>
229        <span class="comment">// arguments that are generated in conjunction with the</span>
230        <span class="comment">// range type to which we are applying the adaptor.</span>
231
232        <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">input</span><span class="special">;</span>
233        <span class="identifier">input</span> <span class="special">+=</span> <span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">;</span>
234
235        <span class="comment">// Note that this call is to a non-template function</span>
236        <span class="identifier">server</span><span class="special">::</span><span class="identifier">display_integers</span><span class="special">(</span><span class="identifier">input</span><span class="special">);</span>
237
238        <span class="identifier">std</span><span class="special">::</span><span class="identifier">list</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">input2</span><span class="special">;</span>
239        <span class="identifier">input2</span> <span class="special">+=</span> <span class="number">6</span><span class="special">,</span><span class="number">7</span><span class="special">,</span><span class="number">8</span><span class="special">,</span><span class="number">9</span><span class="special">,</span><span class="number">10</span><span class="special">;</span>
240
241        <span class="comment">// Note that this call is to the same non-tempate function</span>
242        <span class="identifier">server</span><span class="special">::</span><span class="identifier">display_integers</span><span class="special">(</span><span class="identifier">input2</span><span class="special">);</span>
243
244        <span class="identifier">input2</span><span class="special">.</span><span class="identifier">clear</span><span class="special">();</span>
245        <span class="identifier">input2</span> <span class="special">+=</span> <span class="number">11</span><span class="special">,</span><span class="number">12</span><span class="special">,</span><span class="number">13</span><span class="special">,</span><span class="number">14</span><span class="special">,</span><span class="number">15</span><span class="special">;</span>
246
247        <span class="comment">// Calling using the adaptor looks like this:</span>
248        <span class="comment">// Notice that here I have a type_erased that would be a</span>
249        <span class="comment">// bidirectional_traversal_tag, but this is convertible</span>
250        <span class="comment">// to the forward_traversal_tag equivalent hence this</span>
251        <span class="comment">// works.</span>
252        <span class="identifier">server</span><span class="special">::</span><span class="identifier">display_integers</span><span class="special">(</span><span class="identifier">input2</span> <span class="special">|</span> <span class="identifier">type_erased</span><span class="special">&lt;&gt;());</span>
253
254        <span class="comment">// However we may simply wish to define an adaptor that</span>
255        <span class="comment">// takes a range and makes it into an appropriate</span>
256        <span class="comment">// forward_traversal any_range...</span>
257        <span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">adaptors</span><span class="special">::</span><span class="identifier">type_erased</span><span class="special">&lt;</span>
258            <span class="identifier">boost</span><span class="special">::</span><span class="identifier">use_default</span>
259          <span class="special">,</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">forward_traversal_tag</span>
260        <span class="special">&gt;</span> <span class="identifier">type_erased_forward</span><span class="special">;</span>
261
262        <span class="comment">// This adaptor can turn other containers with different</span>
263        <span class="comment">// value_types and reference_types into the appropriate</span>
264        <span class="comment">// any_range.</span>
265
266        <span class="identifier">server</span><span class="special">::</span><span class="identifier">display_integers</span><span class="special">(</span><span class="identifier">input2</span> <span class="special">|</span> <span class="identifier">type_erased_forward</span><span class="special">());</span>
267    <span class="special">}</span>
268<span class="special">}</span>
269
270<span class="keyword">int</span> <span class="identifier">main</span><span class="special">(</span><span class="keyword">int</span> <span class="identifier">argc</span><span class="special">,</span> <span class="keyword">const</span> <span class="keyword">char</span><span class="special">*</span> <span class="identifier">argv</span><span class="special">[])</span>
271<span class="special">{</span>
272    <span class="identifier">client</span><span class="special">::</span><span class="identifier">run</span><span class="special">();</span>
273    <span class="keyword">return</span> <span class="number">0</span><span class="special">;</span>
274<span class="special">}</span>
275</pre>
276<p>
277            </p>
278</div>
279<p>
280            This would produce the output:
281</p>
282<pre class="programlisting"><span class="number">1</span><span class="special">,</span><span class="number">2</span><span class="special">,</span><span class="number">3</span><span class="special">,</span><span class="number">4</span><span class="special">,</span><span class="number">5</span><span class="special">,</span>
283<span class="number">6</span><span class="special">,</span><span class="number">7</span><span class="special">,</span><span class="number">8</span><span class="special">,</span><span class="number">9</span><span class="special">,</span><span class="number">10</span><span class="special">,</span>
284<span class="number">11</span><span class="special">,</span><span class="number">12</span><span class="special">,</span><span class="number">13</span><span class="special">,</span><span class="number">14</span><span class="special">,</span><span class="number">15</span><span class="special">,</span>
285<span class="number">11</span><span class="special">,</span><span class="number">12</span><span class="special">,</span><span class="number">13</span><span class="special">,</span><span class="number">14</span><span class="special">,</span><span class="number">15</span><span class="special">,</span>
286</pre>
287<p>
288          </p>
289</div>
290<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
291<td align="left"></td>
292<td align="right"><div class="copyright-footer">Copyright © 2003-2010 Thorsten Ottosen,
293      Neil Groves<p>
294        Distributed under the Boost Software License, Version 1.0. (See accompanying
295        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>)
296      </p>
297</div></td>
298</tr></table>
299<hr>
300<div class="spirit-nav">
301<a accesskey="p" href="strided.html"><img src="../../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../reference.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="tokenized.html"><img src="../../../../../../../../doc/src/images/next.png" alt="Next"></a>
302</div>
303</body>
304</html>
305