std_array_iterator.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*=============================================================================
  2. Copyright (c) 2013 Mateusz Loskot
  3. Copyright (c) 2001-2011 Joel de Guzman
  4. Copyright (c) 2005-2006 Dan Marsden
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #if !defined(BOOST_FUSION_STD_ARRAY_ARRAY_ITERATOR_01062013_1700)
  9. #define BOOST_FUSION_STD_ARRAY_ARRAY_ITERATOR_01062013_1700
  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. #include <boost/fusion/adapted/std_array/detail/array_size.hpp>
  19. namespace boost { namespace fusion
  20. {
  21. struct random_access_traversal_tag;
  22. template <typename Array, int Pos>
  23. struct std_array_iterator
  24. : iterator_facade<std_array_iterator<Array, Pos>, random_access_traversal_tag>
  25. {
  26. BOOST_MPL_ASSERT_RELATION(Pos, >=, 0);
  27. BOOST_MPL_ASSERT_RELATION(Pos, <=, std::tuple_size<Array>::value);
  28. typedef mpl::int_<Pos> index;
  29. typedef Array array_type;
  30. std_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. static type
  51. call(Iterator const & it)
  52. {
  53. return it.array[Iterator::index::value];
  54. }
  55. };
  56. template <typename Iterator, typename N>
  57. struct advance
  58. {
  59. typedef typename Iterator::index index;
  60. typedef typename Iterator::array_type array_type;
  61. typedef std_array_iterator<array_type, index::value + N::value> type;
  62. static type
  63. call(Iterator const& i)
  64. {
  65. return type(i.array);
  66. }
  67. };
  68. template <typename Iterator>
  69. struct next : advance<Iterator, mpl::int_<1> > {};
  70. template <typename Iterator>
  71. struct prior : advance<Iterator, mpl::int_<-1> > {};
  72. template <typename I1, typename I2>
  73. struct distance : mpl::minus<typename I2::index, typename I1::index>
  74. {
  75. typedef typename
  76. mpl::minus<
  77. typename I2::index, typename I1::index
  78. >::type
  79. type;
  80. static type
  81. call(I1 const&, I2 const&)
  82. {
  83. return type();
  84. }
  85. };
  86. private:
  87. std_array_iterator<Array, Pos>& operator=(std_array_iterator<Array, Pos> const&);
  88. };
  89. }}
  90. #endif