map_iterator.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*=============================================================================
  2. Copyright (c) 2005-2013 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_MAP_ITERATOR_02042013_0835)
  8. #define BOOST_FUSION_MAP_ITERATOR_02042013_0835
  9. #include <boost/fusion/iterator/iterator_facade.hpp>
  10. #include <boost/mpl/minus.hpp>
  11. #include <boost/mpl/equal_to.hpp>
  12. namespace boost { namespace fusion
  13. {
  14. struct random_access_traversal_tag;
  15. template <typename Seq, int Pos>
  16. struct map_iterator
  17. : iterator_facade<
  18. map_iterator<Seq, Pos>
  19. , typename Seq::category>
  20. {
  21. typedef Seq sequence;
  22. typedef mpl::int_<Pos> index;
  23. map_iterator(Seq& seq)
  24. : seq_(seq)
  25. {}
  26. template<typename Iterator>
  27. struct value_of
  28. {
  29. typedef typename Iterator::sequence sequence;
  30. typedef typename Iterator::index index;
  31. typedef
  32. decltype(std::declval<sequence>().get_val(index()))
  33. type;
  34. };
  35. template<typename Iterator>
  36. struct value_of_data
  37. {
  38. typedef typename Iterator::sequence sequence;
  39. typedef typename Iterator::index index;
  40. typedef
  41. decltype(std::declval<sequence>().get_val(index()).second)
  42. type;
  43. };
  44. template<typename Iterator>
  45. struct key_of
  46. {
  47. typedef typename Iterator::sequence sequence;
  48. typedef typename Iterator::index index;
  49. typedef
  50. decltype(std::declval<sequence>().get_key(index()))
  51. type;
  52. };
  53. template<typename Iterator>
  54. struct deref
  55. {
  56. typedef typename Iterator::sequence sequence;
  57. typedef typename Iterator::index index;
  58. typedef
  59. decltype(std::declval<sequence>().get(index()))
  60. type;
  61. static type
  62. call(Iterator const& it)
  63. {
  64. return it.seq_.get(typename Iterator::index());
  65. }
  66. };
  67. template<typename Iterator>
  68. struct deref_data
  69. {
  70. typedef typename Iterator::sequence sequence;
  71. typedef typename Iterator::index index;
  72. typedef
  73. decltype(std::declval<sequence>().get(index()).second)
  74. type;
  75. static type
  76. call(Iterator const& it)
  77. {
  78. return it.seq_.get(typename Iterator::index()).second;
  79. }
  80. };
  81. template <typename Iterator, typename N>
  82. struct advance
  83. {
  84. typedef typename Iterator::index index;
  85. typedef typename Iterator::sequence sequence;
  86. typedef map_iterator<sequence, index::value + N::value> type;
  87. static type
  88. call(Iterator const& i)
  89. {
  90. return type(i.seq_);
  91. }
  92. };
  93. template<typename Iterator>
  94. struct next
  95. : advance<Iterator, mpl::int_<1> >
  96. {};
  97. template<typename Iterator>
  98. struct prior
  99. : advance<Iterator, mpl::int_<-1> >
  100. {};
  101. template <typename I1, typename I2>
  102. struct distance
  103. {
  104. typedef typename
  105. mpl::minus<
  106. typename I2::index, typename I1::index
  107. >::type
  108. type;
  109. static type
  110. call(I1 const&, I2 const&)
  111. {
  112. return type();
  113. }
  114. };
  115. template<typename I1, typename I2>
  116. struct equal_to
  117. : mpl::equal_to<typename I1::index, typename I2::index>
  118. {};
  119. Seq& seq_;
  120. private:
  121. // silence MSVC warning C4512: assignment operator could not be generated
  122. map_iterator& operator= (map_iterator const&);
  123. };
  124. }}
  125. #endif