Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions include/rfl/internal/cpp20/get_field_names.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#ifndef REFLECTCPP_USE_CPP26_REFLECTION

#include <cstddef>
#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -104,6 +105,24 @@ consteval auto get_field_name() {
#pragma clang diagnostic ignored "-Wundefined-inline"
#endif

template <class T, std::size_t... _is>
#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<T>();
#if defined(__clang__)
return concat_literals(
get_field_name<T,
wrap(rfl::internal::cpp20::select_field_ptr<_is>(ptrs))>()...);
#else
return concat_literals(
get_field_name<T, rfl::internal::cpp20::select_field_ptr<_is>(ptrs)>()...);
#endif
}

template <class T>
#if __GNUC__
#ifndef __clang__
Expand All @@ -113,21 +132,12 @@ template <class T>
consteval auto get_field_names() {
using Type = std::remove_cvref_t<T>;
if constexpr (std::is_pointer_v<Type>) {
return get_field_names<std::remove_pointer_t<T>>();
return get_field_names<std::remove_pointer_t<Type>>();
} else if constexpr (num_fields<Type> == 0) {
return rfl::Literal<>();
} else {
#if defined(__clang__)
const auto get = []<std::size_t... _is>(std::index_sequence<_is...>) {
return concat_literals(
get_field_name<Type,
wrap(get_ith_field_from_fake_object<T, _is>())>()...);
};
#else
const auto get = []<std::size_t... _is>(std::index_sequence<_is...>) {
return concat_literals(
get_field_name<Type, get_ith_field_from_fake_object<T, _is>()>()...);
};
#endif
return get(std::make_index_sequence<num_fields<T>>());
return extract_field_names<Type>(
std::make_index_sequence<num_fields<Type>>());
}
}

Expand Down
60 changes: 52 additions & 8 deletions include/rfl/internal/cpp20/get_ith_field_from_fake_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,40 @@

#include <cstddef>
#include <type_traits>
#include <utility>

#include "../../always_false.hpp"
#include "../nth_element.hpp"
#include "get_fake_object.hpp"
#include "num_fields.hpp"

