diff --git a/include/rfl/internal/IndexedTypeMap.hpp b/include/rfl/internal/IndexedTypeMap.hpp new file mode 100644 index 000000000..6bc1404bd --- /dev/null +++ b/include/rfl/internal/IndexedTypeMap.hpp @@ -0,0 +1,38 @@ +#ifndef RFL_INTERNAL_INDEXEDTYPEMAP_HPP_ +#define RFL_INTERNAL_INDEXEDTYPEMAP_HPP_ + +#include +#include + +namespace rfl { +namespace internal { + +template +struct IndexedTypeMapEntry { + using type = T; +}; + +template +struct IndexedTypeMapImpl; + +template +struct IndexedTypeMapImpl, Ts...> + : IndexedTypeMapEntry... {}; + +template +IndexedTypeMapEntry select_indexed_type_map_entry( + const IndexedTypeMapEntry*); + +template +using IndexedTypeMap = + IndexedTypeMapImpl, Ts...>; + +template +using indexed_type_at_t = + typename decltype(rfl::internal::select_indexed_type_map_entry( + static_cast(nullptr)))::type; + +} // namespace internal +} // namespace rfl + +#endif // RFL_INTERNAL_INDEXEDTYPEMAP_HPP_ diff --git a/include/rfl/internal/define_literal.hpp b/include/rfl/internal/define_literal.hpp index e97eb3c38..ff109744f 100644 --- a/include/rfl/internal/define_literal.hpp +++ b/include/rfl/internal/define_literal.hpp @@ -1,26 +1,91 @@ #ifndef RFL_INTERNAL_DEFINELITERAL_HPP_ #define RFL_INTERNAL_DEFINELITERAL_HPP_ +#include + #include "../Literal.hpp" +#include "IndexedTypeMap.hpp" +#include "StringLiteral.hpp" namespace rfl { namespace internal { +template +struct to_literal_unit; + +template +struct to_literal_unit> { + using type = Literal<_names...>; +}; + +template +struct concat_literal_types; + +template +struct concat_literal_types, Literal<_c2...>> { + using type = Literal<_c1..., _c2...>; +}; + +template +struct concat_literal_types, Literal<_c2...>, Literal<_c3...>> { + using type = Literal<_c1..., _c2..., _c3...>; +}; + +template +struct concat_literal_types, Literal<_c2...>, Literal<_c3...>, + Literal<_c4...>> { + using type = Literal<_c1..., _c2..., _c3..., _c4...>; +}; + +template +struct merge_literal_units { + static constexpr std::size_t q1 = Count / 4; + static constexpr std::size_t q2 = Count / 2; + static constexpr std::size_t q3 = (3 * Count) / 4; + using type = typename concat_literal_types< + typename merge_literal_units::type, + typename merge_literal_units::type, + typename merge_literal_units::type, + typename merge_literal_units::type>::type; +}; + +template +struct merge_literal_units { + using type = Literal<>; +}; + +template +struct merge_literal_units { + using type = typename to_literal_unit>::type; +}; + +template +struct merge_literal_units { + using type = typename concat_literal_types< + typename to_literal_unit>::type, + typename to_literal_unit>::type>::type; +}; + +template +struct merge_literal_units { + using type = typename concat_literal_types< + typename to_literal_unit>::type, + typename to_literal_unit>::type, + typename to_literal_unit>::type>::type; +}; + /// Allows you to combine several literals. template -struct define_literal; - -/// General case -template -struct define_literal, Literal<_content2...>, Tail...> { - using type = typename define_literal, - Tail...>::type; +struct define_literal { + using type = typename merge_literal_units, 0, + sizeof...(LiteralTypes)>::type; }; -/// Special case - only a single literal is left -template -struct define_literal> { - using type = Literal<_content...>; +/// Fast path when every input is a single-name Literal (including 0 inputs). +template +struct define_literal...> { + using type = Literal<_names...>; }; } // namespace internal diff --git a/include/rfl/internal/define_named_tuple.hpp b/include/rfl/internal/define_named_tuple.hpp index ba71091fb..b04e359dd 100644 --- a/include/rfl/internal/define_named_tuple.hpp +++ b/include/rfl/internal/define_named_tuple.hpp @@ -1,43 +1,115 @@ #ifndef RFL_INTERNAL_DEFINENAMEDTUPLE_HPP_ #define RFL_INTERNAL_DEFINENAMEDTUPLE_HPP_ +#include + #include "../NamedTuple.hpp" +#include "IndexedTypeMap.hpp" +#include "is_named_tuple.hpp" namespace rfl { namespace internal { -template -struct define_named_tuple; +template +struct concat_defined_named_tuples; -/// Allows you to combine several named tuples and/or additional fields. -/// Recursive case - all types are fields. -template -struct define_named_tuple { - using type = typename define_named_tuple, Tail...>::type; +template +struct concat_defined_named_tuples, NamedTuple> { + using type = NamedTuple; }; -/// Allows you to combine several named tuples and/or additional fields. -/// Recursive case - first type is NamedTuple, second type is field. -template -struct define_named_tuple, Head, Tail...> { - using type = typename define_named_tuple, - Tail...>::type; +template +struct concat_defined_named_tuples, NamedTuple, + NamedTuple> { + using type = NamedTuple; }; -/// Allows you to combine several named tuples and/or additional fields. -/// Recursive case - first type is NamedTuple, second type is also NamedTuple. -template -struct define_named_tuple, NamedTuple, - Tail...> { - using type = - typename define_named_tuple, - Tail...>::type; +template +struct concat_defined_named_tuples, NamedTuple, + NamedTuple, NamedTuple> { + using type = NamedTuple; +}; + +template +struct to_defined_named_tuple_unit { + using type = NamedTuple; +}; + +template +struct to_defined_named_tuple_unit> { + using type = NamedTuple; +}; + +template +struct merge_defined_named_tuple_units { + static constexpr std::size_t q1 = Count / 4; + static constexpr std::size_t q2 = Count / 2; + static constexpr std::size_t q3 = (3 * Count) / 4; + using type = typename concat_defined_named_tuples< + typename merge_defined_named_tuple_units::type, + typename merge_defined_named_tuple_units::type, + typename merge_defined_named_tuple_units::type, + typename merge_defined_named_tuple_units::type>::type; +}; + +template +struct merge_defined_named_tuple_units { + using type = NamedTuple<>; +}; + +template +struct merge_defined_named_tuple_units { + using type = + typename to_defined_named_tuple_unit>::type; +}; + +template +struct merge_defined_named_tuple_units { + using type = typename concat_defined_named_tuples< + typename to_defined_named_tuple_unit>::type, + typename to_defined_named_tuple_unit< + indexed_type_at_t>::type>::type; +}; + +template +struct merge_defined_named_tuple_units { + using type = typename concat_defined_named_tuples< + typename to_defined_named_tuple_unit>::type, + typename to_defined_named_tuple_unit< + indexed_type_at_t>::type, + typename to_defined_named_tuple_unit< + indexed_type_at_t>::type>::type; +}; + +template +struct define_named_tuple_dispatch { + using type = typename merge_defined_named_tuple_units, + 0, sizeof...(Ts)>::type; +}; + +template +struct define_named_tuple_dispatch { + using type = NamedTuple; +}; + +template +struct define_named_tuple_dispatch { + using type = typename to_defined_named_tuple_unit::type; +}; + +template +struct define_named_tuple_dispatch { + using type = typename concat_defined_named_tuples< + typename to_defined_named_tuple_unit::type, + typename to_defined_named_tuple_unit::type>::type; }; /// Allows you to combine several named tuples and/or additional fields. -template -struct define_named_tuple> { - using type = NamedTuple; +template +struct define_named_tuple { + using type = typename define_named_tuple_dispatch< + (!is_named_tuple::value && ...), FieldTypes...>::type; }; } // namespace internal diff --git a/include/rfl/internal/define_tagged_union.hpp b/include/rfl/internal/define_tagged_union.hpp index a780148c1..933a53561 100644 --- a/include/rfl/internal/define_tagged_union.hpp +++ b/include/rfl/internal/define_tagged_union.hpp @@ -1,54 +1,142 @@ #ifndef RFL_INTERNAL_DEFINETAGGEDUNION_HPP_ #define RFL_INTERNAL_DEFINETAGGEDUNION_HPP_ +#include +#include + +#include "../NamedTuple.hpp" #include "../TaggedUnion.hpp" +#include "IndexedTypeMap.hpp" #include "StringLiteral.hpp" +#include "is_named_tuple.hpp" namespace rfl { namespace internal { +template +struct to_tagged_union_unit; + +template +struct to_tagged_union_unit<_discriminator, + TaggedUnion<_discriminator, Ts...>> { + using type = TaggedUnion<_discriminator, Ts...>; +}; + +template +struct to_tagged_union_unit<_discriminator, NamedTuple> { + using type = TaggedUnion<_discriminator, NamedTuple>; +}; + +template +struct concat_tagged_union_types; + +template +struct concat_tagged_union_types<_discriminator, + TaggedUnion<_discriminator, Ts1...>, + TaggedUnion<_discriminator, Ts2...>> { + using type = TaggedUnion<_discriminator, Ts1..., Ts2...>; +}; + +template +struct concat_tagged_union_types<_discriminator, + TaggedUnion<_discriminator, Ts1...>, + TaggedUnion<_discriminator, Ts2...>, + TaggedUnion<_discriminator, Ts3...>> { + using type = TaggedUnion<_discriminator, Ts1..., Ts2..., Ts3...>; +}; + +template +struct concat_tagged_union_types<_discriminator, + TaggedUnion<_discriminator, Ts1...>, + TaggedUnion<_discriminator, Ts2...>, + TaggedUnion<_discriminator, Ts3...>, + TaggedUnion<_discriminator, Ts4...>> { + using type = TaggedUnion<_discriminator, Ts1..., Ts2..., Ts3..., Ts4...>; +}; + +template +struct merge_tagged_union_units { + static constexpr std::size_t q1 = Count / 4; + static constexpr std::size_t q2 = Count / 2; + static constexpr std::size_t q3 = (3 * Count) / 4; + using type = typename concat_tagged_union_types< + _discriminator, + typename merge_tagged_union_units<_discriminator, Map, Begin, q1>::type, + typename merge_tagged_union_units<_discriminator, Map, Begin + q1, + q2 - q1>::type, + typename merge_tagged_union_units<_discriminator, Map, Begin + q2, + q3 - q2>::type, + typename merge_tagged_union_units<_discriminator, Map, Begin + q3, + Count - q3>::type>::type; +}; + +template +struct merge_tagged_union_units<_discriminator, Map, Begin, 1> { + using type = typename to_tagged_union_unit< + _discriminator, indexed_type_at_t>::type; +}; + +template +struct merge_tagged_union_units<_discriminator, Map, Begin, 2> { + using type = typename concat_tagged_union_types< + _discriminator, + typename to_tagged_union_unit<_discriminator, + indexed_type_at_t>::type, + typename to_tagged_union_unit<_discriminator, + indexed_type_at_t>::type>:: + type; +}; + +template +struct merge_tagged_union_units<_discriminator, Map, Begin, 3> { + using type = typename concat_tagged_union_types< + _discriminator, + typename to_tagged_union_unit<_discriminator, + indexed_type_at_t>::type, + typename to_tagged_union_unit<_discriminator, + indexed_type_at_t>::type, + typename to_tagged_union_unit<_discriminator, + indexed_type_at_t>::type>:: + type; +}; + +template +struct define_tagged_union_dispatch { + using type = typename merge_tagged_union_units<_discriminator, + IndexedTypeMap, 0, + sizeof...(Ts)>::type; +}; + +template +struct define_tagged_union_dispatch<_discriminator, true, Ts...> { + using type = TaggedUnion<_discriminator, Ts...>; +}; + +template +struct define_tagged_union_dispatch<_discriminator, false, T0> { + using type = typename to_tagged_union_unit<_discriminator, T0>::type; +}; + +template +struct define_tagged_union_dispatch<_discriminator, false, T0, T1> { + using type = typename concat_tagged_union_types< + _discriminator, typename to_tagged_union_unit<_discriminator, T0>::type, + typename to_tagged_union_unit<_discriminator, T1>::type>::type; +}; + /// Allows you to combine several tagged unions. template struct define_tagged_union; -/// Recursive case - both tagged union. -template -struct define_tagged_union< - _discriminator, TaggedUnion<_discriminator, NamedTupleTypes1...>, - TaggedUnion<_discriminator, NamedTupleTypes2...>, Tail...> { - using type = typename define_tagged_union< - _discriminator, - TaggedUnion<_discriminator, NamedTupleTypes1..., NamedTupleTypes2...>, - Tail...>::type; -}; - -/// Recursive case - tagged union plus named tuple. -template -struct define_tagged_union<_discriminator, - TaggedUnion<_discriminator, NamedTupleTypes...>, - NamedTuple, Tail...> { - using type = typename define_tagged_union< - _discriminator, - TaggedUnion<_discriminator, NamedTupleTypes..., - NamedTuple>, - Tail...>::type; -}; - -/// Recursive case - named tuple. -template -struct define_tagged_union<_discriminator, NamedTuple, Tail...> { - using type = typename define_tagged_union< - _discriminator, TaggedUnion<_discriminator, NamedTuple>, - Tail...>::type; -}; - -/// Special case - only a single TaggedUnion is left. -template -struct define_tagged_union<_discriminator, - TaggedUnion<_discriminator, NamedTupleTypes...>> { - using type = TaggedUnion<_discriminator, NamedTupleTypes...>; +template +struct define_tagged_union<_discriminator, Head, Tail...> { + using type = typename define_tagged_union_dispatch< + _discriminator, + (is_named_tuple::value && ... && is_named_tuple::value), Head, + Tail...>::type; }; } // namespace internal diff --git a/include/rfl/internal/define_variant.hpp b/include/rfl/internal/define_variant.hpp index 3c09ebf81..7b0bd9c98 100644 --- a/include/rfl/internal/define_variant.hpp +++ b/include/rfl/internal/define_variant.hpp @@ -1,39 +1,118 @@ #ifndef RFL_INTERNAL_DEFINEVARIANT_HPP_ #define RFL_INTERNAL_DEFINEVARIANT_HPP_ +#include +#include #include +#include "IndexedTypeMap.hpp" + namespace rfl { namespace internal { -/// Allows you to combine several variants. -template -struct define_variant; +template +struct is_std_variant_exact : std::false_type {}; + +template +struct is_std_variant_exact> : std::true_type {}; -/// Recursive case - both variants. -template -struct define_variant, std::variant, Tail...> { - using type = typename define_variant, - Tail...>::type; +template +inline constexpr bool is_std_variant_exact_v = is_std_variant_exact::value; + +template +struct to_variant_unit { + using type = std::variant; }; -/// Recursive case - variant plus other type. -template -struct define_variant, Head, Tail...> { - using type = - typename define_variant, Tail...>::type; +template +struct to_variant_unit> { + using type = std::variant; }; -/// Recursive case - other type. -template -struct define_variant { - using type = typename define_variant, Tail...>::type; +template +struct concat_variant_types; + +template +struct concat_variant_types, std::variant> { + using type = std::variant; +}; + +template +struct concat_variant_types, std::variant, + std::variant> { + using type = std::variant; +}; + +template +struct concat_variant_types, std::variant, + std::variant, std::variant> { + using type = std::variant; +}; + +template +struct merge_variant_units { + static constexpr std::size_t q1 = Count / 4; + static constexpr std::size_t q2 = Count / 2; + static constexpr std::size_t q3 = (3 * Count) / 4; + using type = typename concat_variant_types< + typename merge_variant_units::type, + typename merge_variant_units::type, + typename merge_variant_units::type, + typename merge_variant_units::type>::type; +}; + +template +struct merge_variant_units { + using type = typename to_variant_unit>::type; +}; + +template +struct merge_variant_units { + using type = typename concat_variant_types< + typename to_variant_unit>::type, + typename to_variant_unit>::type>::type; +}; + +template +struct merge_variant_units { + using type = typename concat_variant_types< + typename to_variant_unit>::type, + typename to_variant_unit>::type, + typename to_variant_unit>::type>::type; +}; + +template +struct define_variant_dispatch { + using type = typename merge_variant_units, 0, + sizeof...(Vars)>::type; }; -/// Special case - only a single variant is left. template -struct define_variant> { - using type = std::variant; +struct define_variant_dispatch { + using type = std::variant; +}; + +template +struct define_variant_dispatch { + using type = typename to_variant_unit::type; +}; + +template +struct define_variant_dispatch { + using type = typename concat_variant_types< + typename to_variant_unit::type, + typename to_variant_unit::type>::type; +}; + +/// Allows you to combine several variants. +template +struct define_variant; + +template +struct define_variant { + using type = typename define_variant_dispatch< + (!is_std_variant_exact_v && ... && !is_std_variant_exact_v), + Head, Tail...>::type; }; } // namespace internal diff --git a/include/rfl/internal/remove_fields.hpp b/include/rfl/internal/remove_fields.hpp index c0ec676e0..2963a7b94 100644 --- a/include/rfl/internal/remove_fields.hpp +++ b/include/rfl/internal/remove_fields.hpp @@ -1,82 +1,107 @@ #ifndef RFL_INTERNAL_REMOVEFIELDS_HPP_ #define RFL_INTERNAL_REMOVEFIELDS_HPP_ +#include +#include +#include #include +#include #include "../NamedTuple.hpp" -#include "../Tuple.hpp" -#include "../define_named_tuple.hpp" +#include "IndexedTypeMap.hpp" #include "StringLiteral.hpp" +#include "is_named_tuple.hpp" namespace rfl { namespace internal { -/// Recursively builds a new NamedTuple type from the FieldTypes, leaving out -/// the field signified by _name. -template -struct remove_single_field; - -/// Special case - _i == 0 -template -struct remove_single_field<_OldNamedTupleType, _name, _NewNamedTupleType, 0> { - using type = _NewNamedTupleType; +template +struct KeepFieldsPlan { + std::array indices{}; + std::size_t count = 0; }; -/// General case. -template -struct remove_single_field { - using OldNamedTupleType = std::remove_cvref_t<_OldNamedTupleType>; - - constexpr static int num_fields = - rfl::tuple_size_v; - - using FieldType = std::remove_cvref_t< - tuple_element_t>; - - using NewNamedTupleType = - std::conditional_t<_name == FieldType::name_, _NewNamedTupleType, - define_named_tuple_t<_NewNamedTupleType, FieldType>>; - - using type = typename remove_single_field::type; +template +struct KeepFieldsPlanHolder; + +template +struct KeepFieldsPlanHolder, _names...> { + static consteval KeepFieldsPlan compute() noexcept { + KeepFieldsPlan result{}; + if constexpr (sizeof...(FieldTypes) > 0) { + if constexpr (sizeof...(_names) == 0) { + for (std::size_t i = 0; i < sizeof...(FieldTypes); ++i) { + result.indices[i] = i; + } + result.count = sizeof...(FieldTypes); + } else { + constexpr std::array + field_names{ + std::remove_cvref_t::name_.string_view()...}; + constexpr std::array remove_names{ + _names.string_view()...}; + for (std::size_t i = 0; i < sizeof...(FieldTypes); ++i) { + bool remove = false; + for (std::size_t j = 0; j < sizeof...(_names); ++j) { + if (field_names[i] == remove_names[j]) { + remove = true; + break; + } + } + if (!remove) { + result.indices[result.count++] = i; + } + } + } + } + return result; + } + + static constexpr auto plan = compute(); }; -/// Recursively removes all of the fields signified by _head and _tail from the -/// NamedTupleType. -template -struct remove_fields; - -/// Special case - only head is left. -template -struct remove_fields<_NamedTupleType, _head> { - using NamedTupleType = std::remove_cvref_t<_NamedTupleType>; +template +struct keep_field_at { + using type = indexed_type_at_t; +}; - constexpr static int num_fields = - rfl::tuple_size_v; +template +struct build_filtered_named_tuple; - using type = typename remove_single_field, - num_fields>::type; +template +struct build_filtered_named_tuple> { + using type = + NamedTuple::type...>; }; -/// General case. -template +/// Removes all of the fields signified by _names from the NamedTupleType in a +/// single pass. +template struct remove_fields { - using NamedTupleType = std::remove_cvref_t<_NamedTupleType>; - - constexpr static int num_fields = - rfl::tuple_size_v; + using CleanType = std::remove_cvref_t<_NamedTupleType>; + static_assert(is_named_tuple::value, + "rfl::remove_fields_t requires an rfl::NamedTuple type."); + using type = + typename std::conditional_t::value, + remove_fields, + std::type_identity>>::type; +}; - using NewNamedTupleType = - typename remove_single_field, - num_fields>::type; +template +struct remove_fields, _names...> { + using PlanHolder = KeepFieldsPlanHolder, _names...>; + using Map = IndexedTypeMap...>; + using type = typename build_filtered_named_tuple< + PlanHolder, Map, std::make_index_sequence>::type; +}; - using type = typename remove_fields::type; +template +struct remove_fields, _names...> { + using type = NamedTuple<>; }; } // namespace internal } // namespace rfl -#endif // RFL_REMOVEFIELDS_HPP_ +#endif // RFL_INTERNAL_REMOVEFIELDS_HPP_