iterator_categories.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // (C) Copyright Jeremy Siek 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_ITERATOR_CATEGORIES_HPP
  6. # define BOOST_ITERATOR_CATEGORIES_HPP
  7. # include <boost/config.hpp>
  8. # include <boost/iterator/detail/config_def.hpp>
  9. # include <boost/detail/workaround.hpp>
  10. # include <boost/mpl/eval_if.hpp>
  11. # include <boost/mpl/identity.hpp>
  12. # include <boost/mpl/placeholders.hpp>
  13. # include <boost/mpl/aux_/lambda_support.hpp>
  14. # include <boost/type_traits/is_convertible.hpp>
  15. # include <boost/static_assert.hpp>
  16. #include <iterator>
  17. namespace boost {
  18. namespace iterators {
  19. //
  20. // Traversal Categories
  21. //
  22. struct no_traversal_tag {};
  23. struct incrementable_traversal_tag
  24. : no_traversal_tag
  25. {
  26. // incrementable_traversal_tag() {}
  27. // incrementable_traversal_tag(std::output_iterator_tag const&) {};
  28. };
  29. struct single_pass_traversal_tag
  30. : incrementable_traversal_tag
  31. {
  32. // single_pass_traversal_tag() {}
  33. // single_pass_traversal_tag(std::input_iterator_tag const&) {};
  34. };
  35. struct forward_traversal_tag
  36. : single_pass_traversal_tag
  37. {
  38. // forward_traversal_tag() {}
  39. // forward_traversal_tag(std::forward_iterator_tag const&) {};
  40. };
  41. struct bidirectional_traversal_tag
  42. : forward_traversal_tag
  43. {
  44. // bidirectional_traversal_tag() {};
  45. // bidirectional_traversal_tag(std::bidirectional_iterator_tag const&) {};
  46. };
  47. struct random_access_traversal_tag
  48. : bidirectional_traversal_tag
  49. {
  50. // random_access_traversal_tag() {};
  51. // random_access_traversal_tag(std::random_access_iterator_tag const&) {};
  52. };
  53. namespace detail
  54. {
  55. //
  56. // Convert a "strictly old-style" iterator category to a traversal
  57. // tag. This is broken out into a separate metafunction to reduce
  58. // the cost of instantiating iterator_category_to_traversal, below,
  59. // for new-style types.
  60. //
  61. template <class Cat>
  62. struct old_category_to_traversal
  63. : mpl::eval_if<
  64. is_convertible<Cat,std::random_access_iterator_tag>
  65. , mpl::identity<random_access_traversal_tag>
  66. , mpl::eval_if<
  67. is_convertible<Cat,std::bidirectional_iterator_tag>
  68. , mpl::identity<bidirectional_traversal_tag>
  69. , mpl::eval_if<
  70. is_convertible<Cat,std::forward_iterator_tag>
  71. , mpl::identity<forward_traversal_tag>
  72. , mpl::eval_if<
  73. is_convertible<Cat,std::input_iterator_tag>
  74. , mpl::identity<single_pass_traversal_tag>
  75. , mpl::eval_if<
  76. is_convertible<Cat,std::output_iterator_tag>
  77. , mpl::identity<incrementable_traversal_tag>
  78. , void
  79. >
  80. >
  81. >
  82. >
  83. >
  84. {};
  85. } // namespace detail
  86. //
  87. // Convert an iterator category into a traversal tag
  88. //
  89. template <class Cat>
  90. struct iterator_category_to_traversal
  91. : mpl::eval_if< // if already convertible to a traversal tag, we're done.
  92. is_convertible<Cat,incrementable_traversal_tag>
  93. , mpl::identity<Cat>
  94. , boost::iterators::detail::old_category_to_traversal<Cat>
  95. >
  96. {};
  97. // Trait to get an iterator's traversal category
  98. template <class Iterator = mpl::_1>
  99. struct iterator_traversal
  100. : iterator_category_to_traversal<
  101. typename std::iterator_traits<Iterator>::iterator_category
  102. >
  103. {};
  104. # ifdef BOOST_MPL_CFG_NO_FULL_LAMBDA_SUPPORT
  105. // Hack because BOOST_MPL_AUX_LAMBDA_SUPPORT doesn't seem to work
  106. // out well. Instantiating the nested apply template also
  107. // requires instantiating iterator_traits on the
  108. // placeholder. Instead we just specialize it as a metafunction
  109. // class.
  110. template <>
  111. struct iterator_traversal<mpl::_1>
  112. {
  113. template <class T>
  114. struct apply : iterator_traversal<T>
  115. {};
  116. };
  117. template <>
  118. struct iterator_traversal<mpl::_>
  119. : iterator_traversal<mpl::_1>
  120. {};
  121. # endif
  122. //
  123. // Convert an iterator traversal to one of the traversal tags.
  124. //
  125. template <class Traversal>
  126. struct pure_traversal_tag
  127. : mpl::eval_if<
  128. is_convertible<Traversal,random_access_traversal_tag>
  129. , mpl::identity<random_access_traversal_tag>
  130. , mpl::eval_if<
  131. is_convertible<Traversal,bidirectional_traversal_tag>
  132. , mpl::identity<bidirectional_traversal_tag>
  133. , mpl::eval_if<
  134. is_convertible<Traversal,forward_traversal_tag>
  135. , mpl::identity<forward_traversal_tag>
  136. , mpl::eval_if<
  137. is_convertible<Traversal,single_pass_traversal_tag>
  138. , mpl::identity<single_pass_traversal_tag>
  139. , mpl::eval_if<
  140. is_convertible<Traversal,incrementable_traversal_tag>
  141. , mpl::identity<incrementable_traversal_tag>
  142. , void
  143. >
  144. >
  145. >
  146. >
  147. >
  148. {
  149. };
  150. //
  151. // Trait to retrieve one of the iterator traversal tags from the iterator category or traversal.
  152. //
  153. template <class Iterator = mpl::_1>
  154. struct pure_iterator_traversal
  155. : pure_traversal_tag<typename iterator_traversal<Iterator>::type>
  156. {};
  157. # ifdef BOOST_MPL_CFG_NO_FULL_LAMBDA_SUPPORT
  158. template <>
  159. struct pure_iterator_traversal<mpl::_1>
  160. {
  161. template <class T>
  162. struct apply : pure_iterator_traversal<T>
  163. {};
  164. };
  165. template <>
  166. struct pure_iterator_traversal<mpl::_>
  167. : pure_iterator_traversal<mpl::_1>
  168. {};
  169. # endif
  170. } // namespace iterators
  171. using iterators::no_traversal_tag;
  172. using iterators::incrementable_traversal_tag;
  173. using iterators::single_pass_traversal_tag;
  174. using iterators::forward_traversal_tag;
  175. using iterators::bidirectional_traversal_tag;
  176. using iterators::random_access_traversal_tag;
  177. using iterators::iterator_category_to_traversal;
  178. using iterators::iterator_traversal;
  179. // This import is needed for backward compatibility with Boost.Range:
  180. // boost/range/detail/demote_iterator_traversal_tag.hpp
  181. // It should be removed when that header is fixed.
  182. namespace detail {
  183. using iterators::pure_traversal_tag;
  184. } // namespace detail
  185. } // namespace boost
  186. #include <boost/iterator/detail/config_undef.hpp>
  187. #endif // BOOST_ITERATOR_CATEGORIES_HPP