core.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright (c) 2016-2021 Antony Polukhin
  2. //
  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. #ifndef BOOST_PFR_CORE_HPP
  6. #define BOOST_PFR_CORE_HPP
  7. #pragma once
  8. #include <boost/pfr/detail/config.hpp>
  9. #include <boost/pfr/detail/core.hpp>
  10. #include <boost/pfr/detail/sequence_tuple.hpp>
  11. #include <boost/pfr/detail/stdtuple.hpp>
  12. #include <boost/pfr/detail/for_each_field_impl.hpp>
  13. #include <boost/pfr/detail/make_integer_sequence.hpp>
  14. #include <boost/pfr/detail/tie_from_structure_tuple.hpp>
  15. #include <type_traits>
  16. #include <utility> // metaprogramming stuff
  17. #include <boost/pfr/tuple_size.hpp>
  18. /// \file boost/pfr/core.hpp
  19. /// Contains all the basic tuple-like interfaces \forcedlink{get}, \forcedlink{tuple_size}, \forcedlink{tuple_element_t}, and others.
  20. ///
  21. /// \b Synopsis:
  22. namespace boost { namespace pfr {
  23. /// \brief Returns reference or const reference to a field with index `I` in \aggregate `val`.
  24. ///
  25. /// \b Example:
  26. /// \code
  27. /// struct my_struct { int i, short s; };
  28. /// my_struct s {10, 11};
  29. /// assert(boost::pfr::get<0>(s) == 10);
  30. /// boost::pfr::get<1>(s) = 0;
  31. /// \endcode
  32. template <std::size_t I, class T>
  33. constexpr decltype(auto) get(const T& val) noexcept {
  34. return detail::sequence_tuple::get<I>( detail::tie_as_tuple(val) );
  35. }
  36. /// \overload get
  37. template <std::size_t I, class T>
  38. constexpr decltype(auto) get(T& val
  39. #if !BOOST_PFR_USE_CPP17
  40. , std::enable_if_t<std::is_assignable<T, T>::value>* = nullptr
  41. #endif
  42. ) noexcept {
  43. return detail::sequence_tuple::get<I>( detail::tie_as_tuple(val) );
  44. }
  45. #if !BOOST_PFR_USE_CPP17
  46. /// \overload get
  47. template <std::size_t I, class T>
  48. constexpr auto get(T&, std::enable_if_t<!std::is_assignable<T, T>::value>* = nullptr) noexcept {
  49. static_assert(sizeof(T) && false, "====================> Boost.PFR: Calling boost::pfr::get on non const non assignable type is allowed only in C++17");
  50. return 0;
  51. }
  52. #endif
  53. /// \overload get
  54. template <std::size_t I, class T>
  55. constexpr auto get(T&& val, std::enable_if_t< std::is_rvalue_reference<T&&>::value>* = 0) noexcept {
  56. return std::move(detail::sequence_tuple::get<I>( detail::tie_as_tuple(val) ));
  57. }
  58. /// \brief `tuple_element` has a member typedef `type` that returns the type of a field with index I in \aggregate T.
  59. ///
  60. /// \b Example:
  61. /// \code
  62. /// std::vector< boost::pfr::tuple_element<0, my_structure>::type > v;
  63. /// \endcode
  64. template <std::size_t I, class T>
  65. using tuple_element = detail::sequence_tuple::tuple_element<I, decltype( ::boost::pfr::detail::tie_as_tuple(std::declval<T&>()) ) >;
  66. /// \brief Type of a field with index `I` in \aggregate `T`.
  67. ///
  68. /// \b Example:
  69. /// \code
  70. /// std::vector< boost::pfr::tuple_element_t<0, my_structure> > v;
  71. /// \endcode
  72. template <std::size_t I, class T>
  73. using tuple_element_t = typename tuple_element<I, T>::type;
  74. /// \brief Creates a `std::tuple` from fields of an \aggregate `val`.
  75. ///
  76. /// \b Example:
  77. /// \code
  78. /// struct my_struct { int i, short s; };
  79. /// my_struct s {10, 11};
  80. /// std::tuple<int, short> t = make_tuple(s);
  81. /// assert(get<0>(t) == 10);
  82. /// \endcode
  83. template <class T>
  84. constexpr auto structure_to_tuple(const T& val) noexcept {
  85. return detail::make_stdtuple_from_tietuple(
  86. detail::tie_as_tuple(val),
  87. detail::make_index_sequence< tuple_size_v<T> >()
  88. );
  89. }
  90. /// \brief std::tie` like function that ties fields of a structure.
  91. ///
  92. /// \returns a `std::tuple` with lvalue and const lvalue references to fields of an \aggregate `val`.
  93. ///
  94. /// \b Example:
  95. /// \code
  96. /// void foo(const int&, const short&);
  97. /// struct my_struct { int i, short s; };
  98. ///
  99. /// const my_struct const_s{1, 2};
  100. /// std::apply(foo, structure_tie(const_s));
  101. ///
  102. /// my_struct s;
  103. /// structure_tie(s) = std::tuple<int, short>{10, 11};
  104. /// assert(s.s == 11);
  105. /// \endcode
  106. template <class T>
  107. constexpr auto structure_tie(const T& val) noexcept {
  108. return detail::make_conststdtiedtuple_from_tietuple(
  109. detail::tie_as_tuple(const_cast<T&>(val)),
  110. detail::make_index_sequence< tuple_size_v<T> >()
  111. );
  112. }
  113. /// \overload structure_tie
  114. template <class T>
  115. constexpr auto structure_tie(T& val
  116. #if !BOOST_PFR_USE_CPP17
  117. , std::enable_if_t<std::is_assignable<T, T>::value>* = nullptr
  118. #endif
  119. ) noexcept {
  120. return detail::make_stdtiedtuple_from_tietuple(
  121. detail::tie_as_tuple(val),
  122. detail::make_index_sequence< tuple_size_v<T> >()
  123. );
  124. }
  125. #if !BOOST_PFR_USE_CPP17
  126. /// \overload structure_tie
  127. template <class T>
  128. constexpr auto structure_tie(T&, std::enable_if_t<!std::is_assignable<T, T>::value>* = nullptr) noexcept {
  129. static_assert(sizeof(T) && false, "====================> Boost.PFR: Calling boost::pfr::structure_tie on non const non assignable type is allowed only in C++17");
  130. return 0;
  131. }
  132. #endif
  133. /// \overload structure_tie
  134. template <class T>
  135. constexpr auto structure_tie(T&&, std::enable_if_t< std::is_rvalue_reference<T&&>::value>* = 0) noexcept {
  136. static_assert(sizeof(T) && false, "====================> Boost.PFR: Calling boost::pfr::structure_tie on rvalue references is forbidden");
  137. return 0;
  138. }
  139. /// Calls `func` for each field of a `value`.
  140. ///
  141. /// \param func must have one of the following signatures:
  142. /// * any_return_type func(U&& field) // field of value is perfect forwarded to function
  143. /// * any_return_type func(U&& field, std::size_t i)
  144. /// * any_return_type func(U&& value, I i) // Here I is an `std::integral_constant<size_t, field_index>`
  145. ///
  146. /// \param value To each field of this variable will be the `func` applied.
  147. ///
  148. /// \b Example:
  149. /// \code
  150. /// struct my_struct { int i, short s; };
  151. /// int sum = 0;
  152. /// for_each_field(my_struct{20, 22}, [&sum](const auto& field) { sum += field; });
  153. /// assert(sum == 42);
  154. /// \endcode
  155. template <class T, class F>
  156. void for_each_field(T&& value, F&& func) {
  157. constexpr std::size_t fields_count_val = boost::pfr::detail::fields_count<std::remove_reference_t<T>>();
  158. ::boost::pfr::detail::for_each_field_dispatcher(
  159. value,
  160. [f = std::forward<F>(func)](auto&& t) mutable {
  161. // MSVC related workaround. Its lambdas do not capture constexprs.
  162. constexpr std::size_t fields_count_val_in_lambda
  163. = boost::pfr::detail::fields_count<std::remove_reference_t<T>>();
  164. ::boost::pfr::detail::for_each_field_impl(
  165. t,
  166. std::forward<F>(f),
  167. detail::make_index_sequence<fields_count_val_in_lambda>{},
  168. std::is_rvalue_reference<T&&>{}
  169. );
  170. },
  171. detail::make_index_sequence<fields_count_val>{}
  172. );
  173. }
  174. /// \brief std::tie-like function that allows assigning to tied values from aggregates.
  175. ///
  176. /// \returns an object with lvalue references to `args...`; on assignment of an \aggregate value to that
  177. /// object each field of an aggregate is assigned to the corresponding `args...` reference.
  178. ///
  179. /// \b Example:
  180. /// \code
  181. /// auto f() {
  182. /// struct { struct { int x, y } p; short s; } res { { 4, 5 }, 6 };
  183. /// return res;
  184. /// }
  185. /// auto [p, s] = f();
  186. /// tie_from_structure(p, s) = f();
  187. /// \endcode
  188. template <typename... Elements>
  189. constexpr detail::tie_from_structure_tuple<Elements...> tie_from_structure(Elements&... args) noexcept {
  190. return detail::tie_from_structure_tuple<Elements...>(args...);
  191. }
  192. }} // namespace boost::pfr
  193. #endif // BOOST_PFR_CORE_HPP