namespace rfl::internal::cpp20 {

template <std::size_t _i, class Ptr>
struct IndexedFieldPtr {
Ptr ptr_;
};

template <class Seq, class... Ptrs>
struct FieldPtrPack;

template <std::size_t... _is, class... Ptrs>
struct FieldPtrPack<std::index_sequence<_is...>, Ptrs...>
: IndexedFieldPtr<_is, Ptrs>... {};

template <class... Refs>
constexpr auto make_field_ptr_pack_from_refs(Refs&... _refs) noexcept {
return FieldPtrPack<std::index_sequence_for<Refs...>, Refs*...>{{&_refs}...};
}

template <std::size_t _i, class Ptr>
constexpr Ptr select_field_ptr(const IndexedFieldPtr<_i, Ptr>& _p) noexcept {
return _p.ptr_;
}

template <class T, std::size_t n>
struct field_extractor {
template <int _i, class U>
static consteval auto get_ptr(U&) {
template <class U>
static consteval auto get_ptrs(U&) {
static_assert(
rfl::always_false_v<T>,
"\n\nThis error occurs for one of two reasons:\n\n"
Expand All @@ -30,19 +52,30 @@ struct field_extractor {
"classes or custom parsers in the documentation "
"for solutions to this problem.\n\n");
}

template <int _i, class U>
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 <class T> \
struct field_extractor<T, n> { \
template <class U> \
static consteval auto get_ptrs(U& _obj) { \
auto& [__VA_ARGS__] = _obj; \
return rfl::internal::cpp20::make_field_ptr_pack_from_refs(__VA_ARGS__); \
} \
template <int _i, class U> \
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<std::size_t>(_i) < n, "N out of bounds."); \
if constexpr (_i >= 0 && static_cast<std::size_t>(_i) < n) { \
return rfl::internal::cpp20::select_field_ptr< \
static_cast<std::size_t>(_i)>(get_ptrs(_obj)); \
} \
} \
}

Expand Down Expand Up @@ -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 <class T, class U>
consteval auto get_field_ptrs(U& _obj) {
return field_extractor<T, num_fields<T>>::get_ptrs(_obj);
}

template <class T>
consteval auto get_field_ptrs_from_fake_object() {
using Type = std::remove_cvref_t<T>;
return get_field_ptrs<Type>(get_fake_object<Type>());
}

template <class T, int _i, class U>
consteval auto get_ith_field_ptr(U& _obj) {
return field_extractor<T, num_fields<T>>::template get_ptr<_i>(_obj);
Expand Down
104 changes: 69 additions & 35 deletions include/rfl/internal/no_duplicate_field_names.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,82 @@
#include <cstddef>
#include <string_view>
#include <type_traits>
#include <utility>

#include "../Tuple.hpp"
namespace rfl::internal {

namespace rfl {
namespace internal {
template <std::size_t _n>
constexpr bool has_no_duplicate_names(
std::array<std::string_view, _n> _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 <class Fields>
constexpr inline bool no_duplicate_field_names() {
constexpr std::size_t num_fields = rfl::tuple_size_v<Fields>;
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::size_t... _is>(std::index_sequence<_is...>) {
std::array<std::string_view, sizeof...(_is)> arr{
std::remove_cvref_t<tuple_element_t<_is, Fields>>::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<num_fields>());
return result;
}
}

} // namespace internal
} // namespace rfl
template <class Fields>
struct NoDuplicateFieldNames;

template <template <class...> class TupleLike>
struct NoDuplicateFieldNames<TupleLike<>> {
static constexpr bool value = true;
};

template <template <class...> class TupleLike, class SingleField>
struct NoDuplicateFieldNames<TupleLike<SingleField>> {
static constexpr bool value = true;
};

template <template <class...> class TupleLike, class... FieldTypes>
struct NoDuplicateFieldNames<TupleLike<FieldTypes...>> {
static constexpr bool value =
has_no_duplicate_names<sizeof...(FieldTypes)>(
std::array<std::string_view, sizeof...(FieldTypes)>{
{std::remove_cvref_t<FieldTypes>::name_.string_view()...}});
};

template <class Fields>
constexpr inline bool no_duplicate_field_names() {
return NoDuplicateFieldNames<std::remove_cvref_t<Fields>>::value;
}

} // namespace rfl::internal

#endif
81 changes: 58 additions & 23 deletions include/rfl/internal/nth_element.hpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,78 @@
#ifndef RFL_INTERNAL_NTH_ELEMENT_HPP_
#define RFL_INTERNAL_NTH_ELEMENT_HPP_

#include <utility>
#include <cstddef>

namespace rfl::internal {
#if defined(__cpp_pack_indexing) && __cpp_pack_indexing >= 202311L && \
(__cplusplus > 202302L || (defined(_MSVC_LANG) && _MSVC_LANG > 202302L))

template <class T, int N, int _i>
struct ElementWrapper {
T t_;
};
namespace rfl::internal {

template <int N, class T1, int _i1, class T2, int _i2>
constexpr auto operator|(const ElementWrapper<T1, N, _i1>& _e1,
const ElementWrapper<T2, N, _i2>& _e2) {
if constexpr (_i1 == N) {
return _e1;
} else {
return _e2;
/// Returns the N-th argument by decayed value (`auto`). Non-selected arguments
/// are bound by reference only and never copied or moved; only the selected
/// N-th argument is copied (if lvalue) or moved (if rvalue) into the prvalue
/// return value, so binding `const auto&` to the result extends its lifetime
/// safely.
template <int N, class... Ts>
constexpr auto nth_element(Ts&&... _ts) {
static_assert(N >= 0, "N out of bounds.");
static_assert(static_cast<std::size_t>(N) < sizeof...(Ts),
"N out of bounds.");
if constexpr (N >= 0 && static_cast<std::size_t>(N) < sizeof...(Ts)) {
return static_cast<Ts...[static_cast<std::size_t>(N)]&&>(
_ts...[static_cast<std::size_t>(N)]);
}
}

template <class Head, class... Tail>
constexpr auto find_matching_element(const Head& _head, const Tail&... _tail) {
return (_head | ... | _tail);
} // namespace rfl::internal

#else

#include <utility>

namespace rfl::internal {

template <std::size_t _i, class T>
struct IndexedValue {
T&& value_;
};

template <int N, class... Ts, int... _is>
constexpr auto wrap_elements(Ts... _ts, std::integer_sequence<int, _is...>) {
return find_matching_element(ElementWrapper<Ts, N, _is>{_ts}...).t_;
template <class Seq, class... Ts>
struct IndexedValuePack;

template <std::size_t... _is, class... Ts>
struct IndexedValuePack<std::index_sequence<_is...>, Ts...>
: IndexedValue<_is, Ts>... {};

template <class... Ts>
constexpr auto make_indexed_value_pack(Ts&&... _ts) noexcept {
return IndexedValuePack<std::index_sequence_for<Ts...>, Ts...>{
{static_cast<Ts&&>(_ts)}...};
}

template <std::size_t _i, class T>
constexpr T&& select_indexed_value(const IndexedValue<_i, T>& _v) noexcept {
return static_cast<T&&>(_v.value_);
}

/// Returns the N-th argument by decayed value (`auto`). Non-selected arguments
/// are bound by reference only and never copied or moved; only the selected
/// N-th argument is copied (if lvalue) or moved (if rvalue) into the prvalue
/// return value, so binding `const auto&` to the result extends its lifetime
/// safely.
template <int N, class... Ts>
constexpr auto nth_element(Ts... _ts) {
constexpr auto nth_element(Ts&&... _ts) {
static_assert(N >= 0, "N out of bounds.");
static_assert(N < sizeof...(Ts), "N out of bounds.");
return wrap_elements<N, Ts...>(
_ts..., std::make_integer_sequence<int, sizeof...(Ts)>());
static_assert(static_cast<std::size_t>(N) < sizeof...(Ts),
"N out of bounds.");
if constexpr (N >= 0 && static_cast<std::size_t>(N) < sizeof...(Ts)) {
return rfl::internal::select_indexed_value<static_cast<std::size_t>(N)>(
rfl::internal::make_indexed_value_pack(static_cast<Ts&&>(_ts)...));
}
}

} // namespace rfl::internal

#endif

#endif
Loading
Loading