]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/pfr/core.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / pfr / core.hpp
1 // Copyright (c) 2016-2022 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
6 #ifndef BOOST_PFR_CORE_HPP
7 #define BOOST_PFR_CORE_HPP
8 #pragma once
9
10 #include <boost/pfr/detail/config.hpp>
11
12 #include <boost/pfr/detail/core.hpp>
13
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>
19
20 #include <type_traits>
21 #include <utility> // metaprogramming stuff
22
23 #include <boost/pfr/tuple_size.hpp>
24
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.
27 ///
28 /// \b Synopsis:
29
30 namespace boost { namespace pfr {
31
32 /// \brief Returns reference or const reference to a field with index `I` in \aggregate `val`.
33 ///
34 /// \b Example:
35 /// \code
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;
40 /// \endcode
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) );
44 }
45
46
47 /// \overload get
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
52 #endif
53 ) noexcept {
54 return detail::sequence_tuple::get<I>( detail::tie_as_tuple(val) );
55 }
56
57 #if !BOOST_PFR_USE_CPP17
58 /// \overload get
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");
62 return 0;
63 }
64 #endif
65
66
67 /// \overload get
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) ));
71 }
72
73
74 /// \brief `tuple_element` has a member typedef `type` that returns the type of a field with index I in \aggregate T.
75 ///
76 /// \b Example:
77 /// \code
78 /// std::vector< boost::pfr::tuple_element<0, my_structure>::type > v;
79 /// \endcode
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&>()) ) >;
82
83
84 /// \brief Type of a field with index `I` in \aggregate `T`.
85 ///
86 /// \b Example:
87 /// \code
88 /// std::vector< boost::pfr::tuple_element_t<0, my_structure> > v;
89 /// \endcode
90 template <std::size_t I, class T>
91 using tuple_element_t = typename tuple_element<I, T>::type;
92
93
94 /// \brief Creates a `std::tuple` from fields of an \aggregate `val`.
95 ///
96 /// \b Example:
97 /// \code
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);
102 /// \endcode
103 template <class T>
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> >()
108 );
109 }
110
111
112 /// \brief std::tie` like function that ties fields of a structure.
113 ///
114 /// \returns a `std::tuple` with lvalue and const lvalue references to fields of an \aggregate `val`.
115 ///
116 /// \b Example:
117 /// \code
118 /// void foo(const int&, const short&);
119 /// struct my_struct { int i, short s; };
120 ///
121 /// const my_struct const_s{1, 2};
122 /// std::apply(foo, structure_tie(const_s));
123 ///
124 /// my_struct s;
125 /// structure_tie(s) = std::tuple<int, short>{10, 11};
126 /// assert(s.s == 11);
127 /// \endcode
128 template <class T>
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> >()
133 );
134 }
135
136
137 /// \overload structure_tie
138 template <class T>
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
142 #endif
143 ) noexcept {
144 return detail::make_stdtiedtuple_from_tietuple(
145 detail::tie_as_tuple(val),
146 detail::make_index_sequence< tuple_size_v<T> >()
147 );
148 }
149
150 #if !BOOST_PFR_USE_CPP17
151 /// \overload structure_tie
152 template <class T>
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");
155 return 0;
156 }
157 #endif
158
159
160 /// \overload structure_tie
161 template <class T>
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");
164 return 0;
165 }
166
167 /// Calls `func` for each field of a `value`.
168 ///
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>`
173 ///
174 /// \param value To each field of this variable will be the `func` applied.
175 ///
176 /// \b Example:
177 /// \code
178 /// struct my_struct { int i, short s; };
179 /// int sum = 0;
180 /// for_each_field(my_struct{20, 22}, [&sum](const auto& field) { sum += field; });
181 /// assert(sum == 42);
182 /// \endcode
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>>();
186
187 ::boost::pfr::detail::for_each_field_dispatcher(
188 value,
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>>();
193
194 ::boost::pfr::detail::for_each_field_impl(
195 t,
196 std::forward<F>(f),
197 detail::make_index_sequence<fields_count_val_in_lambda>{},
198 std::is_rvalue_reference<T&&>{}
199 );
200 },
201 detail::make_index_sequence<fields_count_val>{}
202 );
203 }
204
205 /// \brief std::tie-like function that allows assigning to tied values from aggregates.
206 ///
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.
209 ///
210 /// \b Example:
211 /// \code
212 /// auto f() {
213 /// struct { struct { int x, y } p; short s; } res { { 4, 5 }, 6 };
214 /// return res;
215 /// }
216 /// auto [p, s] = f();
217 /// tie_from_structure(p, s) = f();
218 /// \endcode
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...);
222 }
223
224 }} // namespace boost::pfr
225
226 #endif // BOOST_PFR_CORE_HPP