basic_iterator.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*=============================================================================
  2. Copyright (c) 2009 Christopher Schmidt
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #ifndef BOOST_FUSION_ITERATOR_BASIC_ITERATOR_HPP
  7. #define BOOST_FUSION_ITERATOR_BASIC_ITERATOR_HPP
  8. #include <boost/fusion/iterator/iterator_facade.hpp>
  9. #include <boost/mpl/and.hpp>
  10. #include <boost/mpl/equal_to.hpp>
  11. #include <boost/mpl/minus.hpp>
  12. #include <boost/mpl/int.hpp>
  13. #include <boost/type_traits/is_same.hpp>
  14. #include <boost/type_traits/remove_const.hpp>
  15. namespace boost { namespace fusion
  16. {
  17. namespace extension
  18. {
  19. template <typename>
  20. struct value_of_impl;
  21. template <typename>
  22. struct deref_impl;
  23. template <typename>
  24. struct value_of_data_impl;
  25. template <typename>
  26. struct key_of_impl;
  27. template <typename>
  28. struct deref_data_impl;
  29. }
  30. template<typename Tag, typename Category, typename Seq, int Index>
  31. struct basic_iterator
  32. : iterator_facade<basic_iterator<Tag,Category,Seq,Index>, Category>
  33. {
  34. typedef mpl::int_<Index> index;
  35. typedef Seq seq_type;
  36. template <typename It>
  37. struct value_of
  38. : extension::value_of_impl<Tag>::template apply<It>
  39. {};
  40. template <typename It>
  41. struct deref
  42. : extension::deref_impl<Tag>::template apply<It>
  43. {};
  44. template <typename It>
  45. struct value_of_data
  46. : extension::value_of_data_impl<Tag>::template apply<It>
  47. {};
  48. template <typename It>
  49. struct key_of
  50. : extension::key_of_impl<Tag>::template apply<It>
  51. {};
  52. template <typename It>
  53. struct deref_data
  54. : extension::deref_data_impl<Tag>::template apply<It>
  55. {};
  56. template <typename It, typename N>
  57. struct advance
  58. {
  59. typedef
  60. basic_iterator<Tag, Category, Seq, Index + N::value>
  61. type;
  62. static type
  63. call(It const& it)
  64. {
  65. return type(*it.seq,0);
  66. }
  67. };
  68. template <typename It>
  69. struct next
  70. : advance<It, mpl::int_<1> >
  71. {};
  72. template <typename It>
  73. struct prior
  74. : advance<It, mpl::int_<-1> >
  75. {};
  76. template <typename It1, typename It2>
  77. struct distance
  78. {
  79. typedef mpl::minus<typename It2::index, typename It1::index> type;
  80. static
  81. type
  82. call(It1 const&, It2 const&)
  83. {
  84. return type();
  85. }
  86. };
  87. template <typename It1, typename It2>
  88. struct equal_to
  89. : mpl::and_<
  90. is_same<
  91. typename remove_const<typename It1::seq_type>::type
  92. , typename remove_const<typename It2::seq_type>::type
  93. >
  94. , mpl::equal_to<typename It1::index,typename It2::index>
  95. >
  96. {};
  97. template<typename OtherSeq>
  98. basic_iterator(basic_iterator<Tag,Category,OtherSeq,Index> const& it)
  99. : seq(it.seq)
  100. {}
  101. basic_iterator(Seq& in_seq, int)
  102. : seq(&in_seq)
  103. {}
  104. template<typename OtherSeq>
  105. basic_iterator&
  106. operator=(basic_iterator<Tag,Category,OtherSeq,Index> const& it)
  107. {
  108. seq=it.seq;
  109. return *this;
  110. }
  111. Seq* seq;
  112. };
  113. }}
  114. #endif