boost_tuple_iterator.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(FUSION_BOOST_TUPLE_ITERATOR_09262006_1851)
  7. #define FUSION_BOOST_TUPLE_ITERATOR_09262006_1851
  8. #include <boost/fusion/support/config.hpp>
  9. #include <boost/fusion/iterator/iterator_facade.hpp>
  10. #include <boost/type_traits/is_const.hpp>
  11. #include <boost/type_traits/add_const.hpp>
  12. #include <boost/mpl/identity.hpp>
  13. #include <boost/mpl/if.hpp>
  14. #include <boost/mpl/eval_if.hpp>
  15. #include <boost/mpl/or.hpp>
  16. #include <boost/mpl/plus.hpp>
  17. #include <boost/mpl/int.hpp>
  18. #include <boost/mpl/apply.hpp>
  19. #include <boost/tuple/tuple.hpp>
  20. namespace boost { namespace fusion
  21. {
  22. struct forward_traversal_tag;
  23. namespace detail
  24. {
  25. template <typename T>
  26. struct boost_tuple_is_empty : mpl::false_ {};
  27. template <>
  28. struct boost_tuple_is_empty<tuples::null_type> : mpl::true_ {};
  29. template <>
  30. struct boost_tuple_is_empty<tuples::null_type const> : mpl::true_ {};
  31. template <>
  32. struct boost_tuple_is_empty<tuples::tuple<> > : mpl::true_ {};
  33. template <>
  34. struct boost_tuple_is_empty<tuples::tuple<> const> : mpl::true_ {};
  35. }
  36. template <typename Cons>
  37. struct boost_tuple_iterator_identity;
  38. template <typename Cons = tuples::null_type>
  39. struct boost_tuple_iterator
  40. : iterator_facade<boost_tuple_iterator<Cons>, forward_traversal_tag>
  41. {
  42. typedef Cons cons_type;
  43. typedef boost_tuple_iterator_identity<
  44. typename add_const<Cons>::type> identity;
  45. BOOST_FUSION_GPU_ENABLED
  46. explicit boost_tuple_iterator(Cons& in_cons)
  47. : cons(in_cons) {}
  48. Cons& cons;
  49. template <typename Iterator>
  50. struct value_of : mpl::identity<typename Iterator::cons_type::head_type> {};
  51. template <typename Iterator>
  52. struct deref
  53. {
  54. typedef typename value_of<Iterator>::type element;
  55. typedef typename
  56. mpl::if_<
  57. is_const<typename Iterator::cons_type>
  58. , typename tuples::access_traits<element>::const_type
  59. , typename tuples::access_traits<element>::non_const_type
  60. >::type
  61. type;
  62. BOOST_FUSION_GPU_ENABLED
  63. static type
  64. call(Iterator const& iter)
  65. {
  66. return iter.cons.get_head();
  67. }
  68. };
  69. template <typename Iterator>
  70. struct next
  71. {
  72. typedef typename Iterator::cons_type cons_type;
  73. typedef typename cons_type::tail_type tail_type;
  74. typedef boost_tuple_iterator<
  75. typename mpl::eval_if<
  76. is_const<cons_type>
  77. , add_const<tail_type>
  78. , mpl::identity<tail_type>
  79. >::type>
  80. type;
  81. BOOST_FUSION_GPU_ENABLED
  82. static type
  83. call(Iterator const& iter)
  84. {
  85. return type(iter.cons.get_tail());
  86. }
  87. };
  88. template <typename I1, typename I2>
  89. struct distance;
  90. // detail
  91. template <typename I1, typename I2>
  92. struct lazy_next_distance
  93. {
  94. typedef
  95. typename mpl::plus<
  96. mpl::int_<1>,
  97. typename distance<
  98. typename next<I1>::type,
  99. I2
  100. >::type
  101. >::type type;
  102. };
  103. template <typename I1, typename I2>
  104. struct distance
  105. {
  106. typedef typename mpl::eval_if<
  107. boost::is_same<I1, I2>,
  108. mpl::int_<0>,
  109. lazy_next_distance<I1, I2>
  110. >::type type;
  111. BOOST_FUSION_GPU_ENABLED
  112. static type
  113. call(I1 const&, I2 const&)
  114. {
  115. return type();
  116. }
  117. };
  118. template <typename I1, typename I2>
  119. struct equal_to
  120. : is_same<typename I1::identity, typename I2::identity>
  121. {};
  122. private:
  123. // silence MSVC warning C4512: assignment operator could not be generated
  124. boost_tuple_iterator& operator= (boost_tuple_iterator const&);
  125. };
  126. template <typename Null>
  127. struct boost_tuple_null_iterator
  128. : iterator_facade<boost_tuple_iterator<Null>, forward_traversal_tag>
  129. {
  130. typedef Null cons_type;
  131. typedef boost_tuple_iterator_identity<
  132. typename add_const<Null>::type> identity;
  133. template <typename I1, typename I2>
  134. struct equal_to
  135. : mpl::or_<
  136. is_same<I1, I2>
  137. , mpl::and_<
  138. detail::boost_tuple_is_empty<typename I1::cons_type>
  139. , detail::boost_tuple_is_empty<typename I2::cons_type>
  140. >
  141. >
  142. {};
  143. };
  144. template <>
  145. struct boost_tuple_iterator<tuples::null_type>
  146. : boost_tuple_null_iterator<tuples::null_type>
  147. {
  148. template <typename Cons>
  149. BOOST_FUSION_GPU_ENABLED
  150. explicit boost_tuple_iterator(Cons const&) {}
  151. };
  152. template <>
  153. struct boost_tuple_iterator<tuples::null_type const>
  154. : boost_tuple_null_iterator<tuples::null_type const>
  155. {
  156. template <typename Cons>
  157. BOOST_FUSION_GPU_ENABLED
  158. explicit boost_tuple_iterator(Cons const&) {}
  159. };
  160. template <>
  161. struct boost_tuple_iterator<tuples::tuple<> >
  162. : boost_tuple_null_iterator<tuples::tuple<> >
  163. {
  164. template <typename Cons>
  165. BOOST_FUSION_GPU_ENABLED
  166. explicit boost_tuple_iterator(Cons const&) {}
  167. };
  168. template <>
  169. struct boost_tuple_iterator<tuples::tuple<> const>
  170. : boost_tuple_null_iterator<tuples::tuple<> const>
  171. {
  172. template <typename Cons>
  173. BOOST_FUSION_GPU_ENABLED
  174. explicit boost_tuple_iterator(Cons const&) {}
  175. };
  176. }}
  177. #ifdef BOOST_FUSION_WORKAROUND_FOR_LWG_2408
  178. namespace std
  179. {
  180. template <typename Cons>
  181. struct iterator_traits< ::boost::fusion::boost_tuple_iterator<Cons> >
  182. { };
  183. }
  184. #endif
  185. #endif