deque_iterator.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*=============================================================================
  2. Copyright (c) 2005-2012 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_DEQUE_ITERATOR_26112006_2154)
  8. #define BOOST_FUSION_DEQUE_ITERATOR_26112006_2154
  9. #include <boost/fusion/support/config.hpp>
  10. #include <boost/fusion/iterator/iterator_facade.hpp>
  11. #include <boost/fusion/container/deque/detail/keyed_element.hpp>
  12. #include <boost/mpl/int.hpp>
  13. #include <boost/mpl/minus.hpp>
  14. #include <boost/mpl/equal_to.hpp>
  15. #include <boost/mpl/identity.hpp>
  16. #include <boost/mpl/if.hpp>
  17. #include <boost/type_traits/is_const.hpp>
  18. #include <boost/type_traits/add_const.hpp>
  19. #include <boost/type_traits/add_reference.hpp>
  20. namespace boost { namespace fusion {
  21. struct bidirectional_traversal_tag;
  22. template <typename Seq, int Pos>
  23. struct deque_iterator
  24. : iterator_facade<deque_iterator<Seq, Pos>, bidirectional_traversal_tag>
  25. {
  26. typedef Seq sequence;
  27. typedef mpl::int_<Pos> index;
  28. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  29. deque_iterator(Seq& seq)
  30. : seq_(seq)
  31. {}
  32. template<typename Iterator>
  33. struct value_of
  34. : detail::keyed_element_value_at<
  35. typename Iterator::sequence, typename Iterator::index>
  36. {};
  37. template<typename Iterator>
  38. struct deref
  39. {
  40. typedef typename detail::keyed_element_value_at<
  41. typename Iterator::sequence, typename Iterator::index>::type element_type;
  42. typedef typename add_reference<
  43. typename mpl::eval_if<
  44. is_const<typename Iterator::sequence>,
  45. add_const<element_type>,
  46. mpl::identity<element_type> >::type>::type type;
  47. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  48. static type
  49. call(Iterator const& it)
  50. {
  51. return it.seq_.get(typename Iterator::index());
  52. }
  53. };
  54. template <typename Iterator, typename N>
  55. struct advance
  56. {
  57. typedef typename Iterator::index index;
  58. typedef typename Iterator::sequence sequence;
  59. typedef deque_iterator<sequence, index::value + N::value> type;
  60. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  61. static type
  62. call(Iterator const& i)
  63. {
  64. return type(i.seq_);
  65. }
  66. };
  67. template<typename Iterator>
  68. struct next
  69. : advance<Iterator, mpl::int_<1> >
  70. {};
  71. template<typename Iterator>
  72. struct prior
  73. : advance<Iterator, mpl::int_<-1> >
  74. {};
  75. template <typename I1, typename I2>
  76. struct distance : mpl::minus<typename I2::index, typename I1::index>
  77. {
  78. typedef typename
  79. mpl::minus<
  80. typename I2::index, typename I1::index
  81. >::type
  82. type;
  83. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  84. static type
  85. call(I1 const&, I2 const&)
  86. {
  87. return type();
  88. }
  89. };
  90. template<typename I1, typename I2>
  91. struct equal_to
  92. : mpl::equal_to<typename I1::index, typename I2::index>
  93. {};
  94. Seq& seq_;
  95. private:
  96. // silence MSVC warning C4512: assignment operator could not be generated
  97. deque_iterator& operator= (deque_iterator const&);
  98. };
  99. }}
  100. #ifdef BOOST_FUSION_WORKAROUND_FOR_LWG_2408
  101. namespace std
  102. {
  103. template <typename Seq, int Pos>
  104. struct iterator_traits< ::boost::fusion::deque_iterator<Seq, Pos> >
  105. { };
  106. }
  107. #endif
  108. #endif