mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 16:56:17 +08:00
Fix num_fields to support c-style arrays.
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -1,41 +0,0 @@
|
||||
#ifndef RFL_INTERNAL_HAS_N_FIELDS_HPP_
|
||||
#define RFL_INTERNAL_HAS_N_FIELDS_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#if __GNUC__
|
||||
#ifndef __clang__
|
||||
#pragma GCC system_header
|
||||
#endif
|
||||
#endif
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
struct any {
|
||||
template <typename T>
|
||||
constexpr operator T() const noexcept;
|
||||
};
|
||||
|
||||
template <typename T, typename Is, typename = void>
|
||||
struct constructible_with_n_fields_impl : std::false_type {};
|
||||
|
||||
template <typename T, std::size_t... Is>
|
||||
struct constructible_with_n_fields_impl<
|
||||
T, std::index_sequence<Is...>,
|
||||
std::void_t<decltype(T{(void(Is), any{})...})>> : std::true_type {};
|
||||
|
||||
template <typename T, std::size_t N>
|
||||
using constructible_with_n_fields =
|
||||
constructible_with_n_fields_impl<T, std::make_index_sequence<N>>;
|
||||
|
||||
template <class T, int _n>
|
||||
constexpr bool has_n_fields = constructible_with_n_fields<T, _n>::value &&
|
||||
!constructible_with_n_fields<T, _n + 1>::value;
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
#endif
|
||||
@@ -2,25 +2,78 @@
|
||||
#define RFL_INTERNAL_NUM_FIELDS_HPP_
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include "has_n_fields.hpp"
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace rfl {
|
||||
namespace internal {
|
||||
|
||||
template <class T, std::size_t _n>
|
||||
struct num_fields_impl {
|
||||
static constexpr std::size_t value = num_fields_impl<T, _n + 1>::value;
|
||||
struct any {
|
||||
any(std::size_t);
|
||||
template <typename T>
|
||||
constexpr operator T() const noexcept;
|
||||
};
|
||||
|
||||
template <class T, std::size_t _n>
|
||||
requires has_n_fields<T, _n>
|
||||
struct num_fields_impl<T, _n> {
|
||||
static constexpr std::size_t value = _n;
|
||||
template <typename T>
|
||||
struct count_fields_helper {
|
||||
template <std::size_t n>
|
||||
static consteval bool constructible() {
|
||||
return []<std::size_t... is>(std::index_sequence<is...>) {
|
||||
return requires { T{any(is)...}; };
|
||||
}(std::make_index_sequence<n>());
|
||||
}
|
||||
|
||||
template <std::size_t l, std::size_t nested, std::size_t r>
|
||||
static consteval bool constructible_with_nested() {
|
||||
return []<std::size_t... i, std::size_t... j, std::size_t... k>(
|
||||
std::index_sequence<i...>, std::index_sequence<j...>,
|
||||
std::index_sequence<k...>) {
|
||||
return requires { T{any(i)..., {any(j)...}, any(k)...}; };
|
||||
}(std::make_index_sequence<l>(), std::make_index_sequence<nested>(),
|
||||
std::make_index_sequence<r>());
|
||||
}
|
||||
|
||||
template <std::size_t n = 0>
|
||||
static consteval std::size_t count_max_fields() {
|
||||
static_assert(n <= static_cast<std::size_t>(sizeof(T)));
|
||||
if constexpr (constructible<n>() && !constructible<n + 1>()) {
|
||||
return n;
|
||||
} else {
|
||||
return count_max_fields<n + 1>();
|
||||
}
|
||||
}
|
||||
|
||||
template <std::size_t index, std::size_t size, std::size_t rest>
|
||||
static consteval std::size_t get_nested_array_size() {
|
||||
if constexpr (size < 1) {
|
||||
return 1;
|
||||
} else if constexpr (constructible_with_nested<index, size, rest>() &&
|
||||
!constructible_with_nested<index, size, rest + 1>()) {
|
||||
return size;
|
||||
} else {
|
||||
return get_nested_array_size<index, size - 1, rest + 1>();
|
||||
}
|
||||
}
|
||||
|
||||
template <std::size_t index, std::size_t max>
|
||||
static consteval std::size_t count_fields_impl() {
|
||||
static_assert(index <= max);
|
||||
if constexpr (index == max) {
|
||||
return 0;
|
||||
} else {
|
||||
return 1 +
|
||||
count_fields_impl<
|
||||
index + get_nested_array_size<index, max - index, 0>(), max>();
|
||||
}
|
||||
}
|
||||
|
||||
static consteval std::size_t count_fields() {
|
||||
return count_fields_impl<0, count_max_fields()>();
|
||||
}
|
||||
};
|
||||
|
||||
template <class T>
|
||||
constexpr std::size_t num_fields = num_fields_impl<T, 0>::value;
|
||||
constexpr std::size_t num_fields = count_fields_helper<T>::count_fields();
|
||||
|
||||
} // namespace internal
|
||||
} // namespace rfl
|
||||
|
||||
Reference in New Issue
Block a user