diff --git a/include/rfl/internal/bind_to_tuple.hpp b/include/rfl/internal/bind_to_tuple.hpp index b6a578c34..49e7333d9 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,6 +11,15 @@ namespace rfl::internal { +template +decltype(auto) apply_to_fields(T& _t, F&& _f) { +#ifndef REFLECTCPP_USE_CPP26_REFLECTION + return cpp20::apply_to_fields(_t, std::forward(_f)); +#else + return cpp26::apply_to_fields(_t, std::forward(_f)); +#endif +} + template auto bind_to_tuple(T& _t) { #ifndef REFLECTCPP_USE_CPP26_REFLECTION diff --git a/include/rfl/internal/cpp20/bind_to_tuple.hpp b/include/rfl/internal/cpp20/bind_to_tuple.hpp index 9733f1dbe..218e34c70 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" @@ -16,14 +17,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 +4385,16 @@ 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 cpp20::apply_to_fields_impl>>( + _t, std::forward(_f)); +} + template auto bind_to_tuple(T& _t) { - return bind_to_tuple_impl>(_t); + return cpp20::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..050e7208c 100644 --- a/include/rfl/internal/cpp26/bind_to_tuple.hpp +++ b/include/rfl/internal/cpp26/bind_to_tuple.hpp @@ -8,18 +8,26 @@ #include #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 cpp26::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..c47ca0cae 100644 --- a/include/rfl/internal/has_default_val_v.hpp +++ b/include/rfl/internal/has_default_val_v.hpp @@ -2,44 +2,85 @@ #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 { +namespace rfl { -template -struct HasDefaultVal; +template +class NamedTuple; + +namespace internal { template struct HasDefaultVal { static constexpr bool value = false; }; +// NamedTupleParser invokes has_default_val_v on view NamedTuples whose fields +// hold pointers to the original members (e.g. Field*>), so +// this specialization intentionally strips one pointer level. template struct HasDefaultVal> { static constexpr bool value = (false || ... || - is_default_val_v< - std::remove_cvref_t>>); + is_default_val>>::value); }; template - requires(std::is_class_v && std::is_aggregate_v) -struct HasDefaultVal : HasDefaultVal> {}; +struct is_named_tuple_spec : std::false_type {}; + +template +struct is_named_tuple_spec> : std::true_type {}; + +template +constexpr bool has_default_val_field() { + using U = std::remove_cvref_t; + if constexpr (is_field::value) { + return is_default_val>::value; + } else if constexpr (is_flatten_field::value) { + return HasDefaultVal< + std::remove_cvref_t>>::value; + } else { + return is_default_val::value; + } +} + +struct HasDefaultValVisitor { + template + constexpr auto operator()(Fields&...) const noexcept + -> std::bool_constant<(false || ... || + has_default_val_field())> { + return {}; + } +}; + +template + requires(!is_named_tuple_spec::value && std::is_class_v && + std::is_aggregate_v) +struct HasDefaultVal { + static constexpr bool value = decltype(rfl::internal::apply_to_fields( + std::declval(), HasDefaultValVisitor{}))::value; +}; -template +template struct HasDefaultVal> : HasDefaultVal> { }; -template +template 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 5b29d356d..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 """ @@ -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