boost_tuple_iterator.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #ifdef _MSC_VER
  39. # pragma warning(push)
  40. # pragma warning(disable: 4512) // assignment operator could not be generated.
  41. #endif
  42. template <typename Cons = tuples::null_type>
  43. struct boost_tuple_iterator
  44. : iterator_facade<boost_tuple_iterator<Cons>, forward_traversal_tag>
  45. {
  46. typedef Cons cons_type;
  47. typedef boost_tuple_iterator_identity<
  48. typename add_const<Cons>::type> identity;
  49. BOOST_FUSION_GPU_ENABLED
  50. explicit boost_tuple_iterator(Cons& in_cons)
  51. : cons(in_cons) {}
  52. Cons& cons;
  53. template <typename Iterator>
  54. struct value_of : mpl::identity<typename Iterator::cons_type::head_type> {};
  55. template <typename Iterator>
  56. struct deref
  57. {
  58. typedef typename value_of<Iterator>::type element;
  59. typedef typename
  60. mpl::if_<
  61. is_const<typename Iterator::cons_type>
  62. , typename tuples::access_traits<element>::const_type
  63. , typename tuples::access_traits<element>::non_const_type
  64. >::type
  65. type;
  66. BOOST_FUSION_GPU_ENABLED
  67. static type
  68. call(Iterator const& iter)
  69. {
  70. return iter.cons.get_head();
  71. }
  72. };
  73. template <typename Iterator>
  74. struct next
  75. {
  76. typedef typename Iterator::cons_type cons_type;
  77. typedef typename cons_type::tail_type tail_type;
  78. typedef boost_tuple_iterator<
  79. typename mpl::eval_if<
  80. is_const<cons_type>
  81. , add_const<tail_type>
  82. , mpl::identity<tail_type>
  83. >::type>
  84. type;
  85. BOOST_FUSION_GPU_ENABLED
  86. static type
  87. call(Iterator const& iter)
  88. {
  89. return type(iter.cons.get_tail());
  90. }
  91. };
  92. template <typename I1, typename I2>
  93. struct distance;
  94. // detail
  95. template <typename I1, typename I2>
  96. struct lazy_next_distance
  97. {
  98. typedef
  99. typename mpl::plus<
  100. mpl::int_<1>,
  101. typename distance<
  102. typename next<I1>::type,
  103. I2
  104. >::type
  105. >::type type;
  106. };
  107. template <typename I1, typename I2>
  108. struct distance
  109. {
  110. typedef typename mpl::eval_if<
  111. boost::is_same<I1, I2>,
  112. mpl::int_<0>,
  113. lazy_next_distance<I1, I2>
  114. >::type type;
  115. BOOST_FUSION_GPU_ENABLED
  116. static type
  117. call(I1 const&, I2 const&)
  118. {
  119. return type();
  120. }
  121. };
  122. template <typename I1, typename I2>
  123. struct equal_to
  124. : is_same<typename I1::identity, typename I2::identity>
  125. {};
  126. };
  127. #ifdef _MSC_VER
  128. # pragma warning(pop)
  129. #endif
  130. template <typename Null>
  131. struct boost_tuple_null_iterator
  132. : iterator_facade<boost_tuple_iterator<Null>, forward_traversal_tag>
  133. {
  134. typedef Null cons_type;
  135. typedef boost_tuple_iterator_identity<
  136. typename add_const<Null>::type> identity;
  137. template <typename I1, typename I2>
  138. struct equal_to
  139. : mpl::or_<
  140. is_same<I1, I2>
  141. , mpl::and_<
  142. detail::boost_tuple_is_empty<typename I1::cons_type>
  143. , detail::boost_tuple_is_empty<typename I2::cons_type>
  144. >
  145. >
  146. {};
  147. };
  148. template <>
  149. struct boost_tuple_iterator<tuples::null_type>
  150. : boost_tuple_null_iterator<tuples::null_type>
  151. {
  152. template <typename Cons>
  153. BOOST_FUSION_GPU_ENABLED
  154. explicit boost_tuple_iterator(Cons const&) {}
  155. };
  156. template <>
  157. struct boost_tuple_iterator<tuples::null_type const>
  158. : boost_tuple_null_iterator<tuples::null_type const>
  159. {
  160. template <typename Cons>
  161. BOOST_FUSION_GPU_ENABLED
  162. explicit boost_tuple_iterator(Cons const&) {}
  163. };
  164. template <>
  165. struct boost_tuple_iterator<tuples::tuple<> >
  166. : boost_tuple_null_iterator<tuples::tuple<> >
  167. {
  168. template <typename Cons>
  169. BOOST_FUSION_GPU_ENABLED
  170. explicit boost_tuple_iterator(Cons const&) {}
  171. };
  172. template <>
  173. struct boost_tuple_iterator<tuples::tuple<> const>
  174. : boost_tuple_null_iterator<tuples::tuple<> const>
  175. {
  176. template <typename Cons>
  177. BOOST_FUSION_GPU_ENABLED
  178. explicit boost_tuple_iterator(Cons const&) {}
  179. };
  180. }}
  181. #ifdef BOOST_FUSION_WORKAROUND_FOR_LWG_2408
  182. namespace std
  183. {
  184. template <typename Cons>
  185. struct iterator_traits< ::boost::fusion::boost_tuple_iterator<Cons> >
  186. { };
  187. }
  188. #endif
  189. #endif