array_iterator.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifdef _MSC_VER
  19. # pragma warning(push)
  20. # pragma warning(disable: 4512) // assignment operator could not be generated.
  21. #endif
  22. namespace boost { namespace fusion
  23. {
  24. struct random_access_traversal_tag;
  25. template <typename Array, int Pos>
  26. struct array_iterator
  27. : iterator_facade<array_iterator<Array, Pos>, random_access_traversal_tag>
  28. {
  29. BOOST_MPL_ASSERT_RELATION(Pos, >=, 0);
  30. BOOST_MPL_ASSERT_RELATION(Pos, <=, static_cast<int>(Array::static_size));
  31. typedef mpl::int_<Pos> index;
  32. typedef Array array_type;
  33. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  34. array_iterator(Array& a)
  35. : array(a) {}
  36. Array& array;
  37. template <typename Iterator>
  38. struct value_of
  39. {
  40. typedef typename Iterator::array_type array_type;
  41. typedef typename array_type::value_type type;
  42. };
  43. template <typename Iterator>
  44. struct deref
  45. {
  46. typedef typename Iterator::array_type array_type;
  47. typedef typename
  48. mpl::if_<
  49. is_const<array_type>
  50. , typename array_type::const_reference
  51. , typename array_type::reference
  52. >::type
  53. type;
  54. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  55. static type
  56. call(Iterator const & it)
  57. {
  58. return it.array[Iterator::index::value];
  59. }
  60. };
  61. template <typename Iterator, typename N>
  62. struct advance
  63. {
  64. typedef typename Iterator::index index;
  65. typedef typename Iterator::array_type array_type;
  66. typedef array_iterator<array_type, index::value + N::value> type;
  67. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  68. static type
  69. call(Iterator const& i)
  70. {
  71. return type(i.array);
  72. }
  73. };
  74. template <typename Iterator>
  75. struct next : advance<Iterator, mpl::int_<1> > {};
  76. template <typename Iterator>
  77. struct prior : advance<Iterator, mpl::int_<-1> > {};
  78. template <typename I1, typename I2>
  79. struct distance : mpl::minus<typename I2::index, typename I1::index>
  80. {
  81. typedef typename
  82. mpl::minus<
  83. typename I2::index, typename I1::index
  84. >::type
  85. type;
  86. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  87. static type
  88. call(I1 const&, I2 const&)
  89. {
  90. return type();
  91. }
  92. };
  93. };
  94. }}
  95. #ifdef _MSC_VER
  96. # pragma warning(pop)
  97. #endif
  98. #ifdef BOOST_FUSION_WORKAROUND_FOR_LWG_2408
  99. namespace std
  100. {
  101. template <typename Array, int Pos>
  102. struct iterator_traits< ::boost::fusion::array_iterator<Array, Pos> >
  103. { };
  104. }
  105. #endif
  106. #endif