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
6#ifndef BOOST_PFR_DETAIL_FIELDS_COUNT_HPP
7#define BOOST_PFR_DETAIL_FIELDS_COUNT_HPP
8#pragma once
9
10#include <boost/pfr/detail/config.hpp>
11#include <boost/pfr/detail/make_integer_sequence.hpp>
12#include <boost/pfr/detail/size_t_.hpp>
13#include <boost/pfr/detail/unsafe_declval.hpp>
14
15#include <climits> // CHAR_BIT
16#include <type_traits>
17#include <utility> // metaprogramming stuff
18
19#ifdef __clang__
20# pragma clang diagnostic push
21# pragma clang diagnostic ignored "-Wmissing-braces"
22# pragma clang diagnostic ignored "-Wundefined-inline"
23# pragma clang diagnostic ignored "-Wundefined-internal"
24# pragma clang diagnostic ignored "-Wmissing-field-initializers"
25#endif
26
27namespace boost { namespace pfr { namespace detail {
28
29///////////////////// Structure that can be converted to reference to anything
30struct ubiq_lref_constructor {
31 std::size_t ignore;
32 template <class Type> constexpr operator Type&() const && noexcept { // tweak for template_unconstrained.cpp like cases
33 return detail::unsafe_declval<Type&>();
34 }
35
36 template <class Type> constexpr operator Type&() const & noexcept { // tweak for optional_chrono.cpp like cases
37 return detail::unsafe_declval<Type&>();
38 }
39};
40
41///////////////////// Structure that can be converted to rvalue reference to anything
42struct ubiq_rref_constructor {
43 std::size_t ignore;
44 template <class Type> /*constexpr*/ operator Type() const && noexcept { // Allows initialization of rvalue reference fields and move-only types
45 return detail::unsafe_declval<Type>();
46 }
47};
48
49
50#ifndef __cpp_lib_is_aggregate
51///////////////////// Hand-made is_aggregate_initializable_n<T> trait
52
53// Structure that can be converted to reference to anything except reference to T
54template <class T, bool IsCopyConstructible>
55struct ubiq_constructor_except {
56 std::size_t ignore;
57 template <class Type> constexpr operator std::enable_if_t<!std::is_same<T, Type>::value, Type&> () const noexcept; // Undefined
58};
59
60template <class T>
61struct ubiq_constructor_except<T, false> {
62 std::size_t ignore;
63 template <class Type> constexpr operator std::enable_if_t<!std::is_same<T, Type>::value, Type&&> () const noexcept; // Undefined
64};
65
66
67// `std::is_constructible<T, ubiq_constructor_except<T>>` consumes a lot of time, so we made a separate lazy trait for it.
68template <std::size_t N, class T> struct is_single_field_and_aggregate_initializable: std::false_type {};
69template <class T> struct is_single_field_and_aggregate_initializable<1, T>: std::integral_constant<
70 bool, !std::is_constructible<T, ubiq_constructor_except<T, std::is_copy_constructible<T>::value>>::value
71> {};
72
73// Hand-made is_aggregate<T> trait:
74// Before C++20 aggregates could be constructed from `decltype(ubiq_?ref_constructor{I})...` but type traits report that
75// there's no constructor from `decltype(ubiq_?ref_constructor{I})...`
76// Special case for N == 1: `std::is_constructible<T, ubiq_?ref_constructor>` returns true if N == 1 and T is copy/move constructible.
77template <class T, std::size_t N>
78struct is_aggregate_initializable_n {
79 template <std::size_t ...I>
80 static constexpr bool is_not_constructible_n(std::index_sequence<I...>) noexcept {
81 return (!std::is_constructible<T, decltype(ubiq_lref_constructor{I})...>::value && !std::is_constructible<T, decltype(ubiq_rref_constructor{I})...>::value)
82 || is_single_field_and_aggregate_initializable<N, T>::value
83 ;
84 }
85
86 static constexpr bool value =
87 std::is_empty<T>::value
88 || std::is_array<T>::value
89 || std::is_fundamental<T>::value
90 || is_not_constructible_n(detail::make_index_sequence<N>{})
91 ;
92};
93
94#endif // #ifndef __cpp_lib_is_aggregate
95
96///////////////////// Detect aggregates with inheritance
97template <class Derived, class U>
98constexpr bool static_assert_non_inherited() noexcept {
99 static_assert(
100 !std::is_base_of<U, Derived>::value,
101 "====================> Boost.PFR: Boost.PFR: Inherited types are not supported."
102 );
103 return true;
104}
105
106template <class Derived>
107struct ubiq_lref_base_asserting {
108 template <class Type> constexpr operator Type&() const && // tweak for template_unconstrained.cpp like cases
109 noexcept(detail::static_assert_non_inherited<Derived, Type>()) // force the computation of assert function
110 {
111 return detail::unsafe_declval<Type&>();
112 }
113
114 template <class Type> constexpr operator Type&() const & // tweak for optional_chrono.cpp like cases
115 noexcept(detail::static_assert_non_inherited<Derived, Type>()) // force the computation of assert function
116 {
117 return detail::unsafe_declval<Type&>();
118 }
119};
120
121template <class Derived>
122struct ubiq_rref_base_asserting {
123 template <class Type> /*constexpr*/ operator Type() const && // Allows initialization of rvalue reference fields and move-only types
124 noexcept(detail::static_assert_non_inherited<Derived, Type>()) // force the computation of assert function
125 {
126 return detail::unsafe_declval<Type>();
127 }
128};
129
130template <class T, std::size_t I0, std::size_t... I, class /*Enable*/ = typename std::enable_if<std::is_copy_constructible<T>::value>::type>
131constexpr auto assert_first_not_base(std::index_sequence<I0, I...>) noexcept
132 -> typename std::add_pointer<decltype(T{ ubiq_lref_base_asserting<T>{}, ubiq_lref_constructor{.ignore: I}... })>::type
133{
134 return nullptr;
135}
136
137template <class T, std::size_t I0, std::size_t... I, class /*Enable*/ = typename std::enable_if<!std::is_copy_constructible<T>::value>::type>
138constexpr auto assert_first_not_base(std::index_sequence<I0, I...>) noexcept
139 -> typename std::add_pointer<decltype(T{ ubiq_rref_base_asserting<T>{}, ubiq_rref_constructor{.ignore: I}... })>::type
140{
141 return nullptr;
142}
143
144template <class T>
145constexpr void* assert_first_not_base(std::index_sequence<>) noexcept
146{
147 return nullptr;
148}
149
150///////////////////// Helper for SFINAE on fields count
151template <class T, std::size_t... I, class /*Enable*/ = typename std::enable_if<std::is_copy_constructible<T>::value>::type>
152constexpr auto enable_if_constructible_helper(std::index_sequence<I...>) noexcept
153 -> typename std::add_pointer<decltype(T{ ubiq_lref_constructor{.ignore: I}... })>::type;
154
155template <class T, std::size_t... I, class /*Enable*/ = typename std::enable_if<!std::is_copy_constructible<T>::value>::type>
156constexpr auto enable_if_constructible_helper(std::index_sequence<I...>) noexcept
157 -> typename std::add_pointer<decltype(T{ ubiq_rref_constructor{.ignore: I}... })>::type;
158
159template <class T, std::size_t N, class /*Enable*/ = decltype( enable_if_constructible_helper<T>(detail::make_index_sequence<N>()) ) >
160using enable_if_constructible_helper_t = std::size_t;
161
162///////////////////// Helpers for range size detection
163template <std::size_t Begin, std::size_t Last>
164using is_one_element_range = std::integral_constant<bool, Begin == Last>;
165
166using multi_element_range = std::false_type;
167using one_element_range = std::true_type;
168
169///////////////////// Non greedy fields count search. Templates instantiation depth is log(sizeof(T)), templates instantiation count is log(sizeof(T)).
170template <class T, std::size_t Begin, std::size_t Middle>
171constexpr std::size_t detect_fields_count(detail::one_element_range, long) noexcept {
172 static_assert(
173 Begin == Middle,
174 "====================> Boost.PFR: Internal logic error."
175 );
176 return Begin;
177}
178
179template <class T, std::size_t Begin, std::size_t Middle>
180constexpr std::size_t detect_fields_count(detail::multi_element_range, int) noexcept;
181
182template <class T, std::size_t Begin, std::size_t Middle>
183constexpr auto detect_fields_count(detail::multi_element_range, long) noexcept
184 -> detail::enable_if_constructible_helper_t<T, Middle>
185{
186 constexpr std::size_t next_v = Middle + (Middle - Begin + 1) / 2;
187 return detail::detect_fields_count<T, Middle, next_v>(detail::is_one_element_range<Middle, next_v>{}, 1L);
188}
189
190template <class T, std::size_t Begin, std::size_t Middle>
191constexpr std::size_t detect_fields_count(detail::multi_element_range, int) noexcept {
192 constexpr std::size_t next_v = Begin + (Middle - Begin) / 2;
193 return detail::detect_fields_count<T, Begin, next_v>(detail::is_one_element_range<Begin, next_v>{}, 1L);
194}
195
196///////////////////// Greedy search. Templates instantiation depth is log(sizeof(T)), templates instantiation count is log(sizeof(T))*T in worst case.
197template <class T, std::size_t N>
198constexpr auto detect_fields_count_greedy_remember(long) noexcept
199 -> detail::enable_if_constructible_helper_t<T, N>
200{
201 return N;
202}
203
204template <class T, std::size_t N>
205constexpr std::size_t detect_fields_count_greedy_remember(int) noexcept {
206 return 0;
207}
208
209template <class T, std::size_t Begin, std::size_t Last>
210constexpr std::size_t detect_fields_count_greedy(detail::one_element_range) noexcept {
211 static_assert(
212 Begin == Last,
213 "====================> Boost.PFR: Internal logic error."
214 );
215 return detail::detect_fields_count_greedy_remember<T, Begin>(1L);
216}
217
218template <class T, std::size_t Begin, std::size_t Last>
219constexpr std::size_t detect_fields_count_greedy(detail::multi_element_range) noexcept {
220 constexpr std::size_t middle = Begin + (Last - Begin) / 2;
221 constexpr std::size_t fields_count_big_range = detail::detect_fields_count_greedy<T, middle + 1, Last>(
222 detail::is_one_element_range<middle + 1, Last>{}
223 );
224
225 constexpr std::size_t small_range_begin = (fields_count_big_range ? 0 : Begin);
226 constexpr std::size_t small_range_last = (fields_count_big_range ? 0 : middle);
227 constexpr std::size_t fields_count_small_range = detail::detect_fields_count_greedy<T, small_range_begin, small_range_last>(
228 detail::is_one_element_range<small_range_begin, small_range_last>{}
229 );
230 return fields_count_big_range ? fields_count_big_range : fields_count_small_range;
231}
232
233///////////////////// Choosing between array size, greedy and non greedy search.
234template <class T, std::size_t N>
235constexpr auto detect_fields_count_dispatch(size_t_<N>, long, long) noexcept
236 -> typename std::enable_if<std::is_array<T>::value, std::size_t>::type
237{
238 return sizeof(T) / sizeof(typename std::remove_all_extents<T>::type);
239}
240
241template <class T, std::size_t N>
242constexpr auto detect_fields_count_dispatch(size_t_<N>, long, int) noexcept
243 -> decltype(sizeof(T{}))
244{
245 constexpr std::size_t middle = N / 2 + 1;
246 return detail::detect_fields_count<T, 0, middle>(detail::multi_element_range{}, 1L);
247}
248
249template <class T, std::size_t N>
250constexpr std::size_t detect_fields_count_dispatch(size_t_<N>, int, int) noexcept {
251 // T is not default aggregate initialzable. It means that at least one of the members is not default constructible,
252 // so we have to check all the aggregate initializations for T up to N parameters and return the bigest succeeded
253 // (we can not use binary search for detecting fields count).
254 return detail::detect_fields_count_greedy<T, 0, N>(detail::multi_element_range{});
255}
256
257///////////////////// Returns fields count
258template <class T>
259constexpr std::size_t fields_count() noexcept {
260 using type = std::remove_cv_t<T>;
261
262 static_assert(
263 !std::is_reference<type>::value,
264 "====================> Boost.PFR: Attempt to get fields count on a reference. This is not allowed because that could hide an issue and different library users expect different behavior in that case."
265 );
266
267#if !BOOST_PFR_HAS_GUARANTEED_COPY_ELISION
268 static_assert(
269 std::is_copy_constructible<std::remove_all_extents_t<type>>::value || (
270 std::is_move_constructible<std::remove_all_extents_t<type>>::value
271 && std::is_move_assignable<std::remove_all_extents_t<type>>::value
272 ),
273 "====================> Boost.PFR: Type and each field in the type must be copy constructible (or move constructible and move assignable)."
274 );
275#endif // #if !BOOST_PFR_HAS_GUARANTEED_COPY_ELISION
276
277 static_assert(
278 !std::is_polymorphic<type>::value,
279 "====================> Boost.PFR: Type must have no virtual function, because otherwise it is not aggregate initializable."
280 );
281
282#ifdef __cpp_lib_is_aggregate
283 static_assert(
284 std::is_aggregate<type>::value // Does not return `true` for built-in types.
285 || std::is_scalar<type>::value,
286 "====================> Boost.PFR: Type must be aggregate initializable."
287 );
288#endif
289
290// Can't use the following. See the non_std_layout.cpp test.
291//#if !BOOST_PFR_USE_CPP17
292// static_assert(
293// std::is_standard_layout<type>::value, // Does not return `true` for structs that have non standard layout members.
294// "Type must be aggregate initializable."
295// );
296//#endif
297
298#if defined(_MSC_VER) && (_MSC_VER <= 1920)
299 // Workaround for msvc compilers. Versions <= 1920 have a limit of max 1024 elements in template parameter pack
300 constexpr std::size_t max_fields_count = (sizeof(type) * CHAR_BIT >= 1024 ? 1024 : sizeof(type) * CHAR_BIT);
301#else
302 constexpr std::size_t max_fields_count = (sizeof(type) * CHAR_BIT); // We multiply by CHAR_BIT because the type may have bitfields in T
303#endif
304
305 constexpr std::size_t result = detail::detect_fields_count_dispatch<type>(size_t_<max_fields_count>{}, 1L, 1L);
306
307 detail::assert_first_not_base<type>(detail::make_index_sequence<result>{});
308
309#ifndef __cpp_lib_is_aggregate
310 static_assert(
311 is_aggregate_initializable_n<type, result>::value,
312 "====================> Boost.PFR: Types with user specified constructors (non-aggregate initializable types) are not supported."
313 );
314#endif
315
316 static_assert(
317 result != 0 || std::is_empty<type>::value || std::is_fundamental<type>::value || std::is_reference<type>::value,
318 "====================> Boost.PFR: If there's no other failed static asserts then something went wrong. Please report this issue to the github along with the structure you're reflecting."
319 );
320
321 return result;
322}
323
324}}} // namespace boost::pfr::detail
325
326#ifdef __clang__
327# pragma clang diagnostic pop
328#endif
329
330#endif // BOOST_PFR_DETAIL_FIELDS_COUNT_HPP
331

source code of boost/libs/pfr/include/boost/pfr/detail/fields_count.hpp