map_iterator.hpp 5.0 KB

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