diff --git a/include/rfl/internal/cpp20/get_field_names.hpp b/include/rfl/internal/cpp20/get_field_names.hpp index 707a0590a..e60222023 100644 --- a/include/rfl/internal/cpp20/get_field_names.hpp +++ b/include/rfl/internal/cpp20/get_field_names.hpp @@ -3,6 +3,7 @@ #ifndef REFLECTCPP_USE_CPP26_REFLECTION +#include #include #include @@ -104,6 +105,24 @@ consteval auto get_field_name() { #pragma clang diagnostic ignored "-Wundefined-inline" #endif +template +#if __GNUC__ +#ifndef __clang__ +[[gnu::no_sanitize_undefined]] +#endif +#endif +consteval auto extract_field_names(std::index_sequence<_is...>) { + constexpr auto ptrs = get_field_ptrs_from_fake_object(); +#if defined(__clang__) + return concat_literals( + get_field_name(ptrs))>()...); +#else + return concat_literals( + get_field_name(ptrs)>()...); +#endif +} + template #if __GNUC__ #ifndef __clang__ @@ -113,21 +132,12 @@ template consteval auto get_field_names() { using Type = std::remove_cvref_t; if constexpr (std::is_pointer_v) { - return get_field_names>(); + return get_field_names>(); + } else if constexpr (num_fields == 0) { + return rfl::Literal<>(); } else { -#if defined(__clang__) - const auto get = [](std::index_sequence<_is...>) { - return concat_literals( - get_field_name())>()...); - }; -#else - const auto get = [](std::index_sequence<_is...>) { - return concat_literals( - get_field_name()>()...); - }; -#endif - return get(std::make_index_sequence>()); + return extract_field_names( + std::make_index_sequence>()); } } diff --git a/include/rfl/internal/cpp20/get_ith_field_from_fake_object.hpp b/include/rfl/internal/cpp20/get_ith_field_from_fake_object.hpp index d29949677..4062dfad4 100644 --- a/include/rfl/internal/cpp20/get_ith_field_from_fake_object.hpp +++ b/include/rfl/internal/cpp20/get_ith_field_from_fake_object.hpp @@ -5,18 +5,40 @@ #include #include +#include #include "../../always_false.hpp" -#include "../nth_element.hpp" #include "get_fake_object.hpp" #include "num_fields.hpp" namespace rfl::internal::cpp20 { +template +struct IndexedFieldPtr { + Ptr ptr_; +}; + +template +struct FieldPtrPack; + +template +struct FieldPtrPack, Ptrs...> + : IndexedFieldPtr<_is, Ptrs>... {}; + +template +constexpr auto make_field_ptr_pack_from_refs(Refs&... _refs) noexcept { + return FieldPtrPack, Refs*...>{{&_refs}...}; +} + +template +constexpr Ptr select_field_ptr(const IndexedFieldPtr<_i, Ptr>& _p) noexcept { + return _p.ptr_; +} + template struct field_extractor { - template - static consteval auto get_ptr(U&) { + template + static consteval auto get_ptrs(U&) { static_assert( rfl::always_false_v, "\n\nThis error occurs for one of two reasons:\n\n" @@ -30,19 +52,30 @@ struct field_extractor { "classes or custom parsers in the documentation " "for solutions to this problem.\n\n"); } + + template + static consteval auto get_ptr(U& _obj) { + return get_ptrs(_obj); + } }; #define RFL_INTERNAL_FAKE_OBJECT_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS( \ n, ...) \ template \ struct field_extractor { \ + template \ + static consteval auto get_ptrs(U& _obj) { \ + auto& [__VA_ARGS__] = _obj; \ + return rfl::internal::cpp20::make_field_ptr_pack_from_refs(__VA_ARGS__); \ + } \ template \ static consteval auto get_ptr(U& _obj) { \ - auto& [__VA_ARGS__] = _obj; \ - const auto get_ptrs = [](auto&... _refs) { \ - return nth_element<_i>(&_refs...); \ - }; \ - return get_ptrs(__VA_ARGS__); \ + static_assert(_i >= 0, "N out of bounds."); \ + static_assert(static_cast(_i) < n, "N out of bounds."); \ + if constexpr (_i >= 0 && static_cast(_i) < n) { \ + return rfl::internal::cpp20::select_field_ptr< \ + static_cast(_i)>(get_ptrs(_obj)); \ + } \ } \ } @@ -2817,6 +2850,17 @@ RFL_INTERNAL_FAKE_OBJECT_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS( #undef RFL_INTERNAL_FAKE_OBJECT_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS +template +consteval auto get_field_ptrs(U& _obj) { + return field_extractor>::get_ptrs(_obj); +} + +template +consteval auto get_field_ptrs_from_fake_object() { + using Type = std::remove_cvref_t; + return get_field_ptrs(get_fake_object()); +} + template consteval auto get_ith_field_ptr(U& _obj) { return field_extractor>::template get_ptr<_i>(_obj); diff --git a/include/rfl/internal/no_duplicate_field_names.hpp b/include/rfl/internal/no_duplicate_field_names.hpp index 86bf5f671..986dcb6b2 100644 --- a/include/rfl/internal/no_duplicate_field_names.hpp +++ b/include/rfl/internal/no_duplicate_field_names.hpp @@ -5,48 +5,82 @@ #include #include #include -#include -#include "../Tuple.hpp" +namespace rfl::internal { -namespace rfl { -namespace internal { +template +constexpr bool has_no_duplicate_names( + std::array _arr) noexcept { + if constexpr (_n <= 1) { + return true; + } else { + const auto sift_down = [&](std::size_t _root, std::size_t _end) noexcept { + while (true) { + const std::size_t left = 2 * _root + 1; + if (left >= _end) { + break; + } + std::size_t largest = left; + const std::size_t right = left + 1; + if (right < _end && _arr[largest] < _arr[right]) { + largest = right; + } + if (!(_arr[_root] < _arr[largest])) { + break; + } + const auto tmp = _arr[_root]; + _arr[_root] = _arr[largest]; + _arr[largest] = tmp; + _root = largest; + } + }; -template -constexpr inline bool no_duplicate_field_names() { - constexpr std::size_t num_fields = rfl::tuple_size_v; + for (std::size_t i = _n / 2; i > 0; --i) { + sift_down(i - 1, _n); + } - if constexpr (num_fields <= 1) { + for (std::size_t end = _n - 1; end > 0; --end) { + const auto tmp = _arr[0]; + _arr[0] = _arr[end]; + _arr[end] = tmp; + sift_down(0, end); + } + + for (std::size_t i = 1; i < _n; ++i) { + if (_arr[i] == _arr[i - 1]) { + return false; + } + } return true; - } else { - constexpr bool result = - [](std::index_sequence<_is...>) { - std::array arr{ - std::remove_cvref_t>::name_ - .string_view()...}; - for (std::size_t gap = arr.size() / 2; gap > 0; gap /= 2) { - for (std::size_t i = gap; i < arr.size(); ++i) { - const auto key = arr[i]; - std::size_t j = i; - while (j >= gap && key < arr[j - gap]) { - arr[j] = arr[j - gap]; - j -= gap; - } - arr[j] = key; - } - } - for (std::size_t i = 1; i < arr.size(); ++i) { - if (arr[i] == arr[i - 1]) { - return false; - } - } - return true; - }(std::make_index_sequence()); - return result; } } -} // namespace internal -} // namespace rfl +template +struct NoDuplicateFieldNames; + +template