indirect_iterator.qbk 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. [section:indirect Indirect Iterator]
  2. `indirect_iterator` adapts an iterator by applying an
  3. *extra* dereference inside of `operator*()`. For example, this
  4. iterator adaptor makes it possible to view a container of pointers
  5. (e.g. `list<foo*>`) as if it were a container of the pointed-to type
  6. (e.g. `list<foo>`). `indirect_iterator` depends on two
  7. auxiliary traits, `pointee` and `indirect_reference`, to
  8. provide support for underlying iterators whose `value_type` is
  9. not an iterator.
  10. [h2 Example]
  11. This example prints an array of characters, using
  12. `indirect_iterator` to access the array of characters through an
  13. array of pointers. Next `indirect_iterator` is used with the
  14. `transform` algorithm to copy the characters (incremented by one) to
  15. another array. A constant indirect iterator is used for the source and
  16. a mutable indirect iterator is used for the destination. The last part
  17. of the example prints the original array of characters, but this time
  18. using the `make_indirect_iterator` helper function.
  19. char characters[] = "abcdefg";
  20. const int N = sizeof(characters)/sizeof(char) - 1; // -1 since characters has a null char
  21. char* pointers_to_chars[N]; // at the end.
  22. for (int i = 0; i < N; ++i)
  23. pointers_to_chars[i] = &characters[i];
  24. // Example of using indirect_iterator
  25. boost::indirect_iterator<char**, char>
  26. indirect_first(pointers_to_chars), indirect_last(pointers_to_chars + N);
  27. std::copy(indirect_first, indirect_last, std::ostream_iterator<char>(std::cout, ","));
  28. std::cout << std::endl;
  29. // Example of making mutable and constant indirect iterators
  30. char mutable_characters[N];
  31. char* pointers_to_mutable_chars[N];
  32. for (int j = 0; j < N; ++j)
  33. pointers_to_mutable_chars[j] = &mutable_characters[j];
  34. boost::indirect_iterator<char* const*> mutable_indirect_first(pointers_to_mutable_chars),
  35. mutable_indirect_last(pointers_to_mutable_chars + N);
  36. boost::indirect_iterator<char* const*, char const> const_indirect_first(pointers_to_chars),
  37. const_indirect_last(pointers_to_chars + N);
  38. std::transform(const_indirect_first, const_indirect_last,
  39. mutable_indirect_first, std::bind1st(std::plus<char>(), 1));
  40. std::copy(mutable_indirect_first, mutable_indirect_last,
  41. std::ostream_iterator<char>(std::cout, ","));
  42. std::cout << std::endl;
  43. // Example of using make_indirect_iterator()
  44. std::copy(boost::make_indirect_iterator(pointers_to_chars),
  45. boost::make_indirect_iterator(pointers_to_chars + N),
  46. std::ostream_iterator<char>(std::cout, ","));
  47. std::cout << std::endl;
  48. The output is:
  49. a,b,c,d,e,f,g,
  50. b,c,d,e,f,g,h,
  51. a,b,c,d,e,f,g,
  52. The source code for this example can be found
  53. [@../example/indirect_iterator_example.cpp here].
  54. [h2 Reference]
  55. [h3 Synopsis]
  56. template <
  57. class Iterator
  58. , class Value = use_default
  59. , class CategoryOrTraversal = use_default
  60. , class Reference = use_default
  61. , class Difference = use_default
  62. >
  63. class indirect_iterator
  64. {
  65. public:
  66. typedef /* see below */ value_type;
  67. typedef /* see below */ reference;
  68. typedef /* see below */ pointer;
  69. typedef /* see below */ difference_type;
  70. typedef /* see below */ iterator_category;
  71. indirect_iterator();
  72. indirect_iterator(Iterator x);
  73. template <
  74. class Iterator2, class Value2, class Category2
  75. , class Reference2, class Difference2
  76. >
  77. indirect_iterator(
  78. indirect_iterator<
  79. Iterator2, Value2, Category2, Reference2, Difference2
  80. > const& y
  81. , typename enable_if_convertible<Iterator2, Iterator>::type* = 0 // exposition
  82. );
  83. Iterator const& base() const;
  84. reference operator*() const;
  85. indirect_iterator& operator++();
  86. indirect_iterator& operator--();
  87. private:
  88. Iterator m_iterator; // exposition
  89. };
  90. The member types of `indirect_iterator` are defined according to
  91. the following pseudo-code, where `V` is
  92. `iterator_traits<Iterator>::value_type`
  93. [pre
  94. if (Value is use_default) then
  95. typedef remove_const<pointee<V>::type>::type value_type;
  96. else
  97. typedef remove_const<Value>::type value_type;
  98. if (Reference is use_default) then
  99. if (Value is use_default) then
  100. typedef indirect_reference<V>::type reference;
  101. else
  102. typedef Value& reference;
  103. else
  104. typedef Reference reference;
  105. if (Value is use_default) then
  106. typedef pointee<V>::type\* pointer;
  107. else
  108. typedef Value\* pointer;
  109. if (Difference is use_default)
  110. typedef iterator_traits<Iterator>::difference_type difference_type;
  111. else
  112. typedef Difference difference_type;
  113. if (CategoryOrTraversal is use_default)
  114. typedef *iterator-category* (
  115. iterator_traversal<Iterator>::type,`reference`,`value_type`
  116. ) iterator_category;
  117. else
  118. typedef *iterator-category* (
  119. CategoryOrTraversal,`reference`,`value_type`
  120. ) iterator_category;
  121. ]
  122. [h3 Requirements]
  123. The expression `*v`, where `v` is an object of
  124. `iterator_traits<Iterator>::value_type`, shall be valid
  125. expression and convertible to `reference`. `Iterator` shall
  126. model the traversal concept indicated by `iterator_category`.
  127. `Value`, `Reference`, and `Difference` shall be chosen so
  128. that `value_type`, `reference`, and `difference_type` meet
  129. the requirements indicated by `iterator_category`.
  130. [blurb Note: there are further requirements on the
  131. `iterator_traits<Iterator>::value_type` if the `Value`
  132. parameter is not `use_default`, as implied by the algorithm for
  133. deducing the default for the `value_type` member.]
  134. [h3 Concepts]
  135. In addition to the concepts indicated by `iterator_category`
  136. and by `iterator_traversal<indirect_iterator>::type`, a
  137. specialization of `indirect_iterator` models the following
  138. concepts, Where `v` is an object of
  139. `iterator_traits<Iterator>::value_type`:
  140. Readable Iterator if `reference(*v)` is convertible to
  141. `value_type`.
  142. Writable Iterator if `reference(*v) = t` is a valid
  143. expression (where `t` is an object of type
  144. `indirect_iterator::value_type`)
  145. Lvalue Iterator if `reference` is a reference type.
  146. `indirect_iterator<X,V1,C1,R1,D1>` is interoperable with
  147. `indirect_iterator<Y,V2,C2,R2,D2>` if and only if `X` is
  148. interoperable with `Y`.
  149. [h3 Operations]
  150. In addition to the operations required by the concepts described
  151. above, specializations of `indirect_iterator` provide the
  152. following operations:
  153. indirect_iterator();
  154. [*Requires: ] `Iterator` must be Default Constructible.[br]
  155. [*Effects: ] Constructs an instance of `indirect_iterator` with
  156. a default-constructed `m_iterator`.
  157. indirect_iterator(Iterator x);
  158. [*Effects: ] Constructs an instance of `indirect_iterator` with
  159. `m_iterator` copy constructed from `x`.
  160. template <
  161. class Iterator2, class Value2, unsigned Access, class Traversal
  162. , class Reference2, class Difference2
  163. >
  164. indirect_iterator(
  165. indirect_iterator<
  166. Iterator2, Value2, Access, Traversal, Reference2, Difference2
  167. > const& y
  168. , typename enable_if_convertible<Iterator2, Iterator>::type* = 0 // exposition
  169. );
  170. [*Requires: ] `Iterator2` is implicitly convertible to `Iterator`.[br]
  171. [*Effects: ] Constructs an instance of `indirect_iterator` whose
  172. `m_iterator` subobject is constructed from `y.base()`.
  173. Iterator const& base() const;
  174. [*Returns: ] `m_iterator`
  175. reference operator*() const;
  176. [*Returns: ] `**m_iterator`
  177. indirect_iterator& operator++();
  178. [*Effects: ] `++m_iterator`[br]
  179. [*Returns: ] `*this`
  180. indirect_iterator& operator--();
  181. [*Effects: ] `--m_iterator`[br]
  182. [*Returns: ] `*this`
  183. [endsect]