array_iterator.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2005-2006 Dan Marsden
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #if !defined(BOOST_FUSION_ARRAY_ITERATOR_26122005_2250)
  8. #define BOOST_FUSION_ARRAY_ITERATOR_26122005_2250
  9. #include <boost/fusion/support/config.hpp>
  10. #include <cstddef>
  11. #include <boost/config.hpp>
  12. #include <boost/mpl/int.hpp>
  13. #include <boost/mpl/assert.hpp>
  14. #include <boost/mpl/if.hpp>
  15. #include <boost/mpl/minus.hpp>
  16. #include <boost/type_traits/is_const.hpp>
  17. #include <boost/fusion/iterator/iterator_facade.hpp>
  18. namespace boost { namespace fusion
  19. {
  20. struct random_access_traversal_tag;
  21. template <typename Array, int Pos>
  22. struct array_iterator
  23. : iterator_facade<array_iterator<Array, Pos>, random_access_traversal_tag>
  24. {
  25. BOOST_MPL_ASSERT_RELATION(Pos, >=, 0);
  26. BOOST_MPL_ASSERT_RELATION(Pos, <=, static_cast<int>(Array::static_size));
  27. typedef mpl::int_<Pos> index;
  28. typedef Array array_type;
  29. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  30. array_iterator(Array& a)
  31. : array(a) {}
  32. Array& array;
  33. template <typename Iterator>
  34. struct value_of
  35. {
  36. typedef typename Iterator::array_type array_type;
  37. typedef typename array_type::value_type type;
  38. };
  39. template <typename Iterator>
  40. struct deref
  41. {
  42. typedef typename Iterator::array_type array_type;
  43. typedef typename
  44. mpl::if_<
  45. is_const<array_type>
  46. , typename array_type::const_reference
  47. , typename array_type::reference
  48. >::type
  49. type;
  50. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  51. static type
  52. call(Iterator const & it)
  53. {
  54. return it.array[Iterator::index::value];
  55. }
  56. };
  57. template <typename Iterator, typename N>
  58. struct advance
  59. {
  60. typedef typename Iterator::index index;
  61. typedef typename Iterator::array_type array_type;
  62. typedef array_iterator<array_type, index::value + N::value> type;
  63. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  64. static type
  65. call(Iterator const& i)
  66. {
  67. return type(i.array);
  68. }
  69. };
  70. template <typename Iterator>
  71. struct next : advance<Iterator, mpl::int_<1> > {};
  72. template <typename Iterator>
  73. struct prior : advance<Iterator, mpl::int_<-1> > {};
  74. template <typename I1, typename I2>
  75. struct distance : mpl::minus<typename I2::index, typename I1::index>
  76. {
  77. typedef typename
  78. mpl::minus<
  79. typename I2::index, typename I1::index
  80. >::type
  81. type;
  82. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  83. static type
  84. call(I1 const&, I2 const&)
  85. {
  86. return type();
  87. }
  88. };
  89. private:
  90. array_iterator<Array, Pos>& operator=(array_iterator<Array, Pos> const&);
  91. };
  92. }}
  93. #ifdef BOOST_FUSION_WORKAROUND_FOR_LWG_2408
  94. namespace std
  95. {
  96. template <typename Array, int Pos>
  97. struct iterator_traits< ::boost::fusion::array_iterator<Array, Pos> >
  98. { };
  99. }
  100. #endif
  101. #endif