shared_container_iterator.qbk 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. [section:shared_container Shared Container Iterator]
  2. Defined in header [@../../../boost/shared_container_iterator.hpp `boost/shared_container_iterator.hpp`].
  3. The purpose of the shared container iterator is to attach the lifetime
  4. of a container to the lifetime of its iterators. In other words, the
  5. container will not be deleted until after all its iterators are
  6. destroyed. The shared container iterator is typically used to
  7. implement functions that return iterators over a range of objects that
  8. only need to exist for the lifetime of the iterators. By returning a
  9. pair of shared iterators from a function, the callee can return a
  10. heap-allocated range of objects whose lifetime is automatically
  11. managed.
  12. The shared container iterator augments an iterator over a shared
  13. container. It maintains a reference count on the shared container. If
  14. only shared container iterators hold references to the container, the
  15. container's lifetime will end when the last shared container iterator
  16. over it is destroyed. In any case, the shared container is guaranteed
  17. to persist beyond the lifetime of all the iterators. In all other
  18. ways, the shared container iterator behaves the same as its base
  19. iterator.
  20. [h2 Synopsis]
  21. namespace boost {
  22. template <typename Container>
  23. class shared_container_iterator;
  24. template <typename Container>
  25. shared_container_iterator<Container>
  26. make_shared_container_iterator(typename Container::iterator base,
  27. boost::shared_ptr<Container> const& container);
  28. std::pair<
  29. typename shared_container_iterator<Container>,
  30. typename shared_container_iterator<Container>
  31. >
  32. make_shared_container_range(boost::shared_ptr<Container> const& container);
  33. }
  34. [section:shared_container_type The Shared Container Iterator Type]
  35. template <typename Container> class shared_container_iterator;
  36. The class template `shared_container_iterator` is the shared container
  37. iterator type. The `Container` template type argument must model the
  38. [@http://www.sgi.com/tech/stl/Container.html Container] concept.
  39. [h2 Example]
  40. The following example illustrates how to create an iterator that
  41. regulates the lifetime of a reference counted `std::vector`. Though the
  42. original shared pointer `ints` ceases to exist after `set_range()`
  43. returns, the `shared_counter_iterator` objects maintain references to
  44. the underlying vector and thereby extend the container's lifetime.
  45. [@../../example/shared_iterator_example1.cpp `shared_iterator_example1.cpp`]:
  46. #include "shared_container_iterator.hpp"
  47. #include "boost/shared_ptr.hpp"
  48. #include <algorithm>
  49. #include <iostream>
  50. #include <vector>
  51. typedef boost::shared_container_iterator< std::vector<int> > iterator;
  52. void set_range(iterator& i, iterator& end) {
  53. boost::shared_ptr< std::vector<int> > ints(new std::vector<int>());
  54. ints->push_back(0);
  55. ints->push_back(1);
  56. ints->push_back(2);
  57. ints->push_back(3);
  58. ints->push_back(4);
  59. ints->push_back(5);
  60. i = iterator(ints->begin(),ints);
  61. end = iterator(ints->end(),ints);
  62. }
  63. int main() {
  64. iterator i,end;
  65. set_range(i,end);
  66. std::copy(i,end,std::ostream_iterator<int>(std::cout,","));
  67. std::cout.put('\n');
  68. return 0;
  69. }
  70. The output from this part is:
  71. 0,1,2,3,4,5,
  72. [table Template Parameters
  73. [[Parameter][Description]]
  74. [[Container][The type of the container that we wish to iterate over. It must be a model of the Container concept.]]
  75. ]
  76. [h2 Concepts]
  77. The `shared_container_iterator` type models the same iterator concept
  78. as the base iterator (`Container::iterator`).
  79. [h2 Operations]
  80. The `shared_container_iterator` type implements the member functions
  81. and operators required of the
  82. [@http://www.sgi.com/tech/stl/RandomAccessIterator.html Random Access
  83. Iterator] concept, though only operations defined for the base
  84. iterator will be valid. In addition it has the following constructor:
  85. shared_container_iterator(Container::iterator const& it,
  86. boost::shared_ptr<Container> const& container)
  87. [endsect]
  88. [section:shared_container_object_generator The Shared Container Iterator Object Generator]
  89. template <typename Container>
  90. shared_container_iterator<Container>
  91. make_shared_container_iterator(Container::iterator base,
  92. boost::shared_ptr<Container> const& container)
  93. This function provides an alternative to directly constructing a
  94. `shared_container_iterator`. Using the object generator, a
  95. `shared_container_iterator` can be created and passed to a function without
  96. explicitly specifying its type.
  97. [h2 Example]
  98. This example, similar to the previous,
  99. uses `make_shared_container_iterator()` to create the iterators.
  100. [@../../example/shared_iterator_example2.cpp `shared_iterator_example2.cpp`]:
  101. #include "shared_container_iterator.hpp"
  102. #include "boost/shared_ptr.hpp"
  103. #include <algorithm>
  104. #include <iterator>
  105. #include <iostream>
  106. #include <vector>
  107. template <typename Iterator>
  108. void print_range_nl (Iterator begin, Iterator end) {
  109. typedef typename std::iterator_traits<Iterator>::value_type val;
  110. std::copy(begin,end,std::ostream_iterator<val>(std::cout,","));
  111. std::cout.put('\n');
  112. }
  113. int main() {
  114. typedef boost::shared_ptr< std::vector<int> > ints_t;
  115. {
  116. ints_t ints(new std::vector<int>());
  117. ints->push_back(0);
  118. ints->push_back(1);
  119. ints->push_back(2);
  120. ints->push_back(3);
  121. ints->push_back(4);
  122. ints->push_back(5);
  123. print_range_nl(boost::make_shared_container_iterator(ints->begin(),ints),
  124. boost::make_shared_container_iterator(ints->end(),ints));
  125. }
  126. return 0;
  127. }
  128. Observe that the `shared_container_iterator` type is never explicitly
  129. named. The output from this example is the same as the previous.
  130. [endsect]
  131. [section:shared_container_generator The Shared Container Iterator Range Generator]
  132. template <typename Container>
  133. std::pair<
  134. shared_container_iterator<Container>,
  135. shared_container_iterator<Container>
  136. >
  137. make_shared_container_range(boost::shared_ptr<Container> const& container);
  138. 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
  139. std::make_pair(make_shared_container_iterator(container->begin(),container),
  140. make_shared_container_iterator(container->end(),container));
  141. [h2 Example]
  142. In the following example, a range of values is returned as a pair of shared_container_iterator objects.
  143. [@../../example/shared_iterator_example3.cpp `shared_iterator_example3.cpp`]:
  144. #include "shared_container_iterator.hpp"
  145. #include "boost/shared_ptr.hpp"
  146. #include "boost/tuple/tuple.hpp" // for boost::tie
  147. #include <algorithm> // for std::copy
  148. #include <iostream>
  149. #include <vector>
  150. typedef boost::shared_container_iterator< std::vector<int> > iterator;
  151. std::pair<iterator,iterator>
  152. return_range() {
  153. boost::shared_ptr< std::vector<int> > range(new std::vector<int>());
  154. range->push_back(0);
  155. range->push_back(1);
  156. range->push_back(2);
  157. range->push_back(3);
  158. range->push_back(4);
  159. range->push_back(5);
  160. return boost::make_shared_container_range(range);
  161. }
  162. int main() {
  163. iterator i,end;
  164. boost::tie(i,end) = return_range();
  165. std::copy(i,end,std::ostream_iterator<int>(std::cout,","));
  166. std::cout.put('\n');
  167. return 0;
  168. }
  169. Though the range object only lives for the duration of the
  170. `return_range` call, the reference counted `std::vector` will live
  171. until `i` and `end` are both destroyed. The output from this example is
  172. the same as the previous two.
  173. [endsect]
  174. [endsect]