tuple.hpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // Copyright (c) 2016-2019Damian Jarek (damian dot jarek93 at gmail dot com)
  3. //
  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. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #ifndef BOOST_BEAST_DETAIL_TUPLE_HPP
  10. #define BOOST_BEAST_DETAIL_TUPLE_HPP
  11. #include <boost/mp11/integer_sequence.hpp>
  12. #include <boost/mp11/algorithm.hpp>
  13. #include <boost/type_traits/remove_cv.hpp>
  14. #include <boost/type_traits/copy_cv.hpp>
  15. #include <cstdlib>
  16. #include <utility>
  17. namespace boost {
  18. namespace beast {
  19. namespace detail {
  20. template<std::size_t I, class T>
  21. struct tuple_element_impl
  22. {
  23. T t;
  24. tuple_element_impl(T const& t_)
  25. : t(t_)
  26. {
  27. }
  28. tuple_element_impl(T&& t_)
  29. : t(std::move(t_))
  30. {
  31. }
  32. };
  33. template<std::size_t I, class T>
  34. struct tuple_element_impl<I, T&>
  35. {
  36. T& t;
  37. tuple_element_impl(T& t_)
  38. : t(t_)
  39. {
  40. }
  41. };
  42. template<class... Ts>
  43. struct tuple_impl;
  44. template<class... Ts, std::size_t... Is>
  45. struct tuple_impl<
  46. boost::mp11::index_sequence<Is...>, Ts...>
  47. : tuple_element_impl<Is, Ts>...
  48. {
  49. template<class... Us>
  50. explicit tuple_impl(Us&&... us)
  51. : tuple_element_impl<Is, Ts>(
  52. std::forward<Us>(us))...
  53. {
  54. }
  55. };
  56. template<class... Ts>
  57. struct tuple : tuple_impl<
  58. boost::mp11::index_sequence_for<Ts...>, Ts...>
  59. {
  60. template<class... Us>
  61. explicit tuple(Us&&... us)
  62. : tuple_impl<
  63. boost::mp11::index_sequence_for<Ts...>, Ts...>{
  64. std::forward<Us>(us)...}
  65. {
  66. }
  67. };
  68. template<std::size_t I, class T>
  69. T&
  70. get(tuple_element_impl<I, T>& te)
  71. {
  72. return te.t;
  73. }
  74. template<std::size_t I, class T>
  75. T const&
  76. get(tuple_element_impl<I, T> const& te)
  77. {
  78. return te.t;
  79. }
  80. template<std::size_t I, class T>
  81. T&&
  82. get(tuple_element_impl<I, T>&& te)
  83. {
  84. return std::move(te.t);
  85. }
  86. template<std::size_t I, class T>
  87. T&
  88. get(tuple_element_impl<I, T&>&& te)
  89. {
  90. return te.t;
  91. }
  92. template <std::size_t I, class T>
  93. using tuple_element = typename boost::copy_cv<
  94. mp11::mp_at_c<typename remove_cv<T>::type, I>, T>::type;
  95. } // detail
  96. } // beast
  97. } // boost
  98. #endif