File: generator_iterator.htm

package info (click to toggle)
boost 1.27.0-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 19,908 kB
  • ctags: 26,546
  • sloc: cpp: 122,225; ansic: 10,956; python: 4,412; sh: 855; yacc: 803; makefile: 257; perl: 165; lex: 90; csh: 6
file content (150 lines) | stat: -rw-r--r-- 4,070 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2//EN">

<html>
<head>
<title>Generator Iterator Adaptor Documentation</title>
</head>

<body bgcolor="#FFFFFF" text="#000000">
        
<img src="../../c++boost.gif" alt="c++boost.gif (8819 bytes)" align="center" width="277" height="86"> 

<h1>Generator Iterator Adaptor</h1>
Defined in header <a href="../../boost/generator_iterator.hpp">boost/generator_iterator.hpp</a> 
<p>
The generator iterator adaptor makes it easier to create custom input
iterators from 0-ary functions and function objects.  The adaptor
takes a
<a href="http://www.sgi.com/tech/stl/Generator.html">Generator</a>
and creates a model of
<a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>.
Each increment retrieves an item from the generator and makes it
available to be retrieved by dereferencing.  The motivation for this
iterator is that some concepts can be more naturally expressed as a
generator, while most STL algorithms expect an iterator.  An example
is the <a href="../random/index.html">Random Number</a> library.

<h2>Synopsis</h2>

<blockquote>
<pre>
namespace boost {
  template &lt;class Generator&gt;
  class generator_iterator_policies;

  template &lt;class Generator&gt;
  class generator_iterator_generator;

  template &lt;class Generator&gt;
  typename generator_iterator_generator&lt;Generator&gt;::type
  make_generator_iterator(Generator &amp; gen);
}
</pre>
</blockquote>

<hr>

<h2>The Generator Iterator Generator Class</h2>

The class generator_iterator_generator is a helper class whose purpose
is to construct a generator iterator type. The template parameter for
this class is the Generator function object type that is being
wrapped.  The generator iterator adaptor only holds a reference (or
pointer) to the function object, therefore the function object must
outlive the generator iterator adaptor constructed from it.

<pre>
template &lt;class Generator>
class generator_iterator_generator
{
public:
  typedef <a href="iterator_adaptors.htm#iterator_adaptor">iterator_adaptor</a>&lt...&gt; type; // the resulting generator iterator type 
}
</pre>


<h3>Template Parameters</h3>

<table border>
<tr>
<th>Parameter</th>
<th>Description</th>
</tr>

<tr>
<td><tt><a href="http://www.sgi.com/tech/stl/Generator.html">Generator</a></tt> 
<td>The generator (0-ary function object) type being
wrapped.  The return type of the function must be defined as
<tt>Generator::result_type</tt>.  The function object must be a model
of
<a href="http://www.sgi.com/tech/stl/Generator.html">Generator</a>.
</td>
</table>

<h3>Concept Model</h3>
The generator iterator class is a model of
<a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>.

<h3>Members</h3>
The generator iterator implements the member functions
and operators required of the
<a href="http://www.sgi.com/tech/stl/InputIterator.html">Input Iterator</a>
concept.

<br>

<hr>
<h2><a name="make_generator_iterator">The Generator Iterator Object Generator</a></h2>

The <tt>make_generator_iterator()</tt> function provides a
convenient way to create generator iterator objects. The function
saves the user the trouble of explicitly writing out the iterator
types.

<blockquote>
<pre>
template &lt;class Generator&gt;
typename generator_iterator_generator&lt;Generator&gt;::type
make_function_output_iterator(Generator &amp; gen);
</pre>
</blockquote>

<hr>


<h3>Example</h3>

The following program shows how <code>generator_iterator</code>
transforms a generator into an input iterator.

<blockquote>
<pre>
#include &lt;iostream>
#include &lt;boost/generator_iterator.hpp>

class my_generator
{
public:
  typedef int result_type;
  my_generator() : state(0) { }
  int operator()() { return ++state; }
private:
  int state;
};

int main()
{
  my_generator gen;
  boost::generator_iterator_generator&lt;my_generator&gt;::type it = boost::make_generator_iterator(gen);
  for(int i = 0; i &lt; 10; ++i, ++it)
    std::cout &lt;&lt; *it &lt;&lt; std::endl;
}
</pre>
</blockquote>

<hr>

Written by Jens Maurer.

</body>
</html>