• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>any_range</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="../ranges.html" title="Provided Ranges">
9<link rel="prev" href="../ranges.html" title="Provided Ranges">
10<link rel="next" href="counting_range.html" title="counting_range">
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="../ranges.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ranges.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="counting_range.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="range.reference.ranges.any_range"></a><a class="link" href="any_range.html" title="any_range">any_range</a>
28</h4></div></div></div>
29<h6>
30<a name="range.reference.ranges.any_range.h0"></a>
31          <span class="phrase"><a name="range.reference.ranges.any_range.description"></a></span><a class="link" href="any_range.html#range.reference.ranges.any_range.description">Description</a>
32        </h6>
33<p>
34          <code class="computeroutput"><span class="identifier">any_range</span></code> is a range that
35          has the type information erased hence a <code class="computeroutput"><span class="identifier">any_range</span><span class="special">&lt;</span><span class="keyword">int</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="keyword">int</span><span class="special">,</span>
36          <span class="identifier">std</span><span class="special">::</span><span class="identifier">ptrdiff_t</span><span class="special">&gt;</span></code>
37          can be used to represent 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">int</span><span class="special">&gt;</span></code>, a <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">int</span><span class="special">&gt;</span></code> or many other types.
38        </p>
39<p>
40          The <a href="http://www.artima.com/cppsource/type_erasure.html" target="_top">type
41          erasure article</a> covers the motivation and goals of type erasure
42          in this context. Clearly my implementation is building upon a lot of prior
43          art created by others. Thomas Becker's <code class="computeroutput"><span class="identifier">any_iterator</span></code>
44          was a strong influence. Adobe also have an <code class="computeroutput"><span class="identifier">any_iterator</span></code>
45          implementation, but this has very tight coupling to other parts of the
46          library that precluded it from use in Boost.Range. Early development versions
47          of this Range Adaptor directly used Thomas Becker's any_iterator implementation.
48          Subsequently I discovered that the heap allocations of this and many other
49          implementations cause poor speed performance particularly at the tails
50          of the distribution. To solve this required a new design that incorporated
51          the embedded buffer optimization.
52        </p>
53<p>
54          Despite the underlying <code class="computeroutput"><span class="identifier">any_iterator</span></code>
55          being the fastest available implementation, the performance overhead of
56          <code class="computeroutput"><span class="identifier">any_range</span></code> is still appreciable
57          due to the cost of virtual function calls required to implement <code class="computeroutput"><span class="identifier">increment</span></code>, <code class="computeroutput"><span class="identifier">decrement</span></code>,
58          <code class="computeroutput"><span class="identifier">advance</span></code>, <code class="computeroutput"><span class="identifier">equal</span></code> etc. Frequently a better design
59          choice is to convert to a canonical form.
60        </p>
61<p>
62          Please see the <a class="link" href="../adaptors/reference/type_erased.html" title="type_erased">type_erased</a>
63          for a Range Adaptor that returns <code class="computeroutput"><span class="identifier">any_range</span></code>
64          instances.
65        </p>
66<h6>
67<a name="range.reference.ranges.any_range.h1"></a>
68          <span class="phrase"><a name="range.reference.ranges.any_range.synopsis"></a></span><a class="link" href="any_range.html#range.reference.ranges.any_range.synopsis">Synopsis</a>
69        </h6>
70<p>
71</p>
72<pre class="programlisting"><span class="keyword">template</span><span class="special">&lt;</span>
73    <span class="keyword">class</span> <span class="identifier">Value</span>
74  <span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Traversal</span>
75  <span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Reference</span>
76  <span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Difference</span>
77  <span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Buffer</span> <span class="special">=</span> <span class="identifier">any_iterator_default_buffer</span>
78<span class="special">&gt;</span>
79<span class="keyword">class</span> <span class="identifier">any_range</span>
80    <span class="special">:</span> <span class="keyword">public</span> <span class="identifier">iterator_range</span><span class="special">&lt;</span>
81        <span class="identifier">range_detail</span><span class="special">::</span><span class="identifier">any_iterator</span><span class="special">&lt;</span>
82            <span class="identifier">Value</span>
83          <span class="special">,</span> <span class="identifier">Traversal</span>
84          <span class="special">,</span> <span class="identifier">Reference</span>
85          <span class="special">,</span> <span class="identifier">Difference</span>
86          <span class="special">,</span> <span class="identifier">Buffer</span>
87        <span class="special">&gt;</span>
88    <span class="special">&gt;</span>
89<span class="special">{</span>
90    <span class="keyword">typedef</span> <span class="identifier">range_detail</span><span class="special">::</span><span class="identifier">any_iterator</span><span class="special">&lt;</span>
91        <span class="identifier">Value</span>
92      <span class="special">,</span> <span class="identifier">Traversal</span>
93      <span class="special">,</span> <span class="identifier">Reference</span>
94      <span class="special">,</span> <span class="identifier">Difference</span>
95      <span class="special">,</span> <span class="identifier">Buffer</span>
96    <span class="special">&gt;</span> <span class="identifier">any_iterator_type</span><span class="special">;</span>
97
98    <span class="keyword">typedef</span> <span class="identifier">iterator_range</span><span class="special">&lt;</span><span class="identifier">any_iterator_type</span><span class="special">&gt;</span> <span class="identifier">base_type</span><span class="special">;</span>
99
100    <span class="keyword">struct</span> <span class="identifier">enabler</span> <span class="special">{};</span>
101    <span class="keyword">struct</span> <span class="identifier">disabler</span> <span class="special">{};</span>
102<span class="keyword">public</span><span class="special">:</span>
103    <span class="keyword">typedef</span> <span class="identifier">any_iterator_type</span> <span class="identifier">iterator</span><span class="special">;</span>
104    <span class="keyword">typedef</span> <span class="identifier">any_iterator_type</span> <span class="identifier">const_iterator</span><span class="special">;</span>
105
106    <span class="identifier">any_range</span><span class="special">()</span>
107    <span class="special">{</span>
108    <span class="special">}</span>
109
110    <span class="identifier">any_range</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">any_range</span><span class="special">&amp;</span> <span class="identifier">other</span><span class="special">)</span>
111        <span class="special">:</span> <span class="identifier">base_type</span><span class="special">(</span><span class="identifier">other</span><span class="special">)</span>
112    <span class="special">{</span>
113    <span class="special">}</span>
114
115    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">WrappedRange</span><span class="special">&gt;</span>
116    <span class="identifier">any_range</span><span class="special">(</span><span class="identifier">WrappedRange</span><span class="special">&amp;</span> <span class="identifier">wrapped_range</span><span class="special">)</span>
117    <span class="special">:</span> <span class="identifier">base_type</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">begin</span><span class="special">(</span><span class="identifier">wrapped_range</span><span class="special">),</span>
118                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">end</span><span class="special">(</span><span class="identifier">wrapped_range</span><span class="special">))</span>
119    <span class="special">{</span>
120    <span class="special">}</span>
121
122    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">WrappedRange</span><span class="special">&gt;</span>
123    <span class="identifier">any_range</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">WrappedRange</span><span class="special">&amp;</span> <span class="identifier">wrapped_range</span><span class="special">)</span>
124    <span class="special">:</span> <span class="identifier">base_type</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">begin</span><span class="special">(</span><span class="identifier">wrapped_range</span><span class="special">),</span>
125                <span class="identifier">boost</span><span class="special">::</span><span class="identifier">end</span><span class="special">(</span><span class="identifier">wrapped_range</span><span class="special">))</span>
126    <span class="special">{</span>
127    <span class="special">}</span>
128
129    <span class="keyword">template</span><span class="special">&lt;</span>
130        <span class="keyword">class</span> <span class="identifier">OtherValue</span>
131      <span class="special">,</span> <span class="keyword">class</span> <span class="identifier">OtherTraversal</span>
132      <span class="special">,</span> <span class="keyword">class</span> <span class="identifier">OtherReference</span>
133      <span class="special">,</span> <span class="keyword">class</span> <span class="identifier">OtherDifference</span>
134    <span class="special">&gt;</span>
135    <span class="identifier">any_range</span><span class="special">(</span><span class="keyword">const</span> <span class="identifier">any_range</span><span class="special">&lt;</span>
136                        <span class="identifier">OtherValue</span>
137                      <span class="special">,</span> <span class="identifier">OtherTraversal</span>
138                      <span class="special">,</span> <span class="identifier">OtherReference</span>
139                      <span class="special">,</span> <span class="identifier">OtherDifference</span>
140                      <span class="special">,</span> <span class="identifier">Buffer</span>
141                    <span class="special">&gt;&amp;</span> <span class="identifier">other</span><span class="special">)</span>
142    <span class="special">:</span> <span class="identifier">base_type</span><span class="special">(</span><span class="identifier">boost</span><span class="special">::</span><span class="identifier">begin</span><span class="special">(</span><span class="identifier">other</span><span class="special">),</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">end</span><span class="special">(</span><span class="identifier">other</span><span class="special">))</span>
143    <span class="special">{</span>
144    <span class="special">}</span>
145
146    <span class="keyword">template</span><span class="special">&lt;</span><span class="keyword">class</span> <span class="identifier">Iterator</span><span class="special">&gt;</span>
147    <span class="identifier">any_range</span><span class="special">(</span><span class="identifier">Iterator</span> <span class="identifier">first</span><span class="special">,</span> <span class="identifier">Iterator</span> <span class="identifier">last</span><span class="special">)</span>
148        <span class="special">:</span> <span class="identifier">base_type</span><span class="special">(</span><span class="identifier">first</span><span class="special">,</span> <span class="identifier">last</span><span class="special">)</span>
149    <span class="special">{</span>
150    <span class="special">}</span>
151<span class="special">};</span>
152</pre>
153<p>
154        </p>
155<h6>
156<a name="range.reference.ranges.any_range.h2"></a>
157          <span class="phrase"><a name="range.reference.ranges.any_range.definition"></a></span><a class="link" href="any_range.html#range.reference.ranges.any_range.definition">Definition</a>
158        </h6>
159<p>
160          Defined in header file <code class="computeroutput"><span class="identifier">boost</span><span class="special">/</span><span class="identifier">range</span><span class="special">/</span><span class="identifier">any_range</span><span class="special">.</span><span class="identifier">hpp</span></code>
161        </p>
162</div>
163<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
164<td align="left"></td>
165<td align="right"><div class="copyright-footer">Copyright © 2003-2010 Thorsten Ottosen,
166      Neil Groves<p>
167        Distributed under the Boost Software License, Version 1.0. (See accompanying
168        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>)
169      </p>
170</div></td>
171</tr></table>
172<hr>
173<div class="spirit-nav">
174<a accesskey="p" href="../ranges.html"><img src="../../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../ranges.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="counting_range.html"><img src="../../../../../../../doc/src/images/next.png" alt="Next"></a>
175</div>
176</body>
177</html>
178