1 // Copyright (c) 2016-2022 Antony Polukhin
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)
6 #ifndef BOOST_PFR_CORE_HPP
7 #define BOOST_PFR_CORE_HPP
10 #include <boost/pfr/detail/config.hpp>
12 #include <boost/pfr/detail/core.hpp>
14 #include <boost/pfr/detail/sequence_tuple.hpp>
15 #include <boost/pfr/detail/stdtuple.hpp>
16 #include <boost/pfr/detail/for_each_field_impl.hpp>
17 #include <boost/pfr/detail/make_integer_sequence.hpp>
18 #include <boost/pfr/detail/tie_from_structure_tuple.hpp>
20 #include <type_traits>
21 #include <utility> // metaprogramming stuff
23 #include <boost/pfr/tuple_size.hpp>
25 /// \file boost/pfr/core.hpp
26 /// Contains all the basic tuple-like interfaces \forcedlink{get}, \forcedlink{tuple_size}, \forcedlink{tuple_element_t}, and others.
30 namespace boost { namespace pfr {
32 /// \brief Returns reference or const reference to a field with index `I` in \aggregate `val`.
36 /// struct my_struct { int i, short s; };
37 /// my_struct s {10, 11};
38 /// assert(boost::pfr::get<0>(s) == 10);
39 /// boost::pfr::get<1>(s) = 0;
41 template <std::size_t I, class T>
42 constexpr decltype(auto) get(const T& val) noexcept {
43 return detail::sequence_tuple::get<I>( detail::tie_as_tuple(val) );
48 template <std::size_t I, class T>
49 constexpr decltype(auto) get(T& val
50 #if !BOOST_PFR_USE_CPP17
51 , std::enable_if_t<std::is_assignable<T, T>::value>* = nullptr
54 return detail::sequence_tuple::get<I>( detail::tie_as_tuple(val) );
57 #if !BOOST_PFR_USE_CPP17
59 template <std::size_t I, class T>
60 constexpr auto get(T&, std::enable_if_t<!std::is_assignable<T, T>::value>* = nullptr) noexcept {
61 static_assert(sizeof(T) && false, "====================> Boost.PFR: Calling boost::pfr::get on non const non assignable type is allowed only in C++17");
68 template <std::size_t I, class T>
69 constexpr auto get(T&& val, std::enable_if_t< std::is_rvalue_reference<T&&>::value>* = 0) noexcept {
70 return std::move(detail::sequence_tuple::get<I>( detail::tie_as_tuple(val) ));
74 /// \brief `tuple_element` has a member typedef `type` that returns the type of a field with index I in \aggregate T.
78 /// std::vector< boost::pfr::tuple_element<0, my_structure>::type > v;
80 template <std::size_t I, class T>
81 using tuple_element = detail::sequence_tuple::tuple_element<I, decltype( ::boost::pfr::detail::tie_as_tuple(std::declval<T&>()) ) >;
84 /// \brief Type of a field with index `I` in \aggregate `T`.
88 /// std::vector< boost::pfr::tuple_element_t<0, my_structure> > v;
90 template <std::size_t I, class T>
91 using tuple_element_t = typename tuple_element<I, T>::type;
94 /// \brief Creates a `std::tuple` from fields of an \aggregate `val`.
98 /// struct my_struct { int i, short s; };
99 /// my_struct s {10, 11};
100 /// std::tuple<int, short> t = make_tuple(s);
101 /// assert(get<0>(t) == 10);
104 constexpr auto structure_to_tuple(const T& val) noexcept {
105 return detail::make_stdtuple_from_tietuple(
106 detail::tie_as_tuple(val),
107 detail::make_index_sequence< tuple_size_v<T> >()
112 /// \brief std::tie` like function that ties fields of a structure.
114 /// \returns a `std::tuple` with lvalue and const lvalue references to fields of an \aggregate `val`.
118 /// void foo(const int&, const short&);
119 /// struct my_struct { int i, short s; };
121 /// const my_struct const_s{1, 2};
122 /// std::apply(foo, structure_tie(const_s));
125 /// structure_tie(s) = std::tuple<int, short>{10, 11};
126 /// assert(s.s == 11);
129 constexpr auto structure_tie(const T& val) noexcept {
130 return detail::make_conststdtiedtuple_from_tietuple(
131 detail::tie_as_tuple(const_cast<T&>(val)),
132 detail::make_index_sequence< tuple_size_v<T> >()
137 /// \overload structure_tie
139 constexpr auto structure_tie(T& val
140 #if !BOOST_PFR_USE_CPP17
141 , std::enable_if_t<std::is_assignable<T, T>::value>* = nullptr
144 return detail::make_stdtiedtuple_from_tietuple(
145 detail::tie_as_tuple(val),
146 detail::make_index_sequence< tuple_size_v<T> >()
150 #if !BOOST_PFR_USE_CPP17
151 /// \overload structure_tie
153 constexpr auto structure_tie(T&, std::enable_if_t<!std::is_assignable<T, T>::value>* = nullptr) noexcept {
154 static_assert(sizeof(T) && false, "====================> Boost.PFR: Calling boost::pfr::structure_tie on non const non assignable type is allowed only in C++17");
160 /// \overload structure_tie
162 constexpr auto structure_tie(T&&, std::enable_if_t< std::is_rvalue_reference<T&&>::value>* = 0) noexcept {
163 static_assert(sizeof(T) && false, "====================> Boost.PFR: Calling boost::pfr::structure_tie on rvalue references is forbidden");
167 /// Calls `func` for each field of a `value`.
169 /// \param func must have one of the following signatures:
170 /// * any_return_type func(U&& field) // field of value is perfect forwarded to function
171 /// * any_return_type func(U&& field, std::size_t i)
172 /// * any_return_type func(U&& value, I i) // Here I is an `std::integral_constant<size_t, field_index>`
174 /// \param value To each field of this variable will be the `func` applied.
178 /// struct my_struct { int i, short s; };
180 /// for_each_field(my_struct{20, 22}, [&sum](const auto& field) { sum += field; });
181 /// assert(sum == 42);
183 template <class T, class F>
184 void for_each_field(T&& value, F&& func) {
185 constexpr std::size_t fields_count_val = boost::pfr::detail::fields_count<std::remove_reference_t<T>>();
187 ::boost::pfr::detail::for_each_field_dispatcher(
189 [f = std::forward<F>(func)](auto&& t) mutable {
190 // MSVC related workaround. Its lambdas do not capture constexprs.
191 constexpr std::size_t fields_count_val_in_lambda
192 = boost::pfr::detail::fields_count<std::remove_reference_t<T>>();
194 ::boost::pfr::detail::for_each_field_impl(
197 detail::make_index_sequence<fields_count_val_in_lambda>{},
198 std::is_rvalue_reference<T&&>{}
201 detail::make_index_sequence<fields_count_val>{}
205 /// \brief std::tie-like function that allows assigning to tied values from aggregates.
207 /// \returns an object with lvalue references to `args...`; on assignment of an \aggregate value to that
208 /// object each field of an aggregate is assigned to the corresponding `args...` reference.
213 /// struct { struct { int x, y } p; short s; } res { { 4, 5 }, 6 };
216 /// auto [p, s] = f();
217 /// tie_from_structure(p, s) = f();
219 template <typename... Elements>
220 constexpr detail::tie_from_structure_tuple<Elements...> tie_from_structure(Elements&... args) noexcept {
221 return detail::tie_from_structure_tuple<Elements...>(args...);
224 }} // namespace boost::pfr
226 #endif // BOOST_PFR_CORE_HPP