std_array_iterator.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #ifdef _MSC_VER
  20. # pragma warning(push)
  21. # pragma warning(disable: 4512) // assignment operator could not be generated.
  22. #endif
  23. namespace boost { namespace fusion
  24. {
  25. struct random_access_traversal_tag;
  26. template <typename Array, int Pos>
  27. struct std_array_iterator
  28. : iterator_facade<std_array_iterator<Array, Pos>, random_access_traversal_tag>
  29. {
  30. BOOST_MPL_ASSERT_RELATION(Pos, >=, 0);
  31. BOOST_MPL_ASSERT_RELATION(Pos, <=, std::tuple_size<Array>::value);
  32. typedef mpl::int_<Pos> index;
  33. typedef Array array_type;
  34. std_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. static type
  55. call(Iterator const & it)
  56. {
  57. return it.array[Iterator::index::value];
  58. }
  59. };
  60. template <typename Iterator, typename N>
  61. struct advance
  62. {
  63. typedef typename Iterator::index index;
  64. typedef typename Iterator::array_type array_type;
  65. typedef std_array_iterator<array_type, index::value + N::value> type;
  66. static type
  67. call(Iterator const& i)
  68. {
  69. return type(i.array);
  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 distance : mpl::minus<typename I2::index, typename I1::index>
  78. {
  79. typedef typename
  80. mpl::minus<
  81. typename I2::index, typename I1::index
  82. >::type
  83. type;
  84. static type
  85. call(I1 const&, I2 const&)
  86. {
  87. return type();
  88. }
  89. };
  90. };
  91. }}
  92. #ifdef _MSC_VER
  93. # pragma warning(pop)
  94. #endif
  95. #endif