From b7e4d5324d53f38cf6fc90867963c15c98718af0 Mon Sep 17 00:00:00 2001 From: clockblocker Date: Sat, 28 Nov 2026 07:20:57 +0100 Subject: [PATCH 1/2] perf(internal): add apply_to_fields and avoid NamedTuple in HasDefaultVal --- include/rfl/internal/bind_to_tuple.hpp | 16 +- include/rfl/internal/cpp20/bind_to_tuple.hpp | 18 +- include/rfl/internal/cpp26/bind_to_tuple.hpp | 13 +- include/rfl/internal/has_default_val_v.hpp | 38 +++- scripts/bind_to_tuple.py | 6 +- tests/json/test_has_default_val.cpp | 173 ++++++++++++++++++ .../test_c_array_has_default_val.cpp | 33 ++++ 7 files changed, 277 insertions(+), 20 deletions(-) create mode 100644 tests/json/test_has_default_val.cpp create mode 100644 tests/json_c_arrays_and_inheritance/test_c_array_has_default_val.cpp diff --git a/include/rfl/internal/bind_to_tuple.hpp b/include/rfl/internal/bind_to_tuple.hpp index b6a578c34..42c68ff96 100644 --- a/include/rfl/internal/bind_to_tuple.hpp +++ b/include/rfl/internal/bind_to_tuple.hpp @@ -1,6 +1,8 @@ #ifndef RFL_INTERNAL_BIND_TO_TUPLE_HPP_ #define RFL_INTERNAL_BIND_TO_TUPLE_HPP_ +#include + #ifndef REFLECTCPP_USE_CPP26_REFLECTION #include "cpp20/bind_to_tuple.hpp" #else @@ -9,15 +11,21 @@ namespace rfl::internal { -template -auto bind_to_tuple(T& _t) { +template +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, std::forward(_f)); #else - return cpp26::bind_to_tuple(_t); + return cpp26::apply_to_fields(_t, std::forward(_f)); #endif } +template +auto bind_to_tuple(T& _t) { + return apply_to_fields( + _t, [](auto&... _refs) { return rfl::make_tuple(&_refs...); }); +} + template auto bind_to_tuple(T& _t, const F& _f) { auto view = bind_to_tuple(_t); diff --git a/include/rfl/internal/cpp20/bind_to_tuple.hpp b/include/rfl/internal/cpp20/bind_to_tuple.hpp index 9733f1dbe..aee8e2dfa 100644 --- a/include/rfl/internal/cpp20/bind_to_tuple.hpp +++ b/include/rfl/internal/cpp20/bind_to_tuple.hpp @@ -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)(__VA_ARGS__); // The following boilerplate code has been generated with -// scripts/bind_to_tuple_impl.py -template -auto bind_to_tuple_impl(auto& _t) { +// scripts/bind_to_tuple.py +template +decltype(auto) apply_to_fields_impl(T& _t, F&& _f) { if constexpr (_num_fields == 0) { - return rfl::make_tuple(); + return std::forward(_f)(); } else { if constexpr (_num_fields < 129) { if constexpr (_num_fields < 65) { @@ -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 +decltype(auto) apply_to_fields(T& _t, F&& _f) { + return apply_to_fields_impl>(_t, std::forward(_f)); +} + template auto bind_to_tuple(T& _t) { - return bind_to_tuple_impl>(_t); + return apply_to_fields( + _t, [](auto&... _refs) { return rfl::make_tuple(&_refs...); }); } } // namespace rfl::internal::cpp20 diff --git a/include/rfl/internal/cpp26/bind_to_tuple.hpp b/include/rfl/internal/cpp26/bind_to_tuple.hpp index e98f3f2c8..c798b4766 100644 --- a/include/rfl/internal/cpp26/bind_to_tuple.hpp +++ b/include/rfl/internal/cpp26/bind_to_tuple.hpp @@ -8,18 +8,25 @@ #include #include #include +#include #include "../../Tuple.hpp" #include "get_members.hpp" namespace rfl::internal::cpp26 { -template -auto bind_to_tuple(T& _t) { +template +decltype(auto) apply_to_fields(T& _t, F&& _f) { using Type = std::remove_cvref_t; constexpr auto members = std::define_static_array(get_members()); constexpr auto [... indices] = std::make_index_sequence(); - return rfl::make_tuple(&_t.[:members[indices]:]...); + return std::forward(_f)(_t.[:members[indices]:]...); +} + +template +auto bind_to_tuple(T& _t) { + return apply_to_fields( + _t, [](auto&... _refs) { return rfl::make_tuple(&_refs...); }); } } // namespace rfl::internal::cpp26 diff --git a/include/rfl/internal/has_default_val_v.hpp b/include/rfl/internal/has_default_val_v.hpp index 1d19ef96b..ca1438f0f 100644 --- a/include/rfl/internal/has_default_val_v.hpp +++ b/include/rfl/internal/has_default_val_v.hpp @@ -2,11 +2,15 @@ #define RFL_HASDEFAULTVALV_HPP_ #include +#include #include +#include #include "../NamedTuple.hpp" -#include "../named_tuple_t.hpp" +#include "bind_to_tuple.hpp" #include "is_default_val_v.hpp" +#include "is_field.hpp" +#include "is_flatten_field.hpp" namespace rfl::internal { @@ -26,15 +30,41 @@ struct HasDefaultVal> { std::remove_cvref_t>>); }; +template +constexpr bool has_default_val_type_v() { + using U = std::remove_cvref_t>; + if constexpr (is_field_v) { + return has_default_val_type_v(); + } else if constexpr (is_flatten_field_v) { + return HasDefaultVal< + std::remove_cvref_t>>::value; + } else { + return is_default_val_v; + } +} + +struct HasDefaultValVisitor { + template + constexpr auto operator()(Fields&...) const noexcept + -> std::bool_constant<(false || ... || + has_default_val_type_v())> { + return {}; + } +}; + template requires(std::is_class_v && std::is_aggregate_v) -struct HasDefaultVal : HasDefaultVal> {}; +struct HasDefaultVal { + static constexpr bool value = + decltype(apply_to_fields(std::declval(), + HasDefaultValVisitor{}))::value; +}; -template +template struct HasDefaultVal> : HasDefaultVal> { }; -template +template struct HasDefaultVal : HasDefaultVal> {}; template diff --git a/scripts/bind_to_tuple.py b/scripts/bind_to_tuple.py index 5b29d356d..97fbad499 100644 --- a/scripts/bind_to_tuple.py +++ b/scripts/bind_to_tuple.py @@ -35,10 +35,10 @@ def _generate_bind_to_tuple_impl(maximum: int) -> str: macros = _generate_macros(1, maximum) return ( """ -template -auto bind_to_tuple_impl(auto& _t) { +template +decltype(auto) apply_to_fields_impl(T& _t, F&& _f) { if constexpr(_num_fields == 0) { - return rfl::make_tuple(); + return std::forward(_f)(); } else { """ + macros diff --git a/tests/json/test_has_default_val.cpp b/tests/json/test_has_default_val.cpp new file mode 100644 index 000000000..d9ca4803a --- /dev/null +++ b/tests/json/test_has_default_val.cpp @@ -0,0 +1,173 @@ +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace { +struct AnonPlain { + int a; + double b; +}; +struct AnonDefault { + int a; + rfl::DefaultVal b = 1; +}; +} // namespace + +namespace test_has_default_val { + +struct Empty {}; + +struct Plain { + int a; + std::string b; +}; + +struct DirectDefault { + int a; + rfl::DefaultVal b = 7; +}; + +struct RenamedDefault { + rfl::Rename<"val", rfl::DefaultVal> b = 9; +}; + +struct FieldWrapperDefault { + rfl::Field<"a", int> a; + rfl::Field<"b", rfl::DefaultVal> b; +}; + +struct FieldWrapperNoDefault { + rfl::Field<"a", int> a; + rfl::Field<"b", std::string> b; +}; + +struct NonFlattenedNested { + DirectDefault inner; +}; + +struct FlattenedWithDefault { + int x; + rfl::Flatten inner; +}; + +struct DeepFlattenedWithDefault { + rfl::Flatten outer; +}; + +struct FlattenedPtrWithDefault { + rfl::Flatten inner; +}; + +struct FlattenedNoDefault { + rfl::Flatten inner; +}; + +struct FlattenedNamedTupleWithDefault { + rfl::Flatten>>> nt; +}; + +struct NonDefaultConstructible { + int x; + explicit NonDefaultConstructible(int _x) : x(_x) {} +}; + +struct WithNonDefaultConstructible { + NonDefaultConstructible ndc; + std::unique_ptr ptr; +}; + +struct WithConst { + const int c; + std::array arr; + rfl::DefaultVal d = 5; +}; + +struct BaseWithDefault { + rfl::DefaultVal b = 42; +}; + +struct DerivedEmpty : BaseWithDefault {}; + +class NonAggregate { + int x_ = 0; + + public: + int x() const { return x_; } +}; + +union SimpleUnion { + int a; + double b; +}; + +TEST(json, test_has_default_val_trait_and_apply_to_fields) { + using rfl::internal::apply_to_fields; + using rfl::internal::has_default_val_v; + + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v>); + static_assert(!has_default_val_v); + + static_assert(has_default_val_v); + static_assert(has_default_val_v); + static_assert(has_default_val_v); + static_assert(has_default_val_v); + static_assert(has_default_val_v); + static_assert(has_default_val_v); + static_assert(has_default_val_v); + static_assert(has_default_val_v); + static_assert(has_default_val_v); + static_assert(has_default_val_v); + static_assert(has_default_val_v>); + static_assert(has_default_val_v); + static_assert( + has_default_val_v>>>); + static_assert( + !has_default_val_v>>); + + // Verify apply_to_fields directly on empty, mutable, and const aggregates. + Empty empty{}; + const auto empty_count = + apply_to_fields(empty, [](auto&... _fs) { return sizeof...(_fs); }); + EXPECT_EQ(empty_count, 0U); + + Plain plain{1, "hi"}; + apply_to_fields(plain, [](int& _a, std::string& _b) { + _a = 10; + _b = "bye"; + }); + EXPECT_EQ(plain.a, 10); + EXPECT_EQ(plain.b, "bye"); + + const Plain& cplain = plain; + const auto sum_len = apply_to_fields( + cplain, [](const int& _a, const std::string& _b) { + return _a + static_cast(_b.size()); + }); + EXPECT_EQ(sum_len, 13); + + // End-to-end JSON read using FlattenedWithDefault (exercises DefaultVal path). + const auto parsed = + rfl::json::read(R"({"x":3,"a":4})").value(); + EXPECT_EQ(parsed.x, 3); + EXPECT_EQ(parsed.inner.value().a, 4); + EXPECT_EQ(parsed.inner.value().b.value(), 7); +} + +} // namespace test_has_default_val diff --git a/tests/json_c_arrays_and_inheritance/test_c_array_has_default_val.cpp b/tests/json_c_arrays_and_inheritance/test_c_array_has_default_val.cpp new file mode 100644 index 000000000..3470fefce --- /dev/null +++ b/tests/json_c_arrays_and_inheritance/test_c_array_has_default_val.cpp @@ -0,0 +1,33 @@ +#include + +#include +#include +#include + +namespace test_c_array_has_default_val { + +struct WithCArrayDefault { + const int c = 1; + int arr[3] = {1, 2, 3}; + rfl::DefaultVal d = 5; +}; + +struct WithCArrayPlain { + int arr[3] = {1, 2, 3}; + int b = 2; +}; + +TEST(json, test_c_array_has_default_val) { + using rfl::internal::has_default_val_v; + static_assert(has_default_val_v); + static_assert(!has_default_val_v); + + const auto parsed = + rfl::json::read(R"({"c":9,"arr":[4,5,6]})").value(); + EXPECT_EQ(parsed.c, 9); + EXPECT_EQ(parsed.arr[0], 4); + EXPECT_EQ(parsed.arr[2], 6); + EXPECT_EQ(parsed.d.value(), 5); +} + +} // namespace test_c_array_has_default_val From 638a565fe49282e9c186576fad959eca0f63c7bb Mon Sep 17 00:00:00 2001 From: clockblocker Date: Sat, 28 Nov 2026 07:33:53 +0100 Subject: [PATCH 2/2] fix(internal): address review findings in HasDefaultVal and apply_to_fields --- include/rfl/internal/bind_to_tuple.hpp | 7 +- include/rfl/internal/cpp20/bind_to_tuple.hpp | 4 +- include/rfl/internal/cpp26/bind_to_tuple.hpp | 1 + include/rfl/internal/has_default_val_v.hpp | 21 ++-- scripts/bind_to_tuple.py | 2 +- tests/json/test_has_default_val.cpp | 103 ++++++++++++++++++- 6 files changed, 123 insertions(+), 15 deletions(-) diff --git a/include/rfl/internal/bind_to_tuple.hpp b/include/rfl/internal/bind_to_tuple.hpp index 42c68ff96..49e7333d9 100644 --- a/include/rfl/internal/bind_to_tuple.hpp +++ b/include/rfl/internal/bind_to_tuple.hpp @@ -22,8 +22,11 @@ decltype(auto) apply_to_fields(T& _t, F&& _f) { template auto bind_to_tuple(T& _t) { - return apply_to_fields( - _t, [](auto&... _refs) { return rfl::make_tuple(&_refs...); }); +#ifndef REFLECTCPP_USE_CPP26_REFLECTION + return cpp20::bind_to_tuple(_t); +#else + return cpp26::bind_to_tuple(_t); +#endif } template diff --git a/include/rfl/internal/cpp20/bind_to_tuple.hpp b/include/rfl/internal/cpp20/bind_to_tuple.hpp index aee8e2dfa..c21765e6c 100644 --- a/include/rfl/internal/cpp20/bind_to_tuple.hpp +++ b/include/rfl/internal/cpp20/bind_to_tuple.hpp @@ -5,6 +5,7 @@ #include #include +#include #include #include "../../Tuple.hpp" @@ -4386,7 +4387,8 @@ decltype(auto) apply_to_fields_impl(T& _t, F&& _f) { template decltype(auto) apply_to_fields(T& _t, F&& _f) { - return apply_to_fields_impl>(_t, std::forward(_f)); + return apply_to_fields_impl>>( + _t, std::forward(_f)); } template diff --git a/include/rfl/internal/cpp26/bind_to_tuple.hpp b/include/rfl/internal/cpp26/bind_to_tuple.hpp index c798b4766..be072f285 100644 --- a/include/rfl/internal/cpp26/bind_to_tuple.hpp +++ b/include/rfl/internal/cpp26/bind_to_tuple.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "../../Tuple.hpp" diff --git a/include/rfl/internal/has_default_val_v.hpp b/include/rfl/internal/has_default_val_v.hpp index ca1438f0f..615eee336 100644 --- a/include/rfl/internal/has_default_val_v.hpp +++ b/include/rfl/internal/has_default_val_v.hpp @@ -6,13 +6,17 @@ #include #include -#include "../NamedTuple.hpp" #include "bind_to_tuple.hpp" #include "is_default_val_v.hpp" #include "is_field.hpp" #include "is_flatten_field.hpp" -namespace rfl::internal { +namespace rfl { + +template +class NamedTuple; + +namespace internal { template struct HasDefaultVal; @@ -32,14 +36,14 @@ struct HasDefaultVal> { template constexpr bool has_default_val_type_v() { - using U = std::remove_cvref_t>; - if constexpr (is_field_v) { - return has_default_val_type_v(); - } else if constexpr (is_flatten_field_v) { + using U = std::remove_cvref_t; + if constexpr (is_field::value) { + return is_default_val_v; + } else if constexpr (is_flatten_field::value) { return HasDefaultVal< std::remove_cvref_t>>::value; } else { - return is_default_val_v; + return is_default_val::value; } } @@ -70,6 +74,7 @@ struct HasDefaultVal : HasDefaultVal> {}; template constexpr bool has_default_val_v = HasDefaultVal>::value; -} // namespace rfl::internal +} // namespace internal +} // namespace rfl #endif diff --git a/scripts/bind_to_tuple.py b/scripts/bind_to_tuple.py index 97fbad499..f3b38224f 100644 --- a/scripts/bind_to_tuple.py +++ b/scripts/bind_to_tuple.py @@ -1,5 +1,5 @@ """ -Creates the boilerplate code in rfl/internal/bind_to_tuple.hpp +Creates the boilerplate code in rfl/internal/cpp20/bind_to_tuple.hpp """ diff --git a/tests/json/test_has_default_val.cpp b/tests/json/test_has_default_val.cpp index d9ca4803a..4fe4747a4 100644 --- a/tests/json/test_has_default_val.cpp +++ b/tests/json/test_has_default_val.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include namespace { @@ -21,6 +22,8 @@ struct AnonDefault { namespace test_has_default_val { +struct Incomplete; + struct Empty {}; struct Plain { @@ -42,11 +45,43 @@ struct FieldWrapperDefault { rfl::Field<"b", rfl::DefaultVal> b; }; +struct FieldWrapperRenamedDefault { + rfl::Field<"a", rfl::Rename<"val", rfl::DefaultVal>> a; +}; + struct FieldWrapperNoDefault { rfl::Field<"a", int> a; rfl::Field<"b", std::string> b; }; +struct RawPointerToDefault { + rfl::DefaultVal* p = nullptr; +}; + +struct RawPointerToField { + rfl::Field<"a", rfl::DefaultVal>* p = nullptr; +}; + +struct RawPointerToFlatten { + rfl::Flatten* p = nullptr; +}; + +struct RawPointerToIncompleteFlatten { + rfl::Flatten* p = nullptr; +}; + +struct RawPointerToIncompleteField { + rfl::Field<"a", Incomplete>* p = nullptr; +}; + +struct FieldWrappingFlatten { + rfl::Field<"a", rfl::Flatten> f; +}; + +struct NestedFieldWrapper { + rfl::Field<"a", rfl::Field<"b", rfl::DefaultVal>> f; +}; + struct NonFlattenedNested { DirectDefault inner; }; @@ -72,6 +107,16 @@ struct FlattenedNamedTupleWithDefault { rfl::Flatten>>> nt; }; +struct WithExtraFields { + int a; + rfl::ExtraFields extra; +}; + +struct WithExtraFieldsAndDefault { + rfl::DefaultVal a = 1; + rfl::ExtraFields extra; +}; + struct NonDefaultConstructible { int x; explicit NonDefaultConstructible(int _x) : x(_x) {} @@ -106,32 +151,56 @@ union SimpleUnion { double b; }; +struct RvalueOnlyVisitor { + int& operator()(int& _a, std::string&) && { return _a; } +}; + TEST(json, test_has_default_val_trait_and_apply_to_fields) { using rfl::internal::apply_to_fields; using rfl::internal::has_default_val_v; + struct LocalPlain { + int a; + }; + struct LocalDefault { + rfl::DefaultVal a = 3; + }; + static_assert(!has_default_val_v); static_assert(!has_default_val_v); static_assert(!has_default_val_v); static_assert(!has_default_val_v); - static_assert(has_default_val_v); + static_assert(!has_default_val_v); static_assert(!has_default_val_v); static_assert(!has_default_val_v); static_assert(!has_default_val_v); static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v); static_assert(!has_default_val_v); static_assert(!has_default_val_v); + static_assert(!has_default_val_v); + static_assert(!has_default_val_v>>); static_assert(!has_default_val_v>); static_assert(!has_default_val_v); static_assert(has_default_val_v); static_assert(has_default_val_v); + static_assert(has_default_val_v); + static_assert(has_default_val_v); static_assert(has_default_val_v); static_assert(has_default_val_v); + static_assert(has_default_val_v); static_assert(has_default_val_v); static_assert(has_default_val_v); static_assert(has_default_val_v); static_assert(has_default_val_v); + static_assert(has_default_val_v); static_assert(has_default_val_v); static_assert(has_default_val_v); static_assert(has_default_val_v>); @@ -141,7 +210,31 @@ TEST(json, test_has_default_val_trait_and_apply_to_fields) { static_assert( !has_default_val_v>>); - // Verify apply_to_fields directly on empty, mutable, and const aggregates. + // TaggedUnion wrappers (both pointer and value forms). + static_assert( + has_default_val_v, "d", true>>); + static_assert( + has_default_val_v, "d", false>>); + static_assert( + !has_default_val_v, "p", true>>); + static_assert( + has_default_val_v, "d", true>>); + static_assert( + has_default_val_v, "d", false>>); + static_assert( + !has_default_val_v, "p", true>>); + static_assert( + !has_default_val_v, "p", false>>); + + // Verify apply_to_fields directly on empty, mutable, const, reference return, + // and rvalue-only callable cases. Empty empty{}; const auto empty_count = apply_to_fields(empty, [](auto&... _fs) { return sizeof...(_fs); }); @@ -155,12 +248,16 @@ TEST(json, test_has_default_val_trait_and_apply_to_fields) { EXPECT_EQ(plain.a, 10); EXPECT_EQ(plain.b, "bye"); + int& a_ref = apply_to_fields(plain, RvalueOnlyVisitor{}); + a_ref = 25; + EXPECT_EQ(plain.a, 25); + const Plain& cplain = plain; const auto sum_len = apply_to_fields( cplain, [](const int& _a, const std::string& _b) { return _a + static_cast(_b.size()); }); - EXPECT_EQ(sum_len, 13); + EXPECT_EQ(sum_len, 28); // End-to-end JSON read using FlattenedWithDefault (exercises DefaultVal path). const auto parsed =