#ifndef RFL_INTERNAL_NUM_FIELDS_HPP_ #define RFL_INTERNAL_NUM_FIELDS_HPP_ /* We infer the number of fields using by figuring out how many fields we need to construct it. This is done by implementing the constructible concept, see below. However, there is a problem with C arrays. Suppose you have a struct like this: struct A{ int arr[3]; }; Then, the struct can be initialized like this: const auto a = A{1, 2, 3}; This is a problem, because a naive logic would believe that A has three fields, when in fact it has only one. That is why we use the constructible concept to get the maximum possible number of fields and then try to subdivide them into arrays in order to figure out which of these fields is in fact an array. Basically, for every field there is, we try to squeeze as many variables into the potential array as we can without missing variables in subsequent fields. This is the purpose of get_nested_array_size(). */ #include #include #include #include #if __GNUC__ #ifndef __clang__ #pragma GCC system_header #endif #endif #ifdef __clang__ #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wundefined-var-template" #pragma clang diagnostic ignored "-Wundefined-inline" #endif namespace rfl { namespace internal { template struct any_empty_base { any_empty_base(std::size_t); template requires(std::is_empty_v> && std::is_base_of_v, std::remove_cv_t> && !std::is_same_v, std::remove_cv_t>) constexpr operator Base&() const noexcept; }; template struct any_base { any_base(std::size_t); template requires(std::is_base_of_v, std::remove_cv_t> && !std::is_same_v, std::remove_cv_t>) constexpr operator Base&() const noexcept; }; struct any { any(std::size_t); template constexpr operator T() const noexcept; }; template struct CountFieldsHelper { template static consteval bool constructible() { return [](std::index_sequence) { return requires { T { any(is)... }; }; }(std::make_index_sequence()); } template static consteval bool constructible_with_nested() { return []< std::size_t... i, std::size_t... j, std:: size_t... k>(std::index_sequence, std::index_sequence, std::index_sequence) { return requires { T { any(i)..., { any(j)... }, any(k)... }; }; }(std::make_index_sequence(), std::make_index_sequence(), std::make_index_sequence()); } template static consteval std::size_t count_max_args_in_agg_init() { static_assert(n <= static_cast(sizeof(T))); if constexpr (constructible() && !constructible()) { return n; } else { return count_max_args_in_agg_init(); } } template static consteval std::size_t get_nested_array_size() { if constexpr (size < 1) { return 1; } else if constexpr (constructible_with_nested() && !constructible_with_nested()) { return size; } else { return get_nested_array_size(); } } template static consteval std::size_t find_the_sole_non_empty_base_index() { static_assert(index < max_args); constexpr auto check = []< std::size_t... l, std::size_t... r>(std::index_sequence, std::index_sequence) { return requires { T { any_empty_base(l)..., any_base(0), any_empty_base(r)... }; }; }; if constexpr (check( std::make_index_sequence(), std::make_index_sequence() )) { return index; } else { return find_the_sole_non_empty_base_index(); } } template static consteval std::size_t get_nested_base_field_count() { static_assert(size <= sizeof(T)); if constexpr (constructible_with_nested() && !constructible_with_nested()) { return size; } else { return get_nested_base_field_count(); } } template static consteval bool has_n_base_param() { constexpr auto right_len = max_arg_num >= n ? max_arg_num - n : 0; return []< std::size_t... l, std::size_t... r>(std::index_sequence, std::index_sequence) { return requires { T { any_base(l)..., any(r)... }; }; }(std::make_index_sequence(), std::make_index_sequence()); } template static consteval std::size_t base_param_num() { if constexpr (!has_n_base_param()) { return index; } else { return base_param_num(); } } template static consteval std::size_t constructible_no_brace_elision() { static_assert(index <= max); if constexpr (index == max) { return 0; } else { return 1 + constructible_no_brace_elision< index + get_nested_array_size(), max>(); } } static consteval std::size_t count_fields() { constexpr std::size_t max_agg_args = count_max_args_in_agg_init(); constexpr std::size_t no_brace_ellison_args = constructible_no_brace_elision<0, max_agg_args>(); constexpr std::size_t base_args = base_param_num(); if constexpr (no_brace_ellison_args == 0 && base_args == 0) { // Empty struct return 0; } else if constexpr (base_args == no_brace_ellison_args) { // Special case when the derived class is empty. // In such cases the filed number is the fields in base class. // Note that there should be only one base class in this case. return get_nested_base_field_count()>(); } else { return no_brace_ellison_args - base_args; } } }; template constexpr std::size_t num_fields = CountFieldsHelper::count_fields(); } // namespace internal } // namespace rfl #ifdef __clang__ #pragma clang diagnostic pop #endif #endif