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
196 changes: 196 additions & 0 deletions include/rfl/internal/DirectFieldNames.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
#ifndef RFL_INTERNAL_DIRECTFIELDNAMES_HPP_
#define RFL_INTERNAL_DIRECTFIELDNAMES_HPP_

#include <array>
#include <cstddef>
#include <span>
#include <string_view>
#include <type_traits>
#include <utility>

#include "../num_fields.hpp"
#include "../parsing/find_field_index.hpp"
#include "get_field_names.hpp"
#ifndef REFLECTCPP_USE_CPP26_REFLECTION
#include "cpp20/get_ith_field_from_fake_object.hpp"
#endif

namespace rfl::internal {

/// Non-templated runtime view over a struct's field names and optional sorted
/// lookup table (`lookup == nullptr` when `size <= 8`, where linear scan over
/// `names` avoids emitting a separate sorted table in `.rodata`).
struct DirectFieldNamesDesc {
const std::string_view* names;
const parsing::detail::FieldNameEntry* lookup;
std::size_t size;

constexpr int find(const std::string_view _name) const noexcept {
if (size <= 8) {
for (std::size_t i = 0; i < size; ++i) {
if (names[i] == _name) {
return static_cast<int>(i);
}
}
return -1;
}
std::size_t lo = 0;
std::size_t hi = size;
while (lo < hi) {
const auto mid = lo + (hi - lo) / 2;
if (lookup[mid].name < _name) {
lo = mid + 1;
} else {
hi = mid;
}
}
return lo < size && lookup[lo].name == _name ? lookup[lo].field_idx : -1;
}

constexpr std::span<const std::string_view> span() const noexcept {
return {names, size};
}
};

#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundefined-var-template"
#pragma clang diagnostic ignored "-Wundefined-inline"
#endif

/// Value-level field name storage and lookup table keyed solely by struct type
/// `T` — never by `StringLiteral` non-type template parameters. All names are
/// copied into a compact null-terminated char buffer so `__PRETTY_FUNCTION__`
/// strings never leak into `.rodata` and every `std::string_view` is
/// null-terminated.
template <class T>
struct DirectFieldNames {
using Type = std::remove_cvref_t<T>;
static constexpr std::size_t size_ = num_fields<Type>;

private:
// Intentionally invoked inside consteval helpers rather than stored as a
// `static constexpr` data member of raw `std::string_view`s so compiler
// `__PRETTY_FUNCTION__` strings are never retained in `.rodata`.
#if __GNUC__
#ifndef __clang__
[[gnu::no_sanitize_undefined]]
#endif
#endif
static consteval auto extract_names() {
return []<std::size_t... Is>(std::index_sequence<Is...>) {
#ifndef REFLECTCPP_USE_CPP26_REFLECTION
#ifdef __clang__
return std::array<std::string_view, sizeof...(Is)>{
cpp20::get_direct_field_name<
Type, cpp20::wrap(
cpp20::get_ith_field_from_fake_object<Type, Is>())>()...};
#else
return std::array<std::string_view, sizeof...(Is)>{
cpp20::get_direct_field_name<
Type, cpp20::get_ith_field_from_fake_object<Type, Is>()>()...};
#endif
#else
return std::array<std::string_view, sizeof...(Is)>{
cpp26::get_direct_field_name<Type, Is>()...};
#endif
}(std::make_index_sequence<size_>{});
}

static consteval auto compute_lengths() {
const auto src = extract_names();
std::array<std::size_t, size_> lens{};
for (std::size_t i = 0; i < size_; ++i) {
lens[i] = src[i].size();
}
return lens;
}

static constexpr auto lengths_ = compute_lengths();

static consteval std::size_t buffer_size() {
std::size_t total = size_;
for (const auto len : lengths_) {
total += len;
}
return total;
}

static consteval auto make_buffer() {
const auto src = extract_names();
std::array<char, buffer_size()> buffer{};
std::size_t offset = 0;
for (const auto name : src) {
for (const char ch : name) {
buffer[offset++] = ch;
}
buffer[offset++] = '\0';
}
return buffer;
}

static constexpr auto buffer_ = make_buffer();

static consteval auto make_names() {
std::array<std::string_view, size_> names{};
std::size_t offset = 0;
for (std::size_t i = 0; i < size_; ++i) {
names[i] = std::string_view(buffer_.data() + offset, lengths_[i]);
offset += lengths_[i] + 1;
}
return names;
}

static consteval auto make_sorted_lookup() {
if constexpr (size_ <= 8) {
return std::array<parsing::detail::FieldNameEntry, 0>{};
} else {
std::array<parsing::detail::FieldNameEntry, size_> table{};
for (std::size_t i = 0; i < size_; ++i) {
table[i] = {names_[i], static_cast<int>(i)};
}
parsing::detail::sort_field_name_table(table);
return table;
}
}

static consteval bool has_unique_names() {
if constexpr (size_ <= 8) {
for (std::size_t i = 0; i < size_; ++i) {
for (std::size_t j = i + 1; j < size_; ++j) {
if (names_[i] == names_[j]) {
return false;
}
}
}
return true;
} else {
for (std::size_t i = 1; i < size_; ++i) {
if (lookup_[i - 1].name == lookup_[i].name) {
return false;
}
}
return true;
}
}

public:
static constexpr auto names_ = make_names();
static constexpr auto lookup_ = make_sorted_lookup();
static_assert(has_unique_names(), "Duplicate field names are not allowed.");

static constexpr DirectFieldNamesDesc desc_{
names_.data(), size_ <= 8 ? nullptr : lookup_.data(), size_};

static constexpr int find(const std::string_view _name) noexcept {
return desc_.find(_name);
}
};

#ifdef __clang__
#pragma clang diagnostic pop
#endif

} // namespace rfl::internal

#endif
13 changes: 9 additions & 4 deletions include/rfl/internal/bind_to_tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@

namespace rfl::internal {

template <class T>
auto bind_to_tuple(T& _t) {
template <class T, class F>
decltype(auto) apply_to_fields(T& _t, F&& _f) {
#ifndef REFLECTCPP_USE_CPP26_REFLECTION
return cpp20::bind_to_tuple(_t);
return cpp20::apply_to_fields(_t, static_cast<F&&>(_f));
#else
return cpp26::bind_to_tuple(_t);
return cpp26::apply_to_fields(_t, static_cast<F&&>(_f));
#endif
}

template <class T>
auto bind_to_tuple(T& _t) {
return apply_to_fields(_t, [](auto&... _refs) { return rfl::make_tuple(&_refs...); });
}

template <class T, typename F>
auto bind_to_tuple(T& _t, const F& _f) {
auto view = bind_to_tuple(_t);
Expand Down
16 changes: 11 additions & 5 deletions include/rfl/internal/cpp20/bind_to_tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ namespace rfl::internal::cpp20 {
#define RFL_INTERNAL_TUPLE_VIEW_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS( \
n, ...) \
auto& [__VA_ARGS__] = _t; \
return [](auto&... _refs) { return rfl::make_tuple(&_refs...); }(__VA_ARGS__);
return std::forward<F>(_f)(__VA_ARGS__);

// The following boilerplate code has been generated with
// scripts/bind_to_tuple_impl.py
template <class T, std::size_t _num_fields>
auto bind_to_tuple_impl(auto& _t) {
template <class T, std::size_t _num_fields, class F>
decltype(auto) apply_to_fields_impl(T& _t, F&& _f) {
if constexpr (_num_fields == 0) {
return rfl::make_tuple();
return std::forward<F>(_f)();
} else {
if constexpr (_num_fields < 129) {
if constexpr (_num_fields < 65) {
Expand Down Expand Up @@ -4384,9 +4384,15 @@ auto bind_to_tuple_impl(auto& _t) {

#undef RFL_INTERNAL_TUPLE_VIEW_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS

template <class T>
decltype(auto) apply_to_fields(T& _t, auto&& _f) {
return apply_to_fields_impl<T, num_fields<std::remove_cv_t<T>>>(
_t, std::forward<decltype(_f)>(_f));
}

template <class T>
auto bind_to_tuple(T& _t) {
return bind_to_tuple_impl<T, num_fields<T>>(_t);
return apply_to_fields(_t, [](auto&... _refs) { return rfl::make_tuple(&_refs...); });
}

} // namespace rfl::internal::cpp20
Expand Down
25 changes: 25 additions & 0 deletions include/rfl/internal/cpp20/get_field_names.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@ consteval auto get_field_name_str_view() {
#endif
}

template <class Lit>
struct single_literal_name_view;

template <StringLiteral _name>
struct single_literal_name_view<rfl::Literal<_name>> {
static constexpr std::string_view value = _name.string_view();
};

/// Returns a field's runtime name without wrapping it in rfl::Literal. Unlike
/// get_field_name(), this is suitable for value-level lookup tables.
template <class T, auto ptr>
consteval std::string_view get_direct_field_name() {
#if defined(__clang__)
using FieldType = std::remove_cvref_t<std::remove_pointer_t<
typename std::remove_pointer_t<decltype(ptr)>::Type>>;
#else
using FieldType = std::remove_cvref_t<std::remove_pointer_t<decltype(ptr)>>;
#endif
if constexpr (is_rename_v<FieldType>) {
return single_literal_name_view<typename FieldType::Name>::value;
} else {
return get_field_name_str_view<T, ptr>();
}
}

template <class T, auto ptr>
consteval auto get_field_name_str_lit() {
constexpr auto name = get_field_name_str_view<T, ptr>();
Expand Down
9 changes: 7 additions & 2 deletions include/rfl/internal/cpp26/bind_to_tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
namespace rfl::internal::cpp26 {

template <class T>
auto bind_to_tuple(T& _t) {
decltype(auto) apply_to_fields(T& _t, auto&& _f) {
using Type = std::remove_cvref_t<T>;
constexpr auto members = std::define_static_array(get_members<Type>());
constexpr auto [... indices] = std::make_index_sequence<members.size()>();
return rfl::make_tuple(&_t.[:members[indices]:]...);
return std::forward<decltype(_f)>(_f)(_t.[:members[indices]:]...);
}

template <class T>
auto bind_to_tuple(T& _t) {
return apply_to_fields(_t, [](auto&... _refs) { return rfl::make_tuple(&_refs...); });
}

} // namespace rfl::internal::cpp26
Expand Down
22 changes: 22 additions & 0 deletions include/rfl/internal/cpp26/get_field_names.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <format>
#include <functional>
#include <meta>
#include <string_view>
#include <type_traits>
#include <utility>

Expand All @@ -20,6 +21,27 @@

namespace rfl::internal::cpp26 {

template <class Lit>
struct single_literal_name_view;

template <StringLiteral _name>
struct single_literal_name_view<rfl::Literal<_name>> {
static constexpr std::string_view value = _name.string_view();
};

template <class T, std::size_t _i>
consteval std::string_view get_direct_field_name() {
using Type = std::remove_cvref_t<T>;
constexpr auto members = std::define_static_array(get_members<Type>());
using FieldType =
std::remove_cvref_t<decltype(std::declval<Type&>().[:members[_i]:])>;
if constexpr (is_rename_v<FieldType>) {
return single_literal_name_view<typename FieldType::Name>::value;
} else {
return std::meta::identifier_of(members[_i]);
}
}

template <class T>
consteval auto get_field_names();

Expand Down
32 changes: 30 additions & 2 deletions include/rfl/internal/has_default_val_v.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
#include <type_traits>

#include "../NamedTuple.hpp"
#include "../named_tuple_t.hpp"
#include "bind_to_tuple.hpp"
#include "is_field.hpp"
#include "is_flatten_field.hpp"
#include "is_default_val_v.hpp"

namespace rfl::internal {
Expand All @@ -26,9 +28,35 @@ struct HasDefaultVal<NamedTuple<Fields...>> {
std::remove_cvref_t<std::remove_pointer_t<typename Fields::Type>>>);
};

template <class T>
constexpr bool has_default_val_type_v() {
using U = std::remove_cvref_t<T>;
if constexpr (is_field_v<U>) {
return has_default_val_type_v<typename U::Type>();
} else if constexpr (is_flatten_field_v<U>) {
return HasDefaultVal<
std::remove_cvref_t<std::remove_pointer_t<typename U::Type>>>::value;
} else {
return is_default_val_v<U>;
}
}

template <class T>
requires(std::is_class_v<T> && std::is_aggregate_v<T>)
struct HasDefaultVal<T> : HasDefaultVal<named_tuple_t<T>> {};
struct HasDefaultVal<T> {
private:
static consteval bool compute() {
using Result = decltype(apply_to_fields(
std::declval<T&>(), []<class... Fields>(Fields&...) {
return std::bool_constant<
(false || ... || has_default_val_type_v<Fields>())>{};
}));
return Result::value;
}

public:
static constexpr bool value = compute();
};

template <class T, size_t N>
struct HasDefaultVal<std::array<T, N>> : HasDefaultVal<std::remove_cvref_t<T>> {
Expand Down
Loading
Loading