std_tuple_iterator.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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_STD_TUPLE_ITERATOR_09112011_1905)
  7. #define FUSION_STD_TUPLE_ITERATOR_09112011_1905
  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/remove_const.hpp>
  12. #include <boost/fusion/support/detail/access.hpp>
  13. #include <boost/mpl/int.hpp>
  14. #include <boost/mpl/if.hpp>
  15. #include <tuple>
  16. #include <utility>
  17. namespace boost { namespace fusion
  18. {
  19. struct random_access_traversal_tag;
  20. template <typename Tuple, int Index>
  21. struct std_tuple_iterator_identity;
  22. template <typename Tuple, int Index>
  23. struct std_tuple_iterator
  24. : iterator_facade<
  25. std_tuple_iterator<Tuple, Index>
  26. , random_access_traversal_tag>
  27. {
  28. typedef Tuple tuple_type;
  29. static int const index = Index;
  30. typedef std_tuple_iterator_identity<
  31. typename add_const<Tuple>::type, Index>
  32. identity;
  33. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  34. explicit std_tuple_iterator(Tuple& tuple)
  35. : tuple(tuple) {}
  36. Tuple& tuple;
  37. template <typename Iterator>
  38. struct value_of
  39. : std::tuple_element<Iterator::index,
  40. typename remove_const<typename Iterator::tuple_type>::type> {};
  41. template <typename Iterator>
  42. struct deref
  43. {
  44. typedef typename value_of<Iterator>::type element;
  45. typedef typename
  46. mpl::if_<
  47. is_const<typename Iterator::tuple_type>
  48. , typename fusion::detail::cref_result<element>::type
  49. , typename fusion::detail::ref_result<element>::type
  50. >::type
  51. type;
  52. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  53. static type
  54. call(Iterator const& iter)
  55. {
  56. return std::get<Index>(iter.tuple);
  57. }
  58. };
  59. template <typename Iterator, typename N>
  60. struct advance
  61. {
  62. static int const index = Iterator::index;
  63. typedef typename Iterator::tuple_type tuple_type;
  64. typedef std_tuple_iterator<tuple_type, index+N::value> type;
  65. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  66. static type
  67. call(Iterator const& i)
  68. {
  69. return type(i.tuple);
  70. }
  71. };
  72. template <typename Iterator>
  73. struct next : advance<Iterator, mpl::int_<1>> {};
  74. template <typename Iterator>
  75. struct prior : advance<Iterator, mpl::int_<-1>> {};
  76. template <typename I1, typename I2>
  77. struct equal_to
  78. : is_same<typename I1::identity, typename I2::identity> {};
  79. template <typename First, typename Last>
  80. struct distance
  81. {
  82. typedef mpl::int_<Last::index-First::index> type;
  83. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  84. static type
  85. call(First const&, Last const&)
  86. {
  87. return type();
  88. }
  89. };
  90. };
  91. }}
  92. #ifdef BOOST_FUSION_WORKAROUND_FOR_LWG_2408
  93. namespace std
  94. {
  95. template <typename Tuple, int Index>
  96. struct iterator_traits< ::boost::fusion::std_tuple_iterator<Tuple, Index> >
  97. { };
  98. }
  99. #endif
  100. #endif