std_tuple_iterator.hpp 3.8 KB

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