core.hpp 8.8 KB

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