core.hpp 9.3 KB

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