deque_iterator.hpp 3.2 KB

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