map_impl.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*=============================================================================
  2. Copyright (c) 2005-2013 Joel de Guzman
  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. #if !defined(BOOST_FUSION_MAP_IMPL_02032013_2233)
  7. #define BOOST_FUSION_MAP_IMPL_02032013_2233
  8. #include <boost/fusion/support/detail/access.hpp>
  9. #include <boost/fusion/iterator/deref.hpp>
  10. #include <boost/fusion/iterator/next.hpp>
  11. #include <boost/mpl/int.hpp>
  12. #include <boost/mpl/identity.hpp>
  13. namespace boost { namespace fusion
  14. {
  15. struct fusion_sequence_tag;
  16. }}
  17. namespace boost { namespace fusion { namespace detail
  18. {
  19. struct map_impl_from_iterator {};
  20. template <int index, typename ...T>
  21. struct map_impl;
  22. template <int index_>
  23. struct map_impl<index_>
  24. {
  25. typedef fusion_sequence_tag tag;
  26. static int const index = index_;
  27. static int const size = 0;
  28. map_impl() {}
  29. template <typename Iterator>
  30. map_impl(Iterator const& iter, map_impl_from_iterator)
  31. {}
  32. template <typename Iterator>
  33. void assign(Iterator const& iter, map_impl_from_iterator)
  34. {}
  35. void get();
  36. void get_val();
  37. void get_key();
  38. };
  39. template <int index_, typename Pair, typename ...T>
  40. struct map_impl<index_, Pair, T...> : map_impl<index_ + 1, T...>
  41. {
  42. typedef fusion_sequence_tag tag;
  43. typedef map_impl<index_+1, T...> rest_type;
  44. using rest_type::get;
  45. using rest_type::get_val;
  46. using rest_type::get_key;
  47. static int const index = index_;
  48. static int const size = rest_type::size + 1;
  49. typedef Pair pair_type;
  50. typedef typename Pair::first_type key_type;
  51. typedef typename Pair::second_type value_type;
  52. map_impl()
  53. : rest_type(), element()
  54. {}
  55. map_impl(map_impl const& rhs)
  56. : rest_type(rhs.get_base()), element(rhs.element)
  57. {}
  58. map_impl(map_impl&& rhs)
  59. : rest_type(std::forward<rest_type>(*static_cast<rest_type*>(this)))
  60. , element(std::forward<Pair>(rhs.element))
  61. {}
  62. template <typename ...U>
  63. map_impl(map_impl<index, U...> const& rhs)
  64. : rest_type(rhs.get_base()), element(rhs.element)
  65. {}
  66. map_impl(typename detail::call_param<Pair>::type element
  67. , typename detail::call_param<T>::type... rest)
  68. : rest_type(rest...), element(element)
  69. {}
  70. map_impl(Pair&& element, T&&... rest)
  71. : rest_type(std::forward<T>(rest)...)
  72. , element(std::forward<Pair>(element))
  73. {}
  74. template <typename Iterator>
  75. map_impl(Iterator const& iter, map_impl_from_iterator fi)
  76. : rest_type(fusion::next(iter), fi)
  77. , element(*iter)
  78. {}
  79. rest_type& get_base()
  80. {
  81. return *this;
  82. }
  83. rest_type const& get_base() const
  84. {
  85. return *this;
  86. }
  87. value_type get_val(mpl::identity<key_type>);
  88. pair_type get_val(mpl::int_<index>);
  89. value_type get_val(mpl::identity<key_type>) const;
  90. pair_type get_val(mpl::int_<index>) const;
  91. key_type get_key(mpl::int_<index>);
  92. key_type get_key(mpl::int_<index>) const;
  93. typename cref_result<value_type>::type
  94. get(mpl::identity<key_type>) const
  95. {
  96. return element.second;
  97. }
  98. typename ref_result<value_type>::type
  99. get(mpl::identity<key_type>)
  100. {
  101. return element.second;
  102. }
  103. typename cref_result<pair_type>::type
  104. get(mpl::int_<index>) const
  105. {
  106. return element;
  107. }
  108. typename ref_result<pair_type>::type
  109. get(mpl::int_<index>)
  110. {
  111. return element;
  112. }
  113. template <typename ...U>
  114. map_impl& operator=(map_impl<index, U...> const& rhs)
  115. {
  116. rest_type::operator=(rhs);
  117. element = rhs.element;
  118. return *this;
  119. }
  120. map_impl& operator=(map_impl const& rhs)
  121. {
  122. rest_type::operator=(rhs);
  123. element = rhs.element;
  124. return *this;
  125. }
  126. map_impl& operator=(map_impl&& rhs)
  127. {
  128. rest_type::operator=(std::forward<map_impl>(rhs));
  129. element = std::forward<Pair>(rhs.element);
  130. return *this;
  131. }
  132. template <typename Iterator>
  133. void assign(Iterator const& iter, map_impl_from_iterator fi)
  134. {
  135. rest_type::assign(fusion::next(iter), fi);
  136. element = *iter;
  137. }
  138. Pair element;
  139. };
  140. }}}
  141. #endif