map_iterator.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/support/config.hpp>
  10. #include <boost/fusion/iterator/iterator_facade.hpp>
  11. #include <boost/mpl/minus.hpp>
  12. #include <boost/mpl/equal_to.hpp>
  13. #include <boost/mpl/if.hpp>
  14. #include <boost/utility/declval.hpp>
  15. #include <boost/type_traits/is_const.hpp>
  16. #include <boost/type_traits/add_const.hpp>
  17. namespace boost { namespace fusion
  18. {
  19. struct random_access_traversal_tag;
  20. template <typename Seq, int Pos>
  21. struct map_iterator
  22. : iterator_facade<
  23. map_iterator<Seq, Pos>
  24. , typename Seq::category>
  25. {
  26. typedef Seq sequence;
  27. typedef mpl::int_<Pos> index;
  28. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  29. map_iterator(Seq& seq)
  30. : seq_(seq)
  31. {}
  32. template<typename Iterator>
  33. struct value_of
  34. {
  35. typedef typename Iterator::sequence sequence;
  36. typedef typename Iterator::index index;
  37. typedef
  38. decltype(boost::declval<sequence>().get_val(index()))
  39. type;
  40. };
  41. template<typename Iterator>
  42. struct value_of_data
  43. {
  44. typedef typename Iterator::sequence sequence;
  45. typedef typename Iterator::index index;
  46. typedef
  47. decltype(boost::declval<sequence>().get_val(index()).second)
  48. type;
  49. };
  50. template<typename Iterator>
  51. struct key_of
  52. {
  53. typedef typename Iterator::sequence sequence;
  54. typedef typename Iterator::index index;
  55. typedef decltype(boost::declval<sequence>().get_key(index())) key_identity_type;
  56. typedef typename key_identity_type::type type;
  57. };
  58. template<typename Iterator>
  59. struct deref
  60. {
  61. typedef typename Iterator::sequence sequence;
  62. typedef typename Iterator::index index;
  63. typedef
  64. decltype(boost::declval<sequence>().get(index()))
  65. type;
  66. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  67. static type
  68. call(Iterator const& it)
  69. {
  70. return it.seq_.get(typename Iterator::index());
  71. }
  72. };
  73. template<typename Iterator>
  74. struct deref_data
  75. {
  76. typedef typename Iterator::sequence sequence;
  77. typedef typename Iterator::index index;
  78. typedef decltype(boost::declval<sequence>().get(index()).second) second_type_;
  79. typedef typename
  80. mpl::if_<
  81. is_const<sequence>
  82. , typename add_const<second_type_>::type
  83. , second_type_
  84. >::type
  85. second_type;
  86. typedef typename add_reference<second_type>::type type;
  87. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  88. static type
  89. call(Iterator const& it)
  90. {
  91. return it.seq_.get(typename Iterator::index()).second;
  92. }
  93. };
  94. template <typename Iterator, typename N>
  95. struct advance
  96. {
  97. typedef typename Iterator::index index;
  98. typedef typename Iterator::sequence sequence;
  99. typedef map_iterator<sequence, index::value + N::value> type;
  100. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  101. static type
  102. call(Iterator const& i)
  103. {
  104. return type(i.seq_);
  105. }
  106. };
  107. template<typename Iterator>
  108. struct next
  109. : advance<Iterator, mpl::int_<1> >
  110. {};
  111. template<typename Iterator>
  112. struct prior
  113. : advance<Iterator, mpl::int_<-1> >
  114. {};
  115. template <typename I1, typename I2>
  116. struct distance
  117. {
  118. typedef typename
  119. mpl::minus<
  120. typename I2::index, typename I1::index
  121. >::type
  122. type;
  123. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  124. static type
  125. call(I1 const&, I2 const&)
  126. {
  127. return type();
  128. }
  129. };
  130. template<typename I1, typename I2>
  131. struct equal_to
  132. : mpl::equal_to<typename I1::index, typename I2::index>
  133. {};
  134. Seq& seq_;
  135. private:
  136. // silence MSVC warning C4512: assignment operator could not be generated
  137. map_iterator& operator= (map_iterator const&);
  138. };
  139. }}
  140. #ifdef BOOST_FUSION_WORKAROUND_FOR_LWG_2408
  141. namespace std
  142. {
  143. template <typename Seq, int Pos>
  144. struct iterator_traits< ::boost::fusion::map_iterator<Seq, Pos> >
  145. { };
  146. }
  147. #endif
  148. #endif