1
1
[section:shared_container Shared Container Iterator]
2
2
3
- Defined in header [@../../../boost/shared_container_iterator.hpp `boost/shared_container_iterator.hpp`].
3
+ Defined in header [@../../../boost/iterator/ shared_container_iterator.hpp `boost/iterator /shared_container_iterator.hpp`].
4
4
5
5
The purpose of the shared container iterator is to attach the lifetime
6
6
of a container to the lifetime of its iterators. In other words, the
@@ -24,9 +24,22 @@ iterator.
24
24
[h2 Synopsis]
25
25
26
26
namespace boost {
27
+ namespace iterators {
27
28
template <typename Container>
28
29
class shared_container_iterator;
29
30
31
+ template <typename Container>
32
+ shared_container_iterator<Container>
33
+ make_shared_container_iterator(typename Container::iterator base,
34
+ std::shared_ptr<Container> const& container);
35
+
36
+ std::pair<
37
+ typename shared_container_iterator<Container>,
38
+ typename shared_container_iterator<Container>
39
+ >
40
+ make_shared_container_range(std::shared_ptr<Container> const& container);
41
+
42
+ // Backward compatibility with boost::shared_ptr
30
43
template <typename Container>
31
44
shared_container_iterator<Container>
32
45
make_shared_container_iterator(typename Container::iterator base,
@@ -38,6 +51,15 @@ iterator.
38
51
>
39
52
make_shared_container_range(boost::shared_ptr<Container> const& container);
40
53
}
54
+ }
55
+
56
+ [note `shared_container_iterator` and its factory functions support both
57
+ `std::shared_ptr` and `boost::shared_ptr` for a smart pointer that holds
58
+ a shared reference to the container. However, the support for `boost::shared_ptr`
59
+ comes at a cost of wrapping it in a `std::shared_ptr` internally. This means
60
+ that when constructing the iterator from a `boost::shared_ptr`, the construction
61
+ will have to allocate memory for `std::shared_ptr` shared state, which may
62
+ potentially fail. It is recommended to use `std::shared_ptr` directly.]
41
63
42
64
[section:shared_container_type The Shared Container Iterator Type]
43
65
@@ -57,18 +79,17 @@ the underlying vector and thereby extend the container's lifetime.
57
79
58
80
[example_link shared_iterator_example1.cpp..`shared_iterator_example1.cpp`]:
59
81
60
- #include "shared_container_iterator.hpp"
61
- #include "boost/shared_ptr.hpp"
82
+ #include <boost/iterator/shared_container_iterator.hpp>
62
83
#include <algorithm>
63
84
#include <iostream>
64
85
#include <vector>
86
+ #include <memory>
65
87
66
- typedef boost::shared_container_iterator< std::vector<int> > iterator;
67
-
88
+ using iterator = boost::iterators::shared_container_iterator< std::vector<int> >;
68
89
69
- void set_range(iterator& i, iterator& end) {
70
90
71
- boost::shared_ptr< std::vector<int> > ints(new std::vector<int>());
91
+ void set_range(iterator& i, iterator& end) {
92
+ std::shared_ptr< std::vector<int> > ints(new std::vector<int>());
72
93
73
94
ints->push_back(0);
74
95
ints->push_back(1);
@@ -77,18 +98,17 @@ the underlying vector and thereby extend the container's lifetime.
77
98
ints->push_back(4);
78
99
ints->push_back(5);
79
100
80
- i = iterator(ints->begin(),ints);
81
- end = iterator(ints->end(),ints);
101
+ i = iterator(ints->begin(), ints);
102
+ end = iterator(ints->end(), ints);
82
103
}
83
104
84
105
85
106
int main() {
107
+ iterator i, end;
86
108
87
- iterator i,end;
88
-
89
- set_range(i,end);
109
+ set_range(i, end);
90
110
91
- std::copy(i,end,std::ostream_iterator<int>(std::cout,","));
111
+ std::copy(i, end, std::ostream_iterator<int>(std::cout, ","));
92
112
std::cout.put('\n');
93
113
94
114
return 0;
@@ -116,15 +136,25 @@ The `shared_container_iterator` type implements the member functions
116
136
and operators required of the
117
137
[@http://www.sgi.com/tech/stl/RandomAccessIterator.html Random Access
118
138
Iterator] concept, though only operations defined for the base
119
- iterator will be valid. In addition it has the following constructor :
139
+ iterator will be valid. In addition it has the following constructors :
120
140
141
+ shared_container_iterator(Container::iterator const& it,
142
+ std::shared_ptr<Container> const& container)
143
+
144
+ // Backward compatibility with boost::shared_ptr
121
145
shared_container_iterator(Container::iterator const& it,
122
146
boost::shared_ptr<Container> const& container)
123
147
124
148
[endsect]
125
149
126
150
[section:shared_container_object_generator The Shared Container Iterator Object Generator]
127
151
152
+ template <typename Container>
153
+ shared_container_iterator<Container>
154
+ make_shared_container_iterator(Container::iterator base,
155
+ std::shared_ptr<Container> const& container)
156
+
157
+ // Backward compatibility with boost::shared_ptr
128
158
template <typename Container>
129
159
shared_container_iterator<Container>
130
160
make_shared_container_iterator(Container::iterator base,
@@ -142,25 +172,23 @@ uses `make_shared_container_iterator()` to create the iterators.
142
172
143
173
[example_link shared_iterator_example2.cpp..`shared_iterator_example2.cpp`]:
144
174
145
- #include "shared_container_iterator.hpp"
146
- #include "boost/shared_ptr.hpp"
175
+ #include <boost/iterator/shared_container_iterator.hpp>
147
176
#include <algorithm>
148
177
#include <iterator>
149
178
#include <iostream>
150
179
#include <vector>
180
+ #include <memory>
151
181
152
182
153
183
template <typename Iterator>
154
- void print_range_nl (Iterator begin, Iterator end) {
155
- typedef typename std::iterator_traits<Iterator>::value_type val ;
156
- std::copy(begin,end,std::ostream_iterator<val>(std::cout,","));
184
+ void print_range_nl(Iterator begin, Iterator end) {
185
+ using val = typename std::iterator_traits<Iterator>::value_type;
186
+ std::copy(begin, end, std::ostream_iterator<val>(std::cout, ","));
157
187
std::cout.put('\n');
158
188
}
159
189
160
-
161
190
int main() {
162
-
163
- typedef boost::shared_ptr< std::vector<int> > ints_t;
191
+ using ints_t = std::shared_ptr< std::vector<int> >;
164
192
{
165
193
ints_t ints(new std::vector<int>());
166
194
@@ -171,12 +199,10 @@ uses `make_shared_container_iterator()` to create the iterators.
171
199
ints->push_back(4);
172
200
ints->push_back(5);
173
201
174
- print_range_nl(boost::make_shared_container_iterator(ints->begin(),ints),
175
- boost::make_shared_container_iterator(ints->end(),ints));
202
+ print_range_nl(boost::iterators:: make_shared_container_iterator(ints->begin(), ints),
203
+ boost::iterators:: make_shared_container_iterator(ints->end(), ints));
176
204
}
177
205
178
-
179
-
180
206
return 0;
181
207
}
182
208
@@ -187,53 +213,61 @@ named. The output from this example is the same as the previous.
187
213
188
214
[section:shared_container_generator The Shared Container Iterator Range Generator]
189
215
216
+ template <typename Container>
217
+ std::pair<
218
+ shared_container_iterator<Container>,
219
+ shared_container_iterator<Container>
220
+ >
221
+ make_shared_container_range(std::shared_ptr<Container> const& container);
222
+
223
+ // Backward compatibility with boost::shared_ptr
190
224
template <typename Container>
191
225
std::pair<
192
226
shared_container_iterator<Container>,
193
227
shared_container_iterator<Container>
194
228
>
195
229
make_shared_container_range(boost::shared_ptr<Container> const& container);
196
- Class shared_container_iterator is meant primarily to return, using iterators, a range of values that we can guarantee will be alive as long as the iterators are. This is a convenience function to do just that. It is equivalent to
197
- std::make_pair(make_shared_container_iterator(container->begin(),container),
198
- make_shared_container_iterator(container->end(),container));
230
+
231
+ Class `shared_container_iterator` is meant primarily to return, using iterators,
232
+ a range of values that we can guarantee will be alive as long as the iterators are.
233
+ This is a convenience function to do just that. It is functionally equivalent to this:
234
+
235
+ std::make_pair(make_shared_container_iterator(container->begin(), container),
236
+ make_shared_container_iterator(container->end(), container));
199
237
200
238
[h2 Example]
201
239
202
- In the following example, a range of values is returned as a pair of shared_container_iterator objects.
240
+ In the following example, a range of values is returned as a pair of ` shared_container_iterator` objects.
203
241
204
242
[example_link shared_iterator_example3.cpp..`shared_iterator_example3.cpp`]:
205
243
206
- #include "shared_container_iterator.hpp"
207
- #include "boost/shared_ptr.hpp"
208
- #include "boost/tuple/tuple.hpp" // for boost::tie
209
- #include <algorithm> // for std::copy
244
+ #include <boost/iterator/shared_container_iterator.hpp>
245
+ #include <algorithm> // for std::copy
210
246
#include <iostream>
211
247
#include <vector>
248
+ #include <memory>
249
+ #include <tuple> // for std::tie
212
250
213
251
214
- typedef boost::shared_container_iterator< std::vector<int> > iterator ;
252
+ using iterator = boost::iterators:: shared_container_iterator< std::vector<int> >;
215
253
216
- std::pair<iterator,iterator>
254
+ std::pair<iterator, iterator>
217
255
return_range() {
218
- boost ::shared_ptr< std::vector<int> > range(new std::vector<int>());
256
+ std ::shared_ptr< std::vector<int> > range(new std::vector<int>());
219
257
range->push_back(0);
220
258
range->push_back(1);
221
259
range->push_back(2);
222
260
range->push_back(3);
223
261
range->push_back(4);
224
262
range->push_back(5);
225
- return boost::make_shared_container_range(range);
263
+ return boost::iterators:: make_shared_container_range(range);
226
264
}
227
265
228
-
229
266
int main() {
230
-
231
-
232
267
iterator i,end;
268
+ std::tie(i, end) = return_range();
233
269
234
- boost::tie(i,end) = return_range();
235
-
236
- std::copy(i,end,std::ostream_iterator<int>(std::cout,","));
270
+ std::copy(i, end, std::ostream_iterator<int>(std::cout, ","));
237
271
std::cout.put('\n');
238
272
239
273
return 0;
0 commit comments