From 9c9eceab929a65aa9a7c846940ba60675fd243b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Sun, 28 Jul 2024 15:35:38 +0200 Subject: [PATCH] Added rfl::Tuple (#160) --- README.md | 1 + docs/README.md | 4 +- docs/named_tuple.md | 3 +- docs/rfl_tuple.md | 16 + include/rfl.hpp | 3 + include/rfl/AddStructName.hpp | 3 +- include/rfl/Literal.hpp | 14 +- include/rfl/NamedTuple.hpp | 266 ++++++++--------- include/rfl/Tuple.hpp | 276 ++++++++++++++++++ include/rfl/apply.hpp | 31 ++ include/rfl/cbor/Parser.hpp | 13 +- include/rfl/internal/Getter.hpp | 10 +- include/rfl/internal/all_fields.hpp | 6 +- include/rfl/internal/bind_to_tuple.hpp | 27 +- .../copy_flattened_tuple_to_named_tuple.hpp | 30 +- include/rfl/internal/field_type.hpp | 7 +- include/rfl/internal/find_index.hpp | 51 ++-- include/rfl/internal/get_field_names.hpp | 17 +- ...hpp => get_ith_field_from_fake_object.hpp} | 37 +-- include/rfl/internal/get_meta_fields.hpp | 4 +- include/rfl/internal/has_fields.hpp | 41 +-- include/rfl/internal/has_flatten_fields.hpp | 19 +- include/rfl/internal/is_empty.hpp | 3 +- .../internal/move_and_flatten_field_tuple.hpp | 31 +- .../move_field_tuple_to_named_tuple.hpp | 5 +- .../rfl/internal/move_from_named_tuple.hpp | 74 ++--- include/rfl/internal/move_from_tuple.hpp | 31 +- include/rfl/internal/move_to_field_tuple.hpp | 11 +- .../rfl/internal/no_duplicate_field_names.hpp | 10 +- .../rfl/internal/nt_to_ptr_named_tuple.hpp | 49 ++-- include/rfl/internal/nth_element.hpp | 44 +++ include/rfl/internal/nth_element_t.hpp | 60 +--- include/rfl/internal/nth_tuple_element_t.hpp | 23 -- include/rfl/internal/remove_fields.hpp | 10 +- include/rfl/internal/remove_ptrs_tup.hpp | 8 +- .../rfl/internal/to_flattened_ptr_tuple.hpp | 35 ++- include/rfl/internal/to_ptr_field_tuple.hpp | 7 +- include/rfl/internal/to_ptr_named_tuple.hpp | 32 +- include/rfl/internal/to_ptr_tuple.hpp | 4 +- include/rfl/internal/tup_to_ptr_tuple.hpp | 18 +- .../rfl/internal/tuple/accumulate_sizes.hpp | 39 +++ include/rfl/internal/tuple/apply.hpp | 30 ++ .../internal/tuple/calculate_positions.hpp | 39 +++ include/rfl/internal/tuple/concat.hpp | 55 ++++ .../rfl/internal/tuple/make_from_tuple.hpp | 24 ++ include/rfl/internal/wrap_in_fields.hpp | 9 +- include/rfl/make_from_tuple.hpp | 25 ++ include/rfl/msgpack/Parser.hpp | 13 +- include/rfl/parsing/CustomParser.hpp | 3 +- include/rfl/parsing/FieldVariantParser.hpp | 5 +- include/rfl/parsing/Parser.hpp | 1 + include/rfl/parsing/Parser_rfl_tuple.hpp | 22 ++ include/rfl/parsing/Parser_tuple.hpp | 4 +- include/rfl/parsing/TupleParser.hpp | 58 ++-- include/rfl/parsing/TupleReader.hpp | 18 +- include/rfl/parsing/ViewReader.hpp | 8 +- .../ViewReaderWithStrippedFieldNames.hpp | 8 +- include/rfl/tuple_cat.hpp | 23 ++ tests/json/test_deep_nested.cpp | 78 +++++ tests/json/test_rfl_tuple.cpp | 35 +++ tests/json/test_tuple.cpp | 35 +++ 61 files changed, 1300 insertions(+), 566 deletions(-) create mode 100644 docs/rfl_tuple.md create mode 100644 include/rfl/Tuple.hpp create mode 100644 include/rfl/apply.hpp rename include/rfl/internal/{bind_fake_object_to_tuple.hpp => get_ith_field_from_fake_object.hpp} (97%) create mode 100644 include/rfl/internal/nth_element.hpp delete mode 100644 include/rfl/internal/nth_tuple_element_t.hpp create mode 100644 include/rfl/internal/tuple/accumulate_sizes.hpp create mode 100644 include/rfl/internal/tuple/apply.hpp create mode 100644 include/rfl/internal/tuple/calculate_positions.hpp create mode 100644 include/rfl/internal/tuple/concat.hpp create mode 100644 include/rfl/internal/tuple/make_from_tuple.hpp create mode 100644 include/rfl/make_from_tuple.hpp create mode 100644 include/rfl/parsing/Parser_rfl_tuple.hpp create mode 100644 include/rfl/tuple_cat.hpp create mode 100644 tests/json/test_deep_nested.cpp create mode 100644 tests/json/test_rfl_tuple.cpp create mode 100644 tests/json/test_tuple.cpp diff --git a/README.md b/README.md index 2adbc365..034b4cb2 100644 --- a/README.md +++ b/README.md @@ -477,6 +477,7 @@ In addition, it supports the following custom containers: - `rfl::Ref`: Similar to `std::shared_ptr`, but (almost) guaranteed to never be null. - `rfl::Result`: Allows for exception-free programming. - `rfl::TaggedUnion`: Similar to `std::variant`, but with explicit tags that make parsing more efficient. +- `rfl::Tuple`: An alternative to `std::tuple` that compiles considerably faster. - `rfl::Validator`: Allows for automatic input validation. - `rfl::Variant`: An alternative to `std::variant` that compiles considerably faster. diff --git a/docs/README.md b/docs/README.md index 1f03f77e..6fbc15cf 100644 --- a/docs/README.md +++ b/docs/README.md @@ -68,7 +68,9 @@ 5.3) [rfl::NamedTuple](https://github.com/getml/reflect-cpp/blob/main/docs/named_tuple.md) - For structural typing. -5.4) [rfl::to_view](https://github.com/getml/reflect-cpp/blob/main/docs/to_view.md) - For accessing fields of a struct by index or name. +5.4) [rfl::Tuple](https://github.com/getml/reflect-cpp/blob/main/docs/rfl_tuple.md) - An alternative to `std::tuple` that compiles more quickly. + +5.5) [rfl::to_view](https://github.com/getml/reflect-cpp/blob/main/docs/to_view.md) - For accessing fields of a struct by index or name. ## 6) Supported formats diff --git a/docs/named_tuple.md b/docs/named_tuple.md index be2943d6..ae0de8d0 100644 --- a/docs/named_tuple.md +++ b/docs/named_tuple.md @@ -1,6 +1,7 @@ # `rfl::NamedTuple` -`rfl::NamedTuple` is very similar to `std::tuple`, but unlike `std::tuple`, the fields have names. +`rfl::NamedTuple` is very similar to `std::tuple` or `rfl::Tuple`, but unlike +these two structures, the fields have names. In other words, consider the following struct: diff --git a/docs/rfl_tuple.md b/docs/rfl_tuple.md new file mode 100644 index 00000000..3b785d03 --- /dev/null +++ b/docs/rfl_tuple.md @@ -0,0 +1,16 @@ +# `rfl::Tuple` + +The problem with `std::tuple` is that it is recursively implemented. That is fine when you +have a small number of elements in your tuple, but as you increase the size of the tuple, +you will pay a price in terms of compile time. + +That is why reflect-cpp contains `rfl::Tuple`, which in many ways behaves +just like `std::tuple`, but it compiles considerably faster. + +The only downside of `rfl::Tuple` is that it cannot be used inside `constexpr` or `consteval` +functions. + +You can use the functions `rfl::get`, `rfl::make_tuple`, `rfl::tuple_element_t`, +`rfl::tuple_size_v`, `rfl::tuple_cat` or `rfl::apply` to create or access the +tuple and they work the same way as their equivalents in the standard library. +In fact, `std::get` will also work on `rfl::Tuple`. diff --git a/include/rfl.hpp b/include/rfl.hpp index c2897dae..4806e59e 100644 --- a/include/rfl.hpp +++ b/include/rfl.hpp @@ -38,6 +38,7 @@ #include "rfl/Validator.hpp" #include "rfl/Variant.hpp" #include "rfl/always_false.hpp" +#include "rfl/apply.hpp" #include "rfl/as.hpp" #include "rfl/comparisons.hpp" #include "rfl/default.hpp" @@ -51,6 +52,7 @@ #include "rfl/fields.hpp" #include "rfl/from_named_tuple.hpp" #include "rfl/get.hpp" +#include "rfl/make_from_tuple.hpp" #include "rfl/make_named_tuple.hpp" #include "rfl/name_t.hpp" #include "rfl/named_tuple_t.hpp" @@ -60,6 +62,7 @@ #include "rfl/replace.hpp" #include "rfl/to_named_tuple.hpp" #include "rfl/to_view.hpp" +#include "rfl/tuple_cat.hpp" #include "rfl/type_name_t.hpp" #include "rfl/visit.hpp" diff --git a/include/rfl/AddStructName.hpp b/include/rfl/AddStructName.hpp index 7a2272f1..5e4a1524 100644 --- a/include/rfl/AddStructName.hpp +++ b/include/rfl/AddStructName.hpp @@ -5,6 +5,7 @@ #include "Field.hpp" #include "Literal.hpp" +#include "apply.hpp" #include "internal/StringLiteral.hpp" #include "internal/get_type_name.hpp" #include "internal/remove_namespaces.hpp" @@ -23,7 +24,7 @@ struct AddStructName { const auto add_new_field = [](auto&&... _fields) { return make_named_tuple(FieldType(LiteralType()), std::move(_fields)...); }; - return std::apply(add_new_field, std::move(_view.fields())); + return rfl::apply(add_new_field, std::move(_view.fields())); } }; diff --git a/include/rfl/Literal.hpp b/include/rfl/Literal.hpp index b273dc08..2c36d9d0 100644 --- a/include/rfl/Literal.hpp +++ b/include/rfl/Literal.hpp @@ -11,9 +11,9 @@ #include #include "Result.hpp" +#include "Tuple.hpp" #include "internal/StringLiteral.hpp" #include "internal/no_duplicate_field_names.hpp" -#include "internal/nth_tuple_element_t.hpp" namespace rfl { @@ -24,7 +24,7 @@ struct LiteralHelper { template class Literal { - using FieldsType = std::tuple...>; + using FieldsType = rfl::Tuple...>; public: using ValueType = @@ -269,7 +269,7 @@ class Literal { template static void allowed_strings_vec_add_one(std::vector* _values) { - using FieldType = internal::nth_tuple_element_t<_i, FieldsType>; + using FieldType = tuple_element_t<_i, FieldsType>; _values->emplace_back(FieldType::name_.str()); } @@ -289,7 +289,7 @@ class Literal { template void find_name_set_if_matches(std::string* _name) const { if (_i == value_) { - using FieldType = internal::nth_tuple_element_t<_i, FieldsType>; + using FieldType = tuple_element_t<_i, FieldsType>; *_name = FieldType::name_.str(); } } @@ -298,7 +298,7 @@ class Literal { /// the string at compile time within the Literal's own fields. template constexpr static auto find_name_within_own_fields() { - return internal::nth_tuple_element_t<_i, FieldsType>::name_; + return tuple_element_t<_i, FieldsType>::name_; } /// Finds the correct value associated with @@ -326,7 +326,7 @@ class Literal { template static void find_value_set_if_matches(const std::string& _str, bool* _found, int* _idx) { - using FieldType = internal::nth_tuple_element_t<_i, FieldsType>; + using FieldType = tuple_element_t<_i, FieldsType>; if (!*_found && FieldType::name_.string_view() == _str) { *_idx = _i; *_found = true; @@ -339,7 +339,7 @@ class Literal { if constexpr (_i == num_fields_) { return -1; } else { - using FieldType = internal::nth_tuple_element_t<_i, FieldsType>; + using FieldType = tuple_element_t<_i, FieldsType>; if constexpr (FieldType::name_ == _name) { return _i; } else { diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index 69ad53bf..2e31bc0b 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -3,22 +3,24 @@ #include #include -#include #include #include #include "Field.hpp" #include "Literal.hpp" +#include "Tuple.hpp" +#include "apply.hpp" #include "get.hpp" #include "internal/StringLiteral.hpp" #include "internal/find_index.hpp" #include "internal/is_extra_fields.hpp" #include "internal/no_duplicate_field_names.hpp" -#include "internal/nth_tuple_element_t.hpp" +#include "make_from_tuple.hpp" +#include "tuple_cat.hpp" namespace rfl { -/// A named tuple behaves like std::tuple, +/// A named tuple behaves like a tuple, /// but the fields have explicit names, which /// allows for reflection. /// IMPORTANT: We have two template specializations. One with fields, one @@ -30,10 +32,16 @@ class NamedTuple; template class NamedTuple { + template + struct Index {}; + + static constexpr auto seq_ = + std::make_integer_sequence(); + public: - using Fields = std::tuple...>; + using Fields = rfl::Tuple...>; using Names = Literal::name_...>; - using Values = std::tuple::Type...>; + using Values = rfl::Tuple::Type...>; public: /// Construct from the values. @@ -48,36 +56,36 @@ class NamedTuple { /// Construct from the values. NamedTuple( const typename std::remove_cvref::type::Type&... _values) - : values_(std::make_tuple(_values...)) { + : values_(rfl::make_tuple(_values...)) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Construct from the fields. NamedTuple(FieldTypes&&... _fields) - : values_(std::make_tuple(std::move(_fields.value_)...)) { + : values_(rfl::make_tuple(std::move(_fields.value_)...)) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Construct from the fields. NamedTuple(const FieldTypes&... _fields) - : values_(std::make_tuple(_fields.value_...)) { + : values_(rfl::make_tuple(_fields.value_...)) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Construct from a tuple containing fields. - NamedTuple(std::tuple&& _tup) - : NamedTuple(std::make_from_tuple>( - std::forward>(_tup))) { + NamedTuple(rfl::Tuple&& _tup) + : NamedTuple(rfl::make_from_tuple>( + std::forward>(_tup))) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } /// Construct from a tuple containing fields. - NamedTuple(const std::tuple& _tup) - : NamedTuple(std::make_from_tuple>(_tup)) { + NamedTuple(const rfl::Tuple& _tup) + : NamedTuple(rfl::make_from_tuple>(_tup)) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } @@ -91,7 +99,7 @@ class NamedTuple { /// Copy constructor. template NamedTuple(const NamedTuple& _other) - : NamedTuple(retrieve_fields(_other.fields())) { + : NamedTuple(retrieve_fields(_other.fields(), seq_)) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } @@ -99,7 +107,7 @@ class NamedTuple { /// Move constructor. template NamedTuple(NamedTuple&& _other) - : NamedTuple(retrieve_fields(_other.fields())) { + : NamedTuple(retrieve_fields(_other.fields(), seq_)) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } @@ -112,11 +120,11 @@ class NamedTuple { using Head = Field<_name, FType>; if constexpr (sizeof...(Tail) > 0) { return NamedTuple>( - make_fields<1, Head>(std::forward(_head))) + make_fields(seq_, std::forward(_head))) .add(std::forward(_tail)...); } else { return NamedTuple>( - make_fields<1, Head>(std::forward(_head))); + make_fields(seq_, std::forward(_head))); } } @@ -126,30 +134,30 @@ class NamedTuple { using Head = Field<_name, FType>; if constexpr (sizeof...(Tail) > 0) { return NamedTuple>( - make_fields<1, Head>(_head)) + make_fields(seq_, _head)) .add(_tail...); } else { return NamedTuple>( - make_fields<1, Head>(_head)); + make_fields(seq_, _head)); } } - /// Template specialization for std::tuple, so we can pass fields from other + /// Template specialization for rfl::Tuple, so we can pass fields from other /// named tuples. template - auto add(std::tuple&& _tuple, Tail&&... _tail) { + auto add(rfl::Tuple&& _tuple, Tail&&... _tail) { if constexpr (sizeof...(Tail) > 0) { - return add_tuple(std::forward>(_tuple)) + return add_tuple(std::forward>(_tuple)) .add(std::forward(_tail)...); } else { - return add_tuple(std::forward>(_tuple)); + return add_tuple(std::forward>(_tuple)); } } - /// Template specialization for std::tuple, so we can pass fields from other + /// Template specialization for rfl::Tuple, so we can pass fields from other /// named tuples. template - auto add(std::tuple _tuple, const Tail&... _tail) const { + auto add(rfl::Tuple _tuple, const Tail&... _tail) const { if constexpr (sizeof...(Tail) > 0) { return add_tuple(std::move(_tuple)).add(_tail...); } else { @@ -161,7 +169,7 @@ class NamedTuple { /// named tuples. template auto add(NamedTuple&& _named_tuple, Tail&&... _tail) { - return add(std::forward>(_named_tuple.fields()), + return add(std::forward>(_named_tuple.fields()), std::forward(_tail)...); } @@ -177,12 +185,12 @@ class NamedTuple { template auto and_then(const F& _f) { const auto transform_field = [&_f](auto... _fields) { - return std::tuple_cat(_f(std::move(_fields)).fields()...); + return rfl::tuple_cat(_f(std::move(_fields)).fields()...); }; - const auto to_nt = [](std::tuple&& _tup) { + const auto to_nt = [](rfl::Tuple&& _tup) { return NamedTuple(_tup); }; - auto new_fields = std::apply(transform_field, std::move(fields())); + auto new_fields = rfl::apply(transform_field, std::move(fields())); return to_nt(std::move(new_fields)); } @@ -191,12 +199,12 @@ class NamedTuple { template auto and_then(const F& _f) const { const auto transform_field = [&_f](auto... _fields) { - return std::tuple_cat(_f(std::move(_fields)).fields()...); + return rfl::tuple_cat(_f(std::move(_fields)).fields()...); }; - const auto to_nt = [](std::tuple&& _tup) { + const auto to_nt = [](rfl::Tuple&& _tup) { return NamedTuple(_tup); }; - auto new_fields = std::apply(transform_field, std::move(fields())); + auto new_fields = rfl::apply(transform_field, std::move(fields())); return to_nt(std::move(new_fields)); } @@ -207,7 +215,7 @@ class NamedTuple { [&_f](AFields&&... fields) { ((_f(std::forward(fields))), ...); }; - std::apply(apply_to_field, fields()); + rfl::apply(apply_to_field, fields()); } /// Invokes a callable object once for each field in order. @@ -216,14 +224,14 @@ class NamedTuple { const auto apply_to_field = [&_f](const auto&... fields) { ((_f(fields)), ...); }; - std::apply(apply_to_field, fields()); + rfl::apply(apply_to_field, fields()); } /// Returns a tuple containing the fields. - Fields fields() { return make_fields(); } + Fields fields() { return make_fields(seq_); } /// Returns a tuple containing the fields. - Fields fields() const { return make_fields(); } + Fields fields() const { return make_fields(seq_); } /// Gets a field by index. template @@ -280,9 +288,9 @@ class NamedTuple { return values() == _other.values(); } - /// Inequality operator - inline auto operator!=(const rfl::NamedTuple& _other) const { - return !(*this == _other); + /// Three-way comparison operator. + inline auto operator<=>(const rfl::NamedTuple& _other) const { + return values() <=> _other.values(); } /// Returns the number of fields. Note that this is not necessary the same @@ -329,22 +337,22 @@ class NamedTuple { } } - /// Template specialization for std::tuple, so we can pass fields from other + /// Template specialization for rfl::Tuple, so we can pass fields from other /// named tuples. template - auto replace(std::tuple&& _tuple, Tail&&... _tail) { + auto replace(rfl::Tuple&& _tuple, Tail&&... _tail) { if constexpr (sizeof...(Tail) > 0) { - return replace_tuple(std::forward>(_tuple)) + return replace_tuple(std::forward>(_tuple)) .replace(std::forward(_tail)...); } else { - return replace_tuple(std::forward>(_tuple)); + return replace_tuple(std::forward>(_tuple)); } } - /// Template specialization for std::tuple, so we can pass fields from other + /// Template specialization for rfl::Tuple, so we can pass fields from other /// named tuples. template - auto replace(std::tuple _tuple, const Tail&... _tail) const { + auto replace(rfl::Tuple _tuple, const Tail&... _tail) const { if constexpr (sizeof...(Tail) > 0) { return replace_tuple(std::move(_tuple)).replace(_tail...); } else { @@ -370,19 +378,19 @@ class NamedTuple { } /// Returns the size of the named tuple - static constexpr size_t size() { return std::tuple_size_v; } + static constexpr size_t size() { return rfl::tuple_size_v; } /// Creates a new named tuple by applying the supplied function to every /// field. template auto transform(const F& _f) { const auto transform_field = [&_f](auto... fields) { - return std::make_tuple(_f(std::move(fields))...); + return rfl::make_tuple(_f(std::move(fields))...); }; - const auto to_nt = [](std::tuple&& _tup) { + const auto to_nt = [](rfl::Tuple&& _tup) { return NamedTuple(_tup); }; - auto new_fields = std::apply(transform_field, std::move(fields())); + auto new_fields = rfl::apply(transform_field, std::move(fields())); return to_nt(std::move(new_fields)); } @@ -391,40 +399,40 @@ class NamedTuple { template auto transform(const F& _f) const { const auto transform_field = [&_f](auto... fields) { - return std::make_tuple(_f(std::move(fields))...); + return rfl::make_tuple(_f(std::move(fields))...); }; - const auto to_nt = [](std::tuple&& _tup) { + const auto to_nt = [](rfl::Tuple&& _tup) { return NamedTuple(_tup); }; - auto new_fields = std::apply(transform_field, std::move(fields())); + auto new_fields = rfl::apply(transform_field, std::move(fields())); return to_nt(std::move(new_fields)); } - /// Returns the underlying std::tuple. + /// Returns the underlying rfl::Tuple. Values& values() { return values_; } - /// Returns the underlying std::tuple. + /// Returns the underlying rfl::Tuple. const Values& values() const { return values_; } private: /// Adds the elements of a tuple to a newly created named tuple, /// and other elements to a newly created named tuple. template - constexpr auto add_tuple(std::tuple&& _tuple) { + constexpr auto add_tuple(rfl::Tuple&& _tuple) { const auto a = [this](auto&&... _fields) { return this->add(std::forward(_fields)...); }; - return std::apply(a, std::forward>(_tuple)); + return rfl::apply(a, std::forward>(_tuple)); } /// Adds the elements of a tuple to a newly created named tuple, /// and other elements to a newly created named tuple. template - constexpr auto add_tuple(std::tuple&& _tuple) const { + constexpr auto add_tuple(rfl::Tuple&& _tuple) const { const auto a = [this](auto&&... _fields) { return this->add(std::forward(_fields)...); }; - return std::apply(a, std::forward>(_tuple)); + return rfl::apply(a, std::forward>(_tuple)); } /// Unfortunately, MSVC forces us to do this... @@ -460,71 +468,43 @@ class NamedTuple { } /// Generates the fields. - template - auto make_fields(Args&&... _args) { - constexpr auto size = sizeof...(Args) - num_additional_fields; - constexpr auto num_fields = std::tuple_size_v; - constexpr auto i = num_fields - size - 1; - - constexpr bool retrieved_all_fields = size == num_fields; - - if constexpr (retrieved_all_fields) { - return std::make_tuple(std::forward(_args)...); - } else { - // When we add additional fields, it is more intuitive to add - // them to the end, that is why we do it like this. - using FieldType = internal::nth_element_t; + template + auto make_fields(std::integer_sequence, + AdditionalArgs&&... _args) { + const auto wrap = [this](Index<_i>) { + using FieldType = internal::nth_element_t<_i, FieldTypes...>; using T = std::remove_cvref_t; - return make_fields( - FieldType(std::forward(std::get(values_))), - std::forward(_args)...); - } + return FieldType(std::forward(rfl::get<_i>(values_))); + }; + return rfl::make_tuple(wrap(Index<_is>{})..., + std::forward(_args)...); } /// Generates the fields. - template - auto make_fields(Args... _args) const { - constexpr auto size = sizeof...(Args) - num_additional_fields; - constexpr auto num_fields = std::tuple_size_v; - constexpr auto i = num_fields - size - 1; - - constexpr bool retrieved_all_fields = size == num_fields; - - if constexpr (retrieved_all_fields) { - return std::make_tuple(std::move(_args)...); - } else { - // When we add additional fields, it is more intuitive to add - // them to the end, that is why we do it like this. - using FieldType = internal::nth_element_t; - return make_fields(FieldType(std::get(values_)), - std::move(_args)...); - } + template + auto make_fields(std::integer_sequence, + AdditionalArgs... _args) const { + const auto wrap = [this](Index<_i>) { + using FieldType = internal::nth_element_t<_i, FieldTypes...>; + return FieldType(rfl::get<_i>(values_)); + }; + return rfl::make_tuple(wrap(Index<_is>{})..., _args...); } /// Generates a new named tuple with one value replaced with a new value. - template - auto make_replaced(V&& _values, T&& _val, Args&&... _args) const { - constexpr auto size = sizeof...(Args); - - constexpr bool retrieved_all_fields = size == std::tuple_size_v; - - if constexpr (retrieved_all_fields) { - return NamedTuple(std::forward(_args)...); - } else { - using FieldType = internal::nth_element_t; - - if constexpr (size == _index) { - return make_replaced<_index, V, T>( - std::forward(_values), std::forward(_val), - std::forward(_args)..., FieldType(std::forward(_val))); + template + auto make_replaced(V&& _values, T&& _val, + std::integer_sequence) const { + const auto wrap = [&](Index<_i>) { + if constexpr (_i == _index) { + return std::forward(_val); } else { + using FieldType = internal::nth_element_t<_i, FieldTypes...>; using U = typename FieldType::Type; - return make_replaced<_index, V, T>( - std::forward(_values), std::forward(_val), - std::forward(_args)..., - FieldType(std::forward(std::get(_values)))); + return FieldType(std::forward(rfl::get<_i>(_values))); } - } + }; + return NamedTuple(wrap(Index<_is>{})...); } /// We cannot allow duplicate field names. @@ -537,8 +517,8 @@ class NamedTuple { NamedTuple replace_value(T&& _val) { using FieldType = std::remove_cvref_t; constexpr auto index = internal::find_index(); - return make_replaced(std::forward(values_), - std::forward(_val)); + return make_replaced(std::forward(values_), + std::forward(_val), seq_); } /// Replaced the field signified by the field type. @@ -547,63 +527,51 @@ class NamedTuple { using FieldType = std::remove_cvref_t; constexpr auto index = internal::find_index(); auto values = values_; - return make_replaced(std::move(values), - std::forward(_val)); + return make_replaced(std::move(values), std::forward(_val), seq_); } /// Adds the elements of a tuple to a newly created named tuple, /// and other elements to a newly created named tuple. template - auto replace_tuple(std::tuple&& _tuple) { + auto replace_tuple(rfl::Tuple&& _tuple) { const auto r = [this](auto&&... _fields) { return this->replace(std::forward(_fields)...); }; - return std::apply(r, std::forward>(_tuple)); + return rfl::apply(r, std::forward>(_tuple)); } /// Adds the elements of a tuple to a newly created named tuple, /// and other elements to a newly created named tuple. template - auto replace_tuple(std::tuple&& _tuple) const { + auto replace_tuple(rfl::Tuple&& _tuple) const { const auto r = [this](auto&&... _fields) { return this->replace(std::forward(_fields)...); }; - return std::apply(r, std::forward>(_tuple)); + return rfl::apply(r, std::forward>(_tuple)); } /// Retrieves the fields from another tuple. - template + template constexpr static Fields retrieve_fields( - std::tuple&& _other_fields, Args&&... _args) { - constexpr auto size = sizeof...(Args); - - constexpr bool retrieved_all_fields = size == std::tuple_size_v; - - if constexpr (retrieved_all_fields) { - return std::make_tuple(std::forward(_args)...); - } else { + rfl::Tuple&& _other_fields, + std::integer_sequence) { + const auto get_field = [&](Index<_i>) { constexpr auto field_name = - internal::nth_element_t::name_; - + internal::nth_element_t<_i, FieldTypes...>::name_; constexpr auto index = - internal::find_index>(); - - using FieldType = internal::nth_element_t; - + internal::find_index>(); + using FieldType = internal::nth_element_t<_i, FieldTypes...>; using T = std::remove_cvref_t; - - return retrieve_fields( - std::forward>(_other_fields), - std::forward(_args)..., - FieldType(std::forward(std::get(_other_fields).value_))); - } + return FieldType(std::forward(rfl::get(_other_fields).value_)); + }; + return rfl::make_tuple(get_field(Index<_is>{})...); } private: /// The values actually contained in the named tuple. /// As you can see, a NamedTuple is just a normal tuple under-the-hood, /// everything else is resolved at compile time. It should have no - /// runtime overhead over a normal std::tuple. + /// runtime overhead over a normal rfl::Tuple. Values values_; /// The position of rfl::ExtraFields, or -1 if there aren't any. @@ -616,9 +584,9 @@ class NamedTuple { template <> class NamedTuple<> { public: - using Fields = std::tuple<>; + using Fields = rfl::Tuple<>; using Names = Literal<>; - using Values = std::tuple<>; + using Values = rfl::Tuple<>; NamedTuple(){}; @@ -634,10 +602,10 @@ class NamedTuple<> { } } - /// Template specialization for std::tuple, so we can pass fields from other + /// Template specialization for rfl::Tuple, so we can pass fields from other /// named tuples. template - auto add(std::tuple _tuple, const Tail&... _tail) const { + auto add(rfl::Tuple _tuple, const Tail&... _tail) const { if constexpr (sizeof...(Tail) > 0) { return NamedTuple(std::move(_tuple)).add(_tail...); } else { @@ -663,7 +631,7 @@ class NamedTuple<> { void apply(F&&) const {} /// Returns an empty tuple. - auto fields() const { return std::tuple(); } + auto fields() const { return rfl::Tuple(); } /// Must always be 0. size_t num_fields() const { return 0; } @@ -681,7 +649,7 @@ class NamedTuple<> { } /// Returns an empty tuple. - auto values() const { return std::tuple(); } + auto values() const { return rfl::Tuple(); } }; // ---------------------------------------------------------------------------- diff --git a/include/rfl/Tuple.hpp b/include/rfl/Tuple.hpp new file mode 100644 index 00000000..748a5471 --- /dev/null +++ b/include/rfl/Tuple.hpp @@ -0,0 +1,276 @@ +#ifndef RFL_TUPLE_HPP_ +#define RFL_TUPLE_HPP_ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "internal/nth_element_t.hpp" +#include "internal/tuple/calculate_positions.hpp" + +namespace rfl { + +template +class Tuple; + +template <> +class Tuple<> { + public: + Tuple() {} +}; + +template +class Tuple { + static constexpr size_t size_ = sizeof...(Types); + + static constexpr auto positions_ = + internal::tuple::calculate_positions(); + + static constexpr auto seq_ = std::make_integer_sequence{}; + + static constexpr unsigned int num_bytes_ = std::get(positions_); + + using DataType = std::array; + + public: + Tuple(const Types&... _t) { copy_from_types(_t..., seq_); } + + Tuple(Types&&... _t) noexcept { move_from_types(std::move(_t)..., seq_); } + + Tuple() : Tuple(Types()...) {} + + Tuple(const Tuple& _other) { copy_from_other(_other, seq_); } + + Tuple(Tuple&& _other) noexcept { + move_from_other(std::move(_other), seq_); + } + + ~Tuple() { destroy_if_necessary(seq_); } + + /// Gets an element by index. + template + auto& get() { + using Type = internal::nth_element_t<_index, Types...>; + return *(reinterpret_cast(data_.data() + pos<_index>())); + } + + /// Gets an element by index. + template + const auto& get() const { + using Type = internal::nth_element_t<_index, Types...>; + return *(reinterpret_cast(data_.data() + pos<_index>())); + } + + /// Assigns the underlying object. + Tuple& operator=(const Tuple& _other) { + if (this == &_other) { + return *this; + } + auto temp = Tuple(_other); + destroy_if_necessary(seq_); + move_from_other(std::move(temp), seq_); + return *this; + } + + /// Assigns the underlying object. + Tuple& operator=(Tuple&& _other) noexcept { + if (this == &_other) { + return *this; + } + destroy_if_necessary(seq_); + move_from_other(std::move(_other), seq_); + return *this; + } + + /// Equality operator. + template + bool operator==(const Tuple& _other) const noexcept { + static_assert(sizeof...(Types) == sizeof...(OtherTypes), + "The size of the two tuples must be the same."); + const auto is_same = [&](std::integral_constant) -> bool { + return this->get<_i>() == _other.template get<_i>(); + }; + return [&](std::integer_sequence) { + return (true && ... && is_same(std::integral_constant{})); + } + (std::make_integer_sequence()); + } + + /// Three-way comparison operator. + template + auto operator<=>(const Tuple& _other) const noexcept { + static_assert(sizeof...(Types) == sizeof...(OtherTypes), + "The size of the two tuples must be the same."); + + const auto compare = [&](std::strong_ordering* _ordering, + std::integral_constant) { + if (*_ordering != std::strong_ordering::equivalent && + this->get<_i>() != _other.template get<_i>()) { + *_ordering = (this->get<_i>() <=> _other.template get<_i>()); + } + }; + + return [&](std::integer_sequence) { + auto ordering = std::strong_ordering::equivalent; + (compare(&ordering, std::integral_constant{}), ...); + return ordering; + } + (std::make_integer_sequence()); + } + + private: + template + void copy_from_other(const Tuple& _other, + std::integer_sequence) { + const auto copy_one = [this](const auto& _other, + std::integral_constant) { + using Type = internal::nth_element_t<_i, Types...>; + new (reinterpret_cast(data_.data() + pos<_i>())) + Type(_other.template get<_i>()); + }; + (copy_one(_other, std::integral_constant{}), ...); + } + + template + void copy_from_types(const Types&... _types, + std::integer_sequence) { + const auto copy_one = [this](const auto& _t, + std::integral_constant) { + using Type = internal::nth_element_t<_i, Types...>; + new (reinterpret_cast(data_.data() + pos<_i>())) Type(_t); + }; + (copy_one(_types, std::integral_constant{}), ...); + } + + template + void destroy_if_necessary(std::integer_sequence) { + const auto destroy_one = [](auto& _t) { + using Type = std::remove_cvref_t; + if constexpr (std::is_destructible_v) { + _t.~Type(); + } + }; + (destroy_one(get<_is>()), ...); + } + + template + void move_from_other(Tuple&& _other, + std::integer_sequence) { + const auto move_one = [this](auto&& _other, + std::integral_constant) { + using Type = internal::nth_element_t<_i, Types...>; + new (reinterpret_cast(data_.data() + pos<_i>())) + Type(std::move(_other.template get<_i>())); + }; + (move_one(_other, std::integral_constant{}), ...); + } + + template + void move_from_types(Types&&... _types, std::integer_sequence) { + const auto move_one = [this](auto&& _t, + std::integral_constant) { + using Type = internal::nth_element_t<_i, Types...>; + new (reinterpret_cast(data_.data() + pos<_i>())) + Type(std::move(_t)); + }; + (move_one(std::move(_types), std::integral_constant{}), ...); + } + + template + static consteval unsigned int pos() { + return std::get<_i>(positions_); + } + + private: + /// The underlying data, can be any of the underlying types. + alignas(Types...) DataType data_; +}; + +/// Gets an element by index. +template +auto& get(rfl::Tuple& _tup) { + return _tup.template get<_index>(); +} + +/// Gets an element by index. +template +const auto& get(const rfl::Tuple& _tup) { + return _tup.template get<_index>(); +} + +/// Gets an element by index. +template +auto& get(std::tuple& _tup) { + return std::get<_index>(_tup); +} + +/// Gets an element by index. +template +const auto& get(const std::tuple& _tup) { + return std::get<_index>(_tup); +} + +template +auto make_tuple(Types&&... _args) { + return rfl::Tuple...>(std::forward(_args)...); +} + +template +struct tuple_element; + +template +struct tuple_element> { + using type = internal::nth_element_t; +}; + +template +struct tuple_element> { + using type = internal::nth_element_t; +}; + +template +using tuple_element_t = + typename rfl::tuple_element>::type; + +template +struct tuple_size; + +template +struct tuple_size> { + static constexpr auto value = sizeof...(Ts); +}; + +template +struct tuple_size> { + static constexpr auto value = sizeof...(Ts); +}; + +template +inline constexpr auto tuple_size_v = + rfl::tuple_size>::value; + +} // namespace rfl + +namespace std { + +/// Gets an element by index. +template +auto& get(rfl::Tuple& _tup) { + return _tup.template get<_index>(); +} + +/// Gets an element by index. +template +const auto& get(const rfl::Tuple& _tup) { + return _tup.template get<_index>(); +} + +} // namespace std + +#endif diff --git a/include/rfl/apply.hpp b/include/rfl/apply.hpp new file mode 100644 index 00000000..6f2f4963 --- /dev/null +++ b/include/rfl/apply.hpp @@ -0,0 +1,31 @@ +#ifndef RFL_APPLY_HPP_ +#define RFL_APPLY_HPP_ + +#include + +#include "Tuple.hpp" +#include "internal/tuple/apply.hpp" + +namespace rfl { + +template +auto apply(F&& _f, const rfl::Tuple& _tup) { + return internal::tuple::apply( + _f, _tup, std::make_integer_sequence()); +} + +template +auto apply(F&& _f, rfl::Tuple& _tup) { + return internal::tuple::apply( + _f, _tup, std::make_integer_sequence()); +} + +template +auto apply(F&& _f, rfl::Tuple&& _tup) { + return internal::tuple::apply( + _f, std::move(_tup), std::make_integer_sequence()); +} + +} // namespace rfl + +#endif diff --git a/include/rfl/cbor/Parser.hpp b/include/rfl/cbor/Parser.hpp index 8ca2b68f..aba1fc3d 100644 --- a/include/rfl/cbor/Parser.hpp +++ b/include/rfl/cbor/Parser.hpp @@ -1,6 +1,7 @@ #ifndef RFL_CBOR_PARSER_HPP_ #define RFL_CBOR_PARSER_HPP_ +#include "../Tuple.hpp" #include "../parsing/Parser.hpp" #include "Reader.hpp" #include "Writer.hpp" @@ -24,12 +25,22 @@ struct Parser, FieldTypes...> { }; +template +requires AreReaderAndWriter> +struct Parser, ProcessorsType> + : public TupleParser> { +}; + template requires AreReaderAndWriter> struct Parser, ProcessorsType> : public TupleParser { + /*_all_required=*/true, ProcessorsType, + std::tuple> { }; } // namespace parsing diff --git a/include/rfl/internal/Getter.hpp b/include/rfl/internal/Getter.hpp index d65a3f52..8d160eb2 100644 --- a/include/rfl/internal/Getter.hpp +++ b/include/rfl/internal/Getter.hpp @@ -4,9 +4,9 @@ #include #include +#include "../Tuple.hpp" #include "StringLiteral.hpp" #include "find_index.hpp" -#include "nth_tuple_element_t.hpp" namespace rfl::internal { @@ -24,7 +24,7 @@ struct Getter { /// Retrieves the indicated value from the tuple. template static inline auto& get(NamedTupleType& _tup) { - return std::get<_index>(_tup.values()); + return rfl::get<_index>(_tup.values()); } /// Gets a field by name. @@ -41,7 +41,7 @@ struct Getter { constexpr auto index = find_index(); static_assert( - std::is_same::Type, typename Field::Type>(), "If two fields have the same name, " @@ -53,7 +53,7 @@ struct Getter { /// Retrieves the indicated value from the tuple. template static inline const auto& get_const(const NamedTupleType& _tup) { - return std::get<_index>(_tup.values()); + return rfl::get<_index>(_tup.values()); } /// Gets a field by name. @@ -70,7 +70,7 @@ struct Getter { constexpr auto index = find_index(); static_assert( - std::is_same::Type, typename Field::Type>(), "If two fields have the same name, " diff --git a/include/rfl/internal/all_fields.hpp b/include/rfl/internal/all_fields.hpp index 9eef8856..169803ba 100644 --- a/include/rfl/internal/all_fields.hpp +++ b/include/rfl/internal/all_fields.hpp @@ -5,18 +5,18 @@ #include #include +#include "../Tuple.hpp" #include "is_field.hpp" -#include "nth_tuple_element_t.hpp" namespace rfl { namespace internal { template constexpr bool all_fields() { - if constexpr (_i == std::tuple_size_v) { + if constexpr (_i == rfl::tuple_size_v) { return true; } else { - using T = nth_tuple_element_t<_i, TupleType>; + using T = tuple_element_t<_i, TupleType>; return is_field_v && all_fields(); } } diff --git a/include/rfl/internal/bind_to_tuple.hpp b/include/rfl/internal/bind_to_tuple.hpp index b21c5df8..b3d46700 100644 --- a/include/rfl/internal/bind_to_tuple.hpp +++ b/include/rfl/internal/bind_to_tuple.hpp @@ -7,17 +7,17 @@ #include #include +#include "../Tuple.hpp" #include "../always_false.hpp" #include "is_named_tuple.hpp" #include "num_fields.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { template struct tuple_view_helper { template - static constexpr auto tuple_view(T&) { + static auto tuple_view(T&) { static_assert( rfl::always_false_v, "\n\nThis error occurs for one of two reasons:\n\n" @@ -35,16 +35,18 @@ struct tuple_view_helper { template <> struct tuple_view_helper<0> { - static constexpr auto tuple_view(auto&) { return std::tie(); } + static auto tuple_view(auto&) { return rfl::make_tuple(); } }; #define RFL_INTERNAL_TUPLE_VIEW_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS( \ n, ...) \ template <> \ struct tuple_view_helper { \ - static constexpr auto tuple_view(auto& t) { \ + static auto tuple_view(auto& t) { \ auto& [__VA_ARGS__] = t; \ - return std::tie(__VA_ARGS__); \ + return [](auto&... _refs) { \ + return rfl::make_tuple(&_refs...); \ + }(__VA_ARGS__); \ } \ } @@ -545,20 +547,19 @@ RFL_INTERNAL_TUPLE_VIEW_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS( #undef RFL_INTERNAL_TUPLE_VIEW_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS template -constexpr auto tuple_view(T& t) { +auto tuple_view(T& t) { return tuple_view_helper>::tuple_view(t); } template -constexpr auto bind_to_tuple(T& _t, const F& _f) { +auto bind_to_tuple(T& _t, const F& _f) { auto view = tuple_view(_t); - return [&](std::index_sequence) { - return std::make_tuple(_f(std::get(view))...); + return [&](std::index_sequence<_is...>) { + return rfl::make_tuple(_f(rfl::get<_is>(view))...); } - (std::make_index_sequence>()); + (std::make_index_sequence>()); } -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/copy_flattened_tuple_to_named_tuple.hpp b/include/rfl/internal/copy_flattened_tuple_to_named_tuple.hpp index 0d9ae912..0edb7825 100644 --- a/include/rfl/internal/copy_flattened_tuple_to_named_tuple.hpp +++ b/include/rfl/internal/copy_flattened_tuple_to_named_tuple.hpp @@ -4,30 +4,32 @@ #include #include #include +#include #include "../Field.hpp" -#include "lit_name.hpp" #include "../make_named_tuple.hpp" +#include "lit_name.hpp" namespace rfl { namespace internal { +template +auto copy_one_element(const auto& _flattened_tuple) { + const auto name_literal = FieldNames::template name_of<_i>(); + return rfl::make_field< + lit_name_v>>( + std::get<_i>(_flattened_tuple)); +} + template -auto copy_flattened_tuple_to_named_tuple(const auto& _flattened_tuple, - Fields&&... _fields) { +auto copy_flattened_tuple_to_named_tuple(const auto& _flattened_tuple) { constexpr auto size = - std::tuple_size_v>; - constexpr auto i = sizeof...(_fields); - if constexpr (i == size) { - return make_named_tuple(std::move(_fields)...); - } else { - const auto name_literal = FieldNames::template name_of(); - auto new_field = rfl::make_field< - lit_name_v>>( - std::get(_flattened_tuple)); - return copy_flattened_tuple_to_named_tuple( - _flattened_tuple, std::move(_fields)..., std::move(new_field)); + rfl::tuple_size_v>; + return [&](std::integer_sequence) { + return make_named_tuple( + copy_one_element(_flattened_tuple)...); } + (std::make_integer_sequence()); } } // namespace internal diff --git a/include/rfl/internal/field_type.hpp b/include/rfl/internal/field_type.hpp index 0f8f9633..228ec06c 100644 --- a/include/rfl/internal/field_type.hpp +++ b/include/rfl/internal/field_type.hpp @@ -7,10 +7,10 @@ #include "../NamedTuple.hpp" #include "../TaggedUnion.hpp" +#include "../Tuple.hpp" #include "../named_tuple_t.hpp" #include "StringLiteral.hpp" #include "find_index.hpp" -#include "nth_tuple_element_t.hpp" namespace rfl { namespace internal { @@ -30,9 +30,8 @@ struct FieldType { static constexpr int field_ix_ = internal::find_index<_field_name, typename NamedTupleType::Fields>(); - using Type = - typename nth_tuple_element_t::Type; + using Type = typename tuple_element_t::Type; }; /// For variants - in this case the FieldType returned by all options must be diff --git a/include/rfl/internal/find_index.hpp b/include/rfl/internal/find_index.hpp index b04c5a8d..1db6d8c6 100644 --- a/include/rfl/internal/find_index.hpp +++ b/include/rfl/internal/find_index.hpp @@ -4,34 +4,47 @@ #include #include "StringLiteral.hpp" -#include "nth_tuple_element_t.hpp" namespace rfl { namespace internal { -/// Finds the index of the field signified by _field_name -template -constexpr static int find_index() { - using FieldType = std::remove_cvref_t>; +template +struct FieldWrapper { + constexpr static int i_ = _i; +}; - constexpr bool name_i_matches = (FieldType::name_ == _field_name); - - if constexpr (name_i_matches) { - return I; +template +constexpr auto operator|(const FieldWrapper& _f1, + const FieldWrapper& _f2) { + if constexpr (F1::name_ == _field_name) { + return _f1; } else { - constexpr bool out_of_range = I + 1 == std::tuple_size_v; - - static_assert(!out_of_range, "Field name not found!"); - - if constexpr (out_of_range) { - // This is to avoid very confusing error messages. - return I; - } else { - return find_index<_field_name, Fields, I + 1>(); - } + return _f2; } } +template +constexpr auto find_matching_field(const Head& _head, const Tail&... _tail) { + return (_head | ... | _tail); +}; + +template +constexpr auto wrap_fields(std::integer_sequence) { + return find_matching_field(FieldWrapper, + _field_name, _is>{}...) + .i_; +} + +/// Finds the index of the field signified by _field_name +template +constexpr static int find_index() { + constexpr int ix = wrap_fields<_field_name, Fields>( + std::make_integer_sequence>()); + static_assert(rfl::tuple_element_t::name_ == _field_name, + "No matching field found."); + return ix; +} + } // namespace internal } // namespace rfl diff --git a/include/rfl/internal/get_field_names.hpp b/include/rfl/internal/get_field_names.hpp index af383e59..be588fa3 100644 --- a/include/rfl/internal/get_field_names.hpp +++ b/include/rfl/internal/get_field_names.hpp @@ -9,7 +9,7 @@ #endif #include "../Literal.hpp" -#include "bind_fake_object_to_tuple.hpp" +#include "get_ith_field_from_fake_object.hpp" #include "is_flatten_field.hpp" #include "is_rename.hpp" #include "num_fields.hpp" @@ -139,16 +139,15 @@ auto get_field_names() { return get_field_names>(); } else { #if defined(__clang__) - const auto get = [](std::index_sequence) { - return concat_literals( - get_field_name( - bind_fake_object_to_tuple()))>()...); - }; -#else - const auto get = [](std::index_sequence) { + const auto get = [](std::index_sequence<_is...>) { return concat_literals( get_field_name(bind_fake_object_to_tuple())>()...); + wrap(get_ith_field_from_fake_object())>()...); + }; +#else + const auto get = [](std::index_sequence<_is...>) { + return concat_literals( + get_field_name()>()...); }; #endif return get(std::make_index_sequence>()); diff --git a/include/rfl/internal/bind_fake_object_to_tuple.hpp b/include/rfl/internal/get_ith_field_from_fake_object.hpp similarity index 97% rename from include/rfl/internal/bind_fake_object_to_tuple.hpp rename to include/rfl/internal/get_ith_field_from_fake_object.hpp index 548ceab8..552d2d22 100644 --- a/include/rfl/internal/bind_fake_object_to_tuple.hpp +++ b/include/rfl/internal/get_ith_field_from_fake_object.hpp @@ -1,5 +1,5 @@ -#ifndef RFL_INTERNAL_BIND_FAKE_OBJECT_TO_TUPLE_HPP_ -#define RFL_INTERNAL_BIND_FAKE_OBJECT_TO_TUPLE_HPP_ +#ifndef RFL_INTERNAL_GET_ITH_FIELD_FROM_FAKE_OBJECT_HPP_ +#define RFL_INTERNAL_GET_ITH_FIELD_FROM_FAKE_OBJECT_HPP_ #include #include @@ -9,14 +9,15 @@ #include "../always_false.hpp" #include "get_fake_object.hpp" +#include "nth_element.hpp" #include "num_fields.hpp" -namespace rfl { -namespace internal { +namespace rfl::internal { template -struct fake_object_tuple_view_helper { - static consteval auto tuple_view() { +struct fake_object_helper { + template + static consteval auto get_field() { static_assert( rfl::always_false_v, "\n\nThis error occurs for one of two reasons:\n\n" @@ -32,22 +33,17 @@ struct fake_object_tuple_view_helper { } }; -template -struct fake_object_tuple_view_helper { - static consteval auto tuple_view() { return std::tie(); } -}; - #define RFL_INTERNAL_FAKE_OBJECT_IF_YOU_SEE_AN_ERROR_REFER_TO_DOCUMENTATION_ON_C_ARRAYS( \ n, ...) \ template \ - struct fake_object_tuple_view_helper { \ - static consteval auto tuple_view() { \ + struct fake_object_helper { \ + template \ + static consteval auto get_field() { \ const auto& [__VA_ARGS__] = get_fake_object>(); \ - const auto ref_tup = std::tie(__VA_ARGS__); \ const auto get_ptrs = [](const auto&... _refs) { \ - return std::make_tuple(&_refs...); \ + return nth_element<_i>(&_refs...); \ }; \ - return std::apply(get_ptrs, ref_tup); \ + return get_ptrs(__VA_ARGS__); \ } \ } @@ -547,12 +543,11 @@ 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 -consteval auto bind_fake_object_to_tuple() { - return fake_object_tuple_view_helper>::tuple_view(); +template +consteval auto get_ith_field_from_fake_object() { + return fake_object_helper>::template get_field<_i>(); } -} // namespace internal -} // namespace rfl +} // namespace rfl::internal #endif diff --git a/include/rfl/internal/get_meta_fields.hpp b/include/rfl/internal/get_meta_fields.hpp index f3d3a290..30cb0812 100644 --- a/include/rfl/internal/get_meta_fields.hpp +++ b/include/rfl/internal/get_meta_fields.hpp @@ -6,8 +6,8 @@ #include #include "../MetaField.hpp" +#include "../Tuple.hpp" #include "../type_name_t.hpp" -#include "nth_tuple_element_t.hpp" namespace rfl { namespace internal { @@ -18,7 +18,7 @@ auto get_meta_fields(AlreadyExtracted&&... _already_extracted) { if constexpr (NamedTupleType::size() == i) { return std::array{std::move(_already_extracted)...}; } else { - using FieldType = nth_tuple_element_t; + using FieldType = tuple_element_t; auto name = typename FieldType::Name().str(); auto type = type_name_t().str(); return get_meta_fields( diff --git a/include/rfl/internal/has_fields.hpp b/include/rfl/internal/has_fields.hpp index 51923bce..dcfc7ded 100644 --- a/include/rfl/internal/has_fields.hpp +++ b/include/rfl/internal/has_fields.hpp @@ -5,45 +5,52 @@ #include #include +#include "../Tuple.hpp" #include "all_fields.hpp" #include "is_field.hpp" #include "is_flatten_field.hpp" -#include "nth_tuple_element_t.hpp" #include "ptr_tuple_t.hpp" namespace rfl { namespace internal { -template +template constexpr bool all_fields_or_flatten() { - if constexpr (_i == std::tuple_size_v) { - return true; - } else { - using T = std::remove_cvref_t>; + const auto is_true_for_one = + [](std::integral_constant) -> bool { + using T = std::remove_cvref_t>; if constexpr (is_flatten_field_v) { return all_fields_or_flatten< - ptr_tuple_t::Type>>() && - all_fields_or_flatten(); + ptr_tuple_t::Type>>(); } else { - return is_field_v && all_fields_or_flatten(); + return is_field_v; } + }; + + return [&](std::integer_sequence) { + return (true && ... && is_true_for_one(std::integral_constant{})); } + (std::make_integer_sequence>()); } -template +template constexpr bool some_fields_or_flatten() { - if constexpr (_i == std::tuple_size_v) { - return false; - } else { - using T = std::remove_cvref_t>; + const auto is_true_for_one = + [](std::integral_constant) -> bool { + using T = std::remove_cvref_t>; if constexpr (is_flatten_field_v) { return some_fields_or_flatten< - ptr_tuple_t::Type>>() || - some_fields_or_flatten(); + ptr_tuple_t::Type>>(); } else { - return is_field_v || some_fields_or_flatten(); + return is_field_v; } + }; + + return [&](std::integer_sequence) { + return (false || ... || + is_true_for_one(std::integral_constant{})); } + (std::make_integer_sequence>()); } template diff --git a/include/rfl/internal/has_flatten_fields.hpp b/include/rfl/internal/has_flatten_fields.hpp index 976e26cf..ed872d31 100644 --- a/include/rfl/internal/has_flatten_fields.hpp +++ b/include/rfl/internal/has_flatten_fields.hpp @@ -5,20 +5,25 @@ #include #include +#include "../Tuple.hpp" #include "is_flatten_field.hpp" -#include "nth_tuple_element_t.hpp" namespace rfl { namespace internal { -template +template constexpr bool has_flatten_fields() { - if constexpr (_i == std::tuple_size_v) { - return false; - } else { - using T = std::remove_cvref_t>; - return is_flatten_field_v || has_flatten_fields(); + const auto is_true_for_one = + [](std::integral_constant) -> bool { + using T = std::remove_cvref_t>; + return is_flatten_field_v; + }; + + return [&](std::integer_sequence) { + return (false || ... || + is_true_for_one(std::integral_constant{})); } + (std::make_integer_sequence>()); } } // namespace internal diff --git a/include/rfl/internal/is_empty.hpp b/include/rfl/internal/is_empty.hpp index a671ef00..9b1dddf2 100644 --- a/include/rfl/internal/is_empty.hpp +++ b/include/rfl/internal/is_empty.hpp @@ -4,6 +4,7 @@ #include #include +#include "../Tuple.hpp" #include "is_named_tuple.hpp" namespace rfl::internal { @@ -15,7 +16,7 @@ constexpr bool is_empty() { return U::size() == 0; } else { using TupleType = ptr_tuple_t; - return std::tuple_size_v == 0; + return rfl::tuple_size_v == 0; } } diff --git a/include/rfl/internal/move_and_flatten_field_tuple.hpp b/include/rfl/internal/move_and_flatten_field_tuple.hpp index fd7338c6..75e0e38c 100644 --- a/include/rfl/internal/move_and_flatten_field_tuple.hpp +++ b/include/rfl/internal/move_and_flatten_field_tuple.hpp @@ -3,31 +3,32 @@ #include +#include "../Tuple.hpp" +#include "../tuple_cat.hpp" #include "is_flatten_field.hpp" #include "move_to_field_tuple.hpp" -#include "nth_tuple_element_t.hpp" namespace rfl { namespace internal { -template -auto move_and_flatten_field_tuple(FieldTuple&& _t, Args&&... _args) { - constexpr auto i = sizeof...(Args); - if constexpr (i == std::tuple_size_v>) { - return std::tuple_cat(std::move(_args)...); - } else { - using T = nth_tuple_element_t>; - if constexpr (is_flatten_field::value) { +template +auto move_and_flatten_field_tuple(FieldTuple&& _t) { + const auto get_one = [&](std::integral_constant) { + using T = tuple_element_t<_i, std::remove_cvref_t>; + if constexpr (is_flatten_field_v) { return move_and_flatten_field_tuple( - std::move(_t), std::move(_args)..., - move_and_flatten_field_tuple( - move_to_field_tuple(std::move(std::get(_t).value_)))); + move_to_field_tuple(std::move(rfl::get<_i>(_t).value_))); } else { - return move_and_flatten_field_tuple( - std::move(_t), std::move(_args)..., - std::make_tuple(std::move(std::get(_t)))); + return rfl::make_tuple(std::move(rfl::get<_i>(_t))); } + }; + + constexpr auto size = rfl::tuple_size_v>; + + return [&](std::integer_sequence) { + return rfl::tuple_cat(get_one(std::integral_constant{})...); } + (std::make_integer_sequence()); } } // namespace internal diff --git a/include/rfl/internal/move_field_tuple_to_named_tuple.hpp b/include/rfl/internal/move_field_tuple_to_named_tuple.hpp index 070e2659..cd4ff666 100644 --- a/include/rfl/internal/move_field_tuple_to_named_tuple.hpp +++ b/include/rfl/internal/move_field_tuple_to_named_tuple.hpp @@ -5,6 +5,7 @@ #include #include "../Field.hpp" +#include "../Tuple.hpp" #include "has_flatten_fields.hpp" #include "move_and_flatten_field_tuple.hpp" @@ -18,11 +19,11 @@ auto move_field_tuple_to_named_tuple(FieldTuple&& _field_tuple) { }; if constexpr (!has_flatten_fields>()) { - return std::apply(ft_to_nt, std::move(_field_tuple)); + return rfl::apply(ft_to_nt, std::move(_field_tuple)); } else { auto flattened_tuple = move_and_flatten_field_tuple(std::move(_field_tuple)); - return std::apply(ft_to_nt, std::move(flattened_tuple)); + return rfl::apply(ft_to_nt, std::move(flattened_tuple)); } } diff --git a/include/rfl/internal/move_from_named_tuple.hpp b/include/rfl/internal/move_from_named_tuple.hpp index 3b14824c..eca9d9e9 100644 --- a/include/rfl/internal/move_from_named_tuple.hpp +++ b/include/rfl/internal/move_from_named_tuple.hpp @@ -4,76 +4,64 @@ #include #include +#include "../Tuple.hpp" #include "../named_tuple_t.hpp" #include "is_field.hpp" #include "is_named_tuple.hpp" #include "nt_to_ptr_named_tuple.hpp" -#include "nth_tuple_element_t.hpp" #include "ptr_field_tuple_t.hpp" namespace rfl { namespace internal { -template -auto make_ptr_fields(PtrNamedTupleType& _n, Args... _args) { - constexpr auto i = sizeof...(Args); - - constexpr auto size = - std::tuple_size_v>; - - if constexpr (i == size) { - return std::make_tuple(_args...); - } else { - using Field = - std::remove_cvref_t>; +template +auto make_ptr_fields(PtrNamedTupleType& _n) { + const auto get_one = [&](std::integral_constant) { + using Field = std::remove_cvref_t>; using T = std::remove_cvref_t>; - if constexpr (is_named_tuple_v) { using SubPtrNamedTupleType = typename std::invoke_result), T>::type; - - return make_ptr_fields( - _n, _args..., SubPtrNamedTupleType(_n).fields()); - + return SubPtrNamedTupleType(_n).fields(); } else if constexpr (is_flatten_field::value) { using SubPtrFieldTupleType = std::remove_cvref_t>; - - return make_ptr_fields( - _n, _args..., make_ptr_fields(_n)); - + return make_ptr_fields(_n); } else { - return make_ptr_fields( - _n, _args..., _n.template get_field()); + return _n.template get_field(); } + }; + + constexpr auto size = + rfl::tuple_size_v>; + + return [&](std::integer_sequence) { + return rfl::make_tuple(get_one(std::integral_constant{})...); } + (std::make_integer_sequence()); } -template -auto move_from_ptr_fields(Pointers& _ptrs, Args&&... _args) { - constexpr auto i = sizeof...(Args); - if constexpr (i == std::tuple_size_v>) { - return T{std::move(_args)...}; - } else { - using FieldType = nth_tuple_element_t>; - +template +auto move_from_ptr_fields(Pointers& _ptrs) { + const auto get_one = [&](std::integral_constant) { + using FieldType = tuple_element_t<_i, std::remove_cvref_t>; if constexpr (is_field_v) { - return move_from_ptr_fields( - _ptrs, std::move(_args)..., - rfl::make_field( - std::move(*std::get(_ptrs).value()))); - + return rfl::make_field( + std::move(*rfl::get<_i>(_ptrs).value())); } else { using PtrFieldTupleType = std::remove_cvref_t>; - using U = std::remove_cvref_t::Type>>; - - return move_from_ptr_fields( - _ptrs, std::move(_args)..., - move_from_ptr_fields(std::get(_ptrs))); + typename tuple_element_t<_i, PtrFieldTupleType>::Type>>; + return move_from_ptr_fields(rfl::get<_i>(_ptrs)); } + }; + + constexpr auto size = rfl::tuple_size_v>; + + return [&](std::integer_sequence) { + return T{std::move(get_one(std::integral_constant{}))...}; } + (std::make_integer_sequence()); } /// Creates a struct of type T from a named tuple by moving the underlying diff --git a/include/rfl/internal/move_from_tuple.hpp b/include/rfl/internal/move_from_tuple.hpp index 5d288cd6..1eebc857 100644 --- a/include/rfl/internal/move_from_tuple.hpp +++ b/include/rfl/internal/move_from_tuple.hpp @@ -4,6 +4,8 @@ #include #include +#include "../Tuple.hpp" +#include "../tuple_cat.hpp" #include "Array.hpp" #include "is_flatten_field.hpp" #include "is_named_tuple.hpp" @@ -16,10 +18,10 @@ namespace internal { template constexpr int calc_flattened_size() { - if constexpr (_i == std::tuple_size_v) { + if constexpr (_i == rfl::tuple_size_v) { return 0; } else { - using T = std::remove_pointer_t>; + using T = std::remove_pointer_t>; if constexpr (is_flatten_field_v) { return calc_flattened_size>() + calc_flattened_size(); @@ -29,17 +31,18 @@ constexpr int calc_flattened_size() { } } +// TODO: Non-recursive implementation template auto unflatten_ptr_tuple(PtrTupleType& _t, Args... _args) { constexpr auto i = sizeof...(Args); - constexpr auto size = std::tuple_size_v>; + constexpr auto size = rfl::tuple_size_v>; if constexpr (i == size) { - return std::make_tuple(_args...); + return rfl::make_tuple(_args...); } else { using T = std::remove_cvref_t< - std::remove_pointer_t>>; + std::remove_pointer_t>>; if constexpr (is_flatten_field_v) { using SubTargetTupleType = @@ -62,10 +65,10 @@ auto unflatten_ptr_tuple(PtrTupleType& _t, Args... _args) { template auto move_from_pointers(Pointers& _ptrs, Args&&... _args) { constexpr auto i = sizeof...(Args); - if constexpr (i == std::tuple_size_v>) { + if constexpr (i == rfl::tuple_size_v>) { return std::remove_cvref_t{std::move(_args)...}; } else { - using FieldType = nth_tuple_element_t>; + using FieldType = tuple_element_t>; if constexpr (std::is_pointer_v) { return move_from_pointers(_ptrs, std::move(_args)..., @@ -75,7 +78,7 @@ auto move_from_pointers(Pointers& _ptrs, Args&&... _args) { using PtrTupleType = ptr_tuple_t>; using U = std::remove_cvref_t>::Type>; + tuple_element_t>::Type>; return move_from_pointers(_ptrs, std::move(_args)..., move_from_pointers(std::get(_ptrs))); @@ -85,20 +88,20 @@ auto move_from_pointers(Pointers& _ptrs, Args&&... _args) { template auto flatten_array(T* _v) { - return std::make_tuple(_v); + return rfl::make_tuple(_v); } template auto flatten_array(std::array* _arr) { const auto fct = [](auto&... _v) { - return std::tuple_cat(flatten_array(&_v)...); + return rfl::tuple_cat(flatten_array(&_v)...); }; - return std::apply(fct, *_arr); + return rfl::apply(fct, *_arr); } template auto make_tuple_from_element(T _v) { - return std::make_tuple(_v); + return rfl::make_tuple(_v); } template @@ -108,9 +111,9 @@ auto make_tuple_from_element(Array* _arr) { auto flatten_c_arrays(const auto& _tup) { const auto fct = [](auto... _v) { - return std::tuple_cat(make_tuple_from_element(_v)...); + return rfl::tuple_cat(make_tuple_from_element(_v)...); }; - return std::apply(fct, _tup); + return rfl::apply(fct, _tup); } /// Creates a struct of type T from a tuple by moving the underlying diff --git a/include/rfl/internal/move_to_field_tuple.hpp b/include/rfl/internal/move_to_field_tuple.hpp index cf860ce1..187436b7 100644 --- a/include/rfl/internal/move_to_field_tuple.hpp +++ b/include/rfl/internal/move_to_field_tuple.hpp @@ -4,6 +4,7 @@ #include #include +#include "../Tuple.hpp" #include "../field_names_t.hpp" #include "Array.hpp" #include "bind_to_tuple.hpp" @@ -21,17 +22,17 @@ auto move_to_field_tuple(OriginalStruct&& _t) { if constexpr (is_named_tuple_v) { return _t.fields(); } else if constexpr (has_fields()) { - return bind_to_tuple(_t, [](auto& x) { return std::move(x); }); + return bind_to_tuple(_t, [](auto _ptr) { return std::move(*_ptr); }); } else if constexpr (is_empty()) { - return std::tuple(); + return rfl::Tuple(); } else { using FieldNames = field_names_t; - const auto fct = [](T& _v) { + const auto fct = [](T* _ptr) { using Type = std::remove_cvref_t; if constexpr (std::is_array_v) { - return Array(_v); + return Array(*_ptr); } else { - return std::move(_v); + return std::move(*_ptr); } }; auto tup = bind_to_tuple(_t, fct); diff --git a/include/rfl/internal/no_duplicate_field_names.hpp b/include/rfl/internal/no_duplicate_field_names.hpp index a24c8cb3..4e3f90dc 100644 --- a/include/rfl/internal/no_duplicate_field_names.hpp +++ b/include/rfl/internal/no_duplicate_field_names.hpp @@ -1,9 +1,7 @@ #ifndef RFL_INTERNAL_NO_DUPLICATE_FIELD_NAMES_HPP_ #define RFL_INTERNAL_NO_DUPLICATE_FIELD_NAMES_HPP_ -#include - -#include "nth_tuple_element_t.hpp" +#include "../Tuple.hpp" namespace rfl { namespace internal { @@ -13,8 +11,8 @@ namespace no_duplicate_field_names_helpers { template constexpr inline void compare_two_fields() { if constexpr (_j < _i) { - using FieldType1 = std::remove_cvref_t>; - using FieldType2 = std::remove_cvref_t>; + using FieldType1 = std::remove_cvref_t>; + using FieldType2 = std::remove_cvref_t>; constexpr auto field_name_i = FieldType1::name_; constexpr auto field_name_j = FieldType2::name_; @@ -40,7 +38,7 @@ constexpr inline void iterate_over_i(std::integer_sequence) { template constexpr inline bool no_duplicate_field_names() { - constexpr auto num_fields = std::tuple_size_v; + constexpr auto num_fields = rfl::tuple_size_v; if constexpr (num_fields <= 1) { return true; diff --git a/include/rfl/internal/nt_to_ptr_named_tuple.hpp b/include/rfl/internal/nt_to_ptr_named_tuple.hpp index 51dff6ae..bb9f7f1e 100644 --- a/include/rfl/internal/nt_to_ptr_named_tuple.hpp +++ b/include/rfl/internal/nt_to_ptr_named_tuple.hpp @@ -4,49 +4,50 @@ #include #include "../Field.hpp" +#include "../Tuple.hpp" #include "../make_named_tuple.hpp" -#include "nth_tuple_element_t.hpp" namespace rfl { namespace internal { /// Generates a named tuple that contains pointers to the original values in /// the struct from a named tuple. -template -auto nt_to_ptr_named_tuple(NamedTupleType& _nt, AlreadyExtracted... _a) { +template +auto nt_to_ptr_named_tuple(NamedTupleType& _nt) { using Fields = typename NamedTupleType::Fields; - constexpr auto i = sizeof...(AlreadyExtracted); - constexpr auto num_fields = std::tuple_size_v; - - if constexpr (i == num_fields) { - return make_named_tuple(_a...); - } else { - using FieldType = nth_tuple_element_t; + const auto get_one = [&](std::integral_constant) { + using FieldType = tuple_element_t<_i, Fields>; using T = std::remove_cvref_t; - return nt_to_ptr_named_tuple( - _nt, _a..., Field(&std::get(_nt.values()))); + return Field(&rfl::get<_i>(_nt.values())); + }; + + constexpr auto num_fields = rfl::tuple_size_v; + + return [&](std::integer_sequence) { + return make_named_tuple(get_one(std::integral_constant{})...); } + (std::make_integer_sequence()); } /// Generates a named tuple that contains pointers to the original values in /// the struct from a named tuple. -template -auto nt_to_ptr_named_tuple(const NamedTupleType& _nt, AlreadyExtracted... _a) { +template +auto nt_to_ptr_named_tuple(const NamedTupleType& _nt) { using Fields = typename NamedTupleType::Fields; - constexpr auto i = sizeof...(AlreadyExtracted); - constexpr auto num_fields = std::tuple_size_v; - - if constexpr (i == num_fields) { - return make_named_tuple(_a...); - } else { - using FieldType = nth_tuple_element_t; + const auto get_one = [&](std::integral_constant) { + using FieldType = tuple_element_t<_i, Fields>; using T = std::remove_cvref_t; - return nt_to_ptr_named_tuple( - _nt, _a..., - Field(&std::get(_nt.values()))); + return Field(&rfl::get<_i>(_nt.values())); + }; + + constexpr auto num_fields = rfl::tuple_size_v; + + return [&](std::integer_sequence) { + return make_named_tuple(get_one(std::integral_constant{})...); } + (std::make_integer_sequence()); } } // namespace internal diff --git a/include/rfl/internal/nth_element.hpp b/include/rfl/internal/nth_element.hpp new file mode 100644 index 00000000..ff32a6c5 --- /dev/null +++ b/include/rfl/internal/nth_element.hpp @@ -0,0 +1,44 @@ +#ifndef RFL_INTERNAL_NTH_ELEMENT_HPP_ +#define RFL_INTERNAL_NTH_ELEMENT_HPP_ + +#include +#include + +namespace rfl::internal { + +template +struct ElementWrapper { + T t_; +}; + +template +constexpr auto operator|(const ElementWrapper& _e1, + const ElementWrapper& _e2) { + if constexpr (_i1 == N) { + return _e1; + } else { + return _e2; + } +} + +template +constexpr auto find_matching_element(const Head& _head, const Tail&... _tail) { + return (_head | ... | _tail); +}; + +template +constexpr auto wrap_elements(Ts... _ts, std::integer_sequence) { + return find_matching_element(ElementWrapper{_ts}...).t_; +} + +template +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( + _ts..., std::make_integer_sequence()); +} + +} // namespace rfl::internal + +#endif diff --git a/include/rfl/internal/nth_element_t.hpp b/include/rfl/internal/nth_element_t.hpp index dafcd120..793657b1 100644 --- a/include/rfl/internal/nth_element_t.hpp +++ b/include/rfl/internal/nth_element_t.hpp @@ -1,7 +1,9 @@ #ifndef RFL_INTERNAL_NTH_ELEMENT_T_HPP_ #define RFL_INTERNAL_NTH_ELEMENT_T_HPP_ -#include +#include + +#include "nth_element.hpp" namespace rfl::internal { @@ -12,59 +14,15 @@ using nth_element_t = __type_pack_element; #else -template -struct NthElement; - -template -struct NthElement { - using Type = typename NthElement::Type; -}; - -template -struct NthElement<0, T0, Ts...> { - using Type = T0; -}; - -template -struct NthElement<1, T0, T1, Ts...> { - using Type = T1; -}; - -template -struct NthElement<2, T0, T1, T2, Ts...> { - using Type = T2; -}; - -template -struct NthElement<3, T0, T1, T2, T3, Ts...> { - using Type = T3; -}; - -template -struct NthElement<4, T0, T1, T2, T3, T4, Ts...> { - using Type = T4; -}; - -template -struct NthElement<5, T0, T1, T2, T3, T4, T5, Ts...> { - using Type = T5; -}; - -template -struct NthElement<6, T0, T1, T2, T3, T4, T5, T6, Ts...> { - using Type = T6; -}; - -template -struct NthElement<7, T0, T1, T2, T3, T4, T5, T6, T7, Ts...> { - using Type = T7; +template +struct TypeWrapper { + using Type = T; }; template -using nth_element_t = typename NthElement::Type; +using nth_element_t = + typename std::invoke_result_t...>), + TypeWrapper...>::Type; #endif diff --git a/include/rfl/internal/nth_tuple_element_t.hpp b/include/rfl/internal/nth_tuple_element_t.hpp deleted file mode 100644 index 913c8455..00000000 --- a/include/rfl/internal/nth_tuple_element_t.hpp +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef RFL_INTERNAL_NTH_TUPLE_ELEMENT_T_HPP_ -#define RFL_INTERNAL_NTH_TUPLE_ELEMENT_T_HPP_ - -#include - -#include "nth_element_t.hpp" - -namespace rfl::internal { - -template -struct nth_tuple_element; - -template -struct nth_tuple_element> { - using Type = nth_element_t; -}; - -template -using nth_tuple_element_t = typename nth_tuple_element::Type; - -} // namespace rfl::internal - -#endif diff --git a/include/rfl/internal/remove_fields.hpp b/include/rfl/internal/remove_fields.hpp index efa6d011..c411507b 100644 --- a/include/rfl/internal/remove_fields.hpp +++ b/include/rfl/internal/remove_fields.hpp @@ -6,9 +6,9 @@ #include #include "../NamedTuple.hpp" +#include "../Tuple.hpp" #include "../define_named_tuple.hpp" #include "StringLiteral.hpp" -#include "nth_tuple_element_t.hpp" namespace rfl { namespace internal { @@ -33,10 +33,10 @@ struct remove_single_field { using OldNamedTupleType = std::remove_cvref_t<_OldNamedTupleType>; constexpr static int num_fields = - std::tuple_size_v; + rfl::tuple_size_v; using FieldType = std::remove_cvref_t< - nth_tuple_element_t>; + tuple_element_t>; using NewNamedTupleType = std::conditional_t<_name == FieldType::name_, _NewNamedTupleType, @@ -57,7 +57,7 @@ struct remove_fields<_NamedTupleType, _head> { using NamedTupleType = std::remove_cvref_t<_NamedTupleType>; constexpr static int num_fields = - std::tuple_size_v; + rfl::tuple_size_v; using type = typename remove_single_field, num_fields>::type; @@ -69,7 +69,7 @@ struct remove_fields { using NamedTupleType = std::remove_cvref_t<_NamedTupleType>; constexpr static int num_fields = - std::tuple_size_v; + rfl::tuple_size_v; using NewNamedTupleType = typename remove_single_field, diff --git a/include/rfl/internal/remove_ptrs_tup.hpp b/include/rfl/internal/remove_ptrs_tup.hpp index 899e12f4..d81f14d6 100644 --- a/include/rfl/internal/remove_ptrs_tup.hpp +++ b/include/rfl/internal/remove_ptrs_tup.hpp @@ -2,11 +2,11 @@ #define RFL_INTERNAL_REMOVE_PTRS_TUP_HPP_ #include -#include #include -#include "ptr_tuple_t.hpp" +#include "../Tuple.hpp" #include "../to_named_tuple.hpp" +#include "ptr_tuple_t.hpp" namespace rfl { namespace internal { @@ -15,9 +15,9 @@ template struct remove_ptrs_tup; template -struct remove_ptrs_tup> { +struct remove_ptrs_tup> { using TupleType = - std::tuple>...>; + rfl::Tuple>...>; }; } // namespace internal diff --git a/include/rfl/internal/to_flattened_ptr_tuple.hpp b/include/rfl/internal/to_flattened_ptr_tuple.hpp index a003448b..ff53a635 100644 --- a/include/rfl/internal/to_flattened_ptr_tuple.hpp +++ b/include/rfl/internal/to_flattened_ptr_tuple.hpp @@ -3,32 +3,35 @@ #include +#include "../Tuple.hpp" +#include "../tuple_cat.hpp" #include "has_flatten_fields.hpp" #include "is_flatten_field.hpp" -#include "nth_tuple_element_t.hpp" #include "to_ptr_tuple.hpp" namespace rfl { namespace internal { -template -auto flatten_ptr_tuple(PtrTuple&& _t, Args... _args) { - constexpr auto i = sizeof...(Args); - if constexpr (i == 0 && !has_flatten_fields()) { +template +auto flatten_ptr_tuple(PtrTuple&& _t) { + if constexpr (0 && !has_flatten_fields()) { return std::forward(_t); - } else if constexpr (i == std::tuple_size_v>) { - return std::tuple_cat(std::forward(_args)...); } else { - using T = nth_tuple_element_t>; - if constexpr (is_flatten_field_v) { - return flatten_ptr_tuple( - std::forward(_t), std::forward(_args)..., - flatten_ptr_tuple(to_ptr_tuple(std::get(_t)->get()))); - } else { - return flatten_ptr_tuple(std::forward(_t), - std::forward(_args)..., - std::make_tuple(std::get(_t))); + const auto get_one = [&](std::integral_constant) { + using T = tuple_element_t<_i, std::remove_cvref_t>; + if constexpr (is_flatten_field_v) { + return flatten_ptr_tuple(to_ptr_tuple(std::get<_i>(_t)->get())); + } else { + return rfl::make_tuple(std::get<_i>(_t)); + } + }; + + constexpr auto size = rfl::tuple_size_v>; + + return [&](std::integer_sequence) { + return rfl::tuple_cat(get_one(std::integral_constant{})...); } + (std::make_integer_sequence()); } } diff --git a/include/rfl/internal/to_ptr_field_tuple.hpp b/include/rfl/internal/to_ptr_field_tuple.hpp index 42d5c4b4..e7877130 100644 --- a/include/rfl/internal/to_ptr_field_tuple.hpp +++ b/include/rfl/internal/to_ptr_field_tuple.hpp @@ -24,12 +24,13 @@ auto to_ptr_field_tuple(T& _t) { } else if constexpr (is_named_tuple_v) { return nt_to_ptr_named_tuple(_t).fields(); } else if constexpr (has_fields()) { - return bind_to_tuple(_t, [](auto& x) { return to_ptr_field(x); }); + return bind_to_tuple(_t, [](auto* _ptr) { return to_ptr_field(*_ptr); }); } else if constexpr (is_empty()) { - return std::tuple(); + return rfl::Tuple(); } else { using FieldNames = field_names_t; - auto tup = bind_to_tuple(_t, [](auto& x) { return to_ptr_field(x); }); + auto tup = + bind_to_tuple(_t, [](auto* _ptr) { return to_ptr_field(*_ptr); }); return wrap_in_fields(std::move(tup)); } } diff --git a/include/rfl/internal/to_ptr_named_tuple.hpp b/include/rfl/internal/to_ptr_named_tuple.hpp index 1286f8d1..87a35bb2 100644 --- a/include/rfl/internal/to_ptr_named_tuple.hpp +++ b/include/rfl/internal/to_ptr_named_tuple.hpp @@ -4,6 +4,7 @@ #include #include +#include "../Tuple.hpp" #include "../always_false.hpp" #include "../field_names_t.hpp" #include "../make_named_tuple.hpp" @@ -13,29 +14,30 @@ #include "is_empty.hpp" #include "is_field.hpp" #include "is_named_tuple.hpp" -#include "nth_tuple_element_t.hpp" #include "to_flattened_ptr_tuple.hpp" #include "to_ptr_field_tuple.hpp" namespace rfl { namespace internal { -template -auto flatten_ptr_field_tuple(PtrFieldTuple& _t, Args&&... _args) { - constexpr auto i = sizeof...(Args); - if constexpr (i == std::tuple_size_v>) { - return std::tuple_cat(std::forward(_args)...); - } else { - using T = nth_tuple_element_t>; +template +auto flatten_ptr_field_tuple(PtrFieldTuple& _t) { + const auto get_one = [&](std::integral_constant) { + using T = tuple_element_t<_i, std::remove_cvref_t>; if constexpr (internal::is_flatten_field::value) { - auto subtuple = internal::to_ptr_field_tuple(*std::get(_t).get()); - return flatten_ptr_field_tuple(_t, std::forward(_args)..., - flatten_ptr_field_tuple(subtuple)); + auto subtuple = internal::to_ptr_field_tuple(*rfl::get<_i>(_t).get()); + return flatten_ptr_field_tuple(subtuple); } else { - return flatten_ptr_field_tuple(_t, std::forward(_args)..., - std::make_tuple(std::get(_t))); + return rfl::make_tuple(rfl::get<_i>(_t)); } + }; + + constexpr auto size = rfl::tuple_size_v>; + + return [&](std::integer_sequence) { + return rfl::tuple_cat(get_one(std::integral_constant{})...); } + (std::make_integer_sequence()); } template @@ -45,10 +47,10 @@ auto field_tuple_to_named_tuple(PtrFieldTuple& _ptr_field_tuple) { }; if constexpr (!has_flatten_fields>()) { - return std::apply(ft_to_nt, std::move(_ptr_field_tuple)); + return rfl::apply(ft_to_nt, std::move(_ptr_field_tuple)); } else { const auto flattened_tuple = flatten_ptr_field_tuple(_ptr_field_tuple); - return std::apply(ft_to_nt, flattened_tuple); + return rfl::apply(ft_to_nt, flattened_tuple); } } diff --git a/include/rfl/internal/to_ptr_tuple.hpp b/include/rfl/internal/to_ptr_tuple.hpp index c3507887..d5c95088 100644 --- a/include/rfl/internal/to_ptr_tuple.hpp +++ b/include/rfl/internal/to_ptr_tuple.hpp @@ -11,11 +11,11 @@ namespace rfl { namespace internal { template -constexpr auto to_ptr_tuple(T& _t) { +auto to_ptr_tuple(T& _t) { if constexpr (std::is_pointer_v>) { return to_ptr_tuple(*_t); } else { - return bind_to_tuple(_t, [](auto& x) { return &x; }); + return bind_to_tuple(_t, [](auto* _ptr) { return _ptr; }); } } diff --git a/include/rfl/internal/tup_to_ptr_tuple.hpp b/include/rfl/internal/tup_to_ptr_tuple.hpp index aaf5cdd2..063e3d93 100644 --- a/include/rfl/internal/tup_to_ptr_tuple.hpp +++ b/include/rfl/internal/tup_to_ptr_tuple.hpp @@ -4,6 +4,8 @@ #include #include "../Field.hpp" +#include "../Tuple.hpp" +#include "../apply.hpp" #include "../make_named_tuple.hpp" namespace rfl { @@ -11,16 +13,12 @@ namespace internal { /// Generates a named tuple that contains pointers to the original values in /// the struct from a tuple. -template -auto tup_to_ptr_tuple(TupleType& _t, AlreadyExtracted... _a) { - constexpr auto i = sizeof...(AlreadyExtracted); - constexpr auto size = std::tuple_size_v; - - if constexpr (i == size) { - return std::make_tuple(_a...); - } else { - return tup_to_ptr_tuple(_t, _a..., &std::get(_t)); - } +template +auto tup_to_ptr_tuple(TupleType& _t) { + const auto to_ptr = [](auto&... _fields) { + return rfl::make_tuple(&_fields...); + }; + return rfl::apply(to_ptr, _t); } } // namespace internal diff --git a/include/rfl/internal/tuple/accumulate_sizes.hpp b/include/rfl/internal/tuple/accumulate_sizes.hpp new file mode 100644 index 00000000..defb5a83 --- /dev/null +++ b/include/rfl/internal/tuple/accumulate_sizes.hpp @@ -0,0 +1,39 @@ +#ifndef RFL_INTERNAL_TUPLE_ACCUMULATE_SIZES_HPP_ +#define RFL_INTERNAL_TUPLE_ACCUMULATE_SIZES_HPP_ + +#include +#include + +namespace rfl::internal::tuple { + +template +struct SizeWrapper {}; + +template +struct Sizes { + static consteval auto to_array() { + return std::array({_is..., _last}); + } +}; + +template +consteval auto operator+(const Sizes<_last, _is...>& _sizes, + const SizeWrapper& _w) { + if constexpr (_last % alignof(T) == 0) { + constexpr auto last_new = _last + sizeof(T); + return Sizes{}; + } else { + constexpr auto last_corrected = _last + alignof(T) - (_last % alignof(T)); + constexpr auto last_new = last_corrected + sizeof(T); + return Sizes{}; + } +} + +template +consteval auto accumulate_sizes() { + return (Sizes<0>{} + ... + SizeWrapper{}).to_array(); +} + +} // namespace rfl::internal::tuple + +#endif diff --git a/include/rfl/internal/tuple/apply.hpp b/include/rfl/internal/tuple/apply.hpp new file mode 100644 index 00000000..18e38eec --- /dev/null +++ b/include/rfl/internal/tuple/apply.hpp @@ -0,0 +1,30 @@ +#ifndef RFL_INTERNAL_TUPLE_APPLY_HPP_ +#define RFL_INTERNAL_TUPLE_APPLY_HPP_ + +#include + +#include "../../Tuple.hpp" + +namespace rfl::internal::tuple { + +template +auto apply(F&& _f, const rfl::Tuple& _tup, + std::integer_sequence) { + return _f(rfl::get<_is>(_tup)...); +} + +template +auto apply(F&& _f, rfl::Tuple& _tup, + std::integer_sequence) { + return _f(rfl::get<_is>(_tup)...); +} + +template +auto apply(F&& _f, rfl::Tuple&& _tup, + std::integer_sequence) { + return _f(std::move(rfl::get<_is>(_tup))...); +} + +} // namespace rfl::internal::tuple + +#endif diff --git a/include/rfl/internal/tuple/calculate_positions.hpp b/include/rfl/internal/tuple/calculate_positions.hpp new file mode 100644 index 00000000..1b884336 --- /dev/null +++ b/include/rfl/internal/tuple/calculate_positions.hpp @@ -0,0 +1,39 @@ +#ifndef RFL_INTERNAL_TUPLE_CALCULATE_POSITIONS_HPP_ +#define RFL_INTERNAL_TUPLE_CALCULATE_POSITIONS_HPP_ + +#include +#include + +namespace rfl::internal::tuple { + +template +struct PositionWrapper {}; + +template +struct Positions { + static consteval auto to_array() { + return std::array({_is..., _last}); + } +}; + +template +consteval auto operator+(const Positions<_last, _is...>& _sizes, + const PositionWrapper& _w) { + if constexpr (_last % alignof(T) == 0) { + constexpr auto last_new = _last + sizeof(T); + return Positions{}; + } else { + constexpr auto last_corrected = _last + alignof(T) - (_last % alignof(T)); + constexpr auto last_new = last_corrected + sizeof(T); + return Positions{}; + } +} + +template +consteval auto calculate_positions() { + return (Positions<0>{} + ... + PositionWrapper{}).to_array(); +} + +} // namespace rfl::internal::tuple + +#endif diff --git a/include/rfl/internal/tuple/concat.hpp b/include/rfl/internal/tuple/concat.hpp new file mode 100644 index 00000000..7d441ad3 --- /dev/null +++ b/include/rfl/internal/tuple/concat.hpp @@ -0,0 +1,55 @@ +#ifndef RFL_INTERNAL_TUPLE_CONCAT_HPP_ +#define RFL_INTERNAL_TUPLE_CONCAT_HPP_ + +#include + +#include "../../Tuple.hpp" + +namespace rfl::internal::tuple { + +template +struct TupleWrapper { + rfl::Tuple tuple_; +}; + +template +auto wrap_tuple(const rfl::Tuple& _tuple) { + return TupleWrapper{_tuple}; +} + +template +auto wrap_tuple(rfl::Tuple&& _tuple) { + return TupleWrapper{std::move(_tuple)}; +} + +template +auto concat_two(rfl::Tuple&& _t1, rfl::Tuple&& _t2, + std::integer_sequence, + std::integer_sequence) { + return rfl::Tuple(std::move(rfl::get<_is>(_t1))..., + std::move(rfl::get<_js>(_t2))...); +} + +template +auto operator+(TupleWrapper&& _t1, TupleWrapper&& _t2) { + return TupleWrapper{ + .tuple_ = + concat_two(std::move(_t1.tuple_), std::move(_t2.tuple_), + std::make_integer_sequence(), + std::make_integer_sequence())}; +} + +template +auto concat(const Head& _head, const Tail&... _tail) { + return (wrap_tuple(_head) + ... + wrap_tuple(_tail)); +} + +template +auto concat(Head&& _head, Tail&&... _tail) { + return (wrap_tuple(std::move(_head)) + ... + wrap_tuple(std::move(_tail))) + .tuple_; +} + +} // namespace rfl::internal::tuple + +#endif diff --git a/include/rfl/internal/tuple/make_from_tuple.hpp b/include/rfl/internal/tuple/make_from_tuple.hpp new file mode 100644 index 00000000..93e362f1 --- /dev/null +++ b/include/rfl/internal/tuple/make_from_tuple.hpp @@ -0,0 +1,24 @@ +#ifndef RFL_INTERNAL_TUPLE_MAKE_FROM_TUPLE_HPP_ +#define RFL_INTERNAL_TUPLE_MAKE_FROM_TUPLE_HPP_ + +#include + +#include "../../Tuple.hpp" + +namespace rfl::internal::tuple { + +template +T make_from_tuple(const rfl::Tuple& _t1, + std::integer_sequence) { + return T{rfl::get<_is>(_t1)...}; +} + +template +T make_from_tuple(rfl::Tuple&& _t1, + std::integer_sequence) { + return T{std::move(rfl::get<_is>(_t1))...}; +} + +} // namespace rfl::internal::tuple + +#endif diff --git a/include/rfl/internal/wrap_in_fields.hpp b/include/rfl/internal/wrap_in_fields.hpp index 7eb0611c..ed9ae3ad 100644 --- a/include/rfl/internal/wrap_in_fields.hpp +++ b/include/rfl/internal/wrap_in_fields.hpp @@ -4,6 +4,7 @@ #include #include +#include "../Tuple.hpp" #include "flattened_ptr_tuple_t.hpp" #include "is_flatten_field.hpp" #include "lit_name.hpp" @@ -11,20 +12,21 @@ namespace rfl { namespace internal { +// TODO: Non-recursive implementation template auto wrap_in_fields(auto&& _tuple, Fields&&... _fields) { constexpr auto size = - std::tuple_size_v>; + rfl::tuple_size_v>; constexpr auto i = sizeof...(_fields); if constexpr (i == size) { - return std::make_tuple(std::move(_fields)...); + return rfl::make_tuple(std::move(_fields)...); } else { auto value = std::move(std::get(_tuple)); using Type = std::remove_cvref_t>; if constexpr (is_flatten_field_v) { // The problem here is that the FieldNames are already flattened, but this // is not, so we need to determine how many field names to skip. - constexpr auto n_skip = std::tuple_size_v< + constexpr auto n_skip = rfl::tuple_size_v< std::remove_cvref_t>>; return wrap_in_fields( std::move(_tuple), std::move(_fields)..., std::move(value)); @@ -41,4 +43,5 @@ auto wrap_in_fields(auto&& _tuple, Fields&&... _fields) { } // namespace internal } // namespace rfl + #endif diff --git a/include/rfl/make_from_tuple.hpp b/include/rfl/make_from_tuple.hpp new file mode 100644 index 00000000..f2872d47 --- /dev/null +++ b/include/rfl/make_from_tuple.hpp @@ -0,0 +1,25 @@ +#ifndef RFL_MAKE_FROM_TUPLE_HPP_ +#define RFL_MAKE_FROM_TUPLE_HPP_ + +#include + +#include "Tuple.hpp" +#include "internal/tuple/make_from_tuple.hpp" + +namespace rfl { + +template +auto make_from_tuple(const rfl::Tuple& _tup) { + return internal::tuple::make_from_tuple( + _tup, std::make_integer_sequence()); +} + +template +auto make_from_tuple(rfl::Tuple&& _tup) { + return internal::tuple::make_from_tuple( + std::move(_tup), std::make_integer_sequence()); +} + +} // namespace rfl + +#endif diff --git a/include/rfl/msgpack/Parser.hpp b/include/rfl/msgpack/Parser.hpp index 7caea76b..e7fdae3d 100644 --- a/include/rfl/msgpack/Parser.hpp +++ b/include/rfl/msgpack/Parser.hpp @@ -24,13 +24,24 @@ struct Parser, FieldTypes...> { }; +template +requires AreReaderAndWriter> +struct Parser, + ProcessorsType> + : public TupleParser> { +}; + template requires AreReaderAndWriter> struct Parser, ProcessorsType> : public TupleParser { + /*_all_required=*/true, ProcessorsType, + std::tuple> { }; } // namespace parsing diff --git a/include/rfl/parsing/CustomParser.hpp b/include/rfl/parsing/CustomParser.hpp index 07996fd3..200c0ff9 100644 --- a/include/rfl/parsing/CustomParser.hpp +++ b/include/rfl/parsing/CustomParser.hpp @@ -4,6 +4,7 @@ #include #include +#include "../Tuple.hpp" #include "../internal/has_to_class_method_v.hpp" #include "../internal/to_ptr_field_tuple.hpp" #include "Parser.hpp" @@ -25,7 +26,7 @@ struct CustomParser { const auto class_from_ptrs = [](auto&... _ptrs) { return OriginalClass(std::move(*_ptrs.value_)...); }; - return std::apply(class_from_ptrs, ptr_field_tuple); + return rfl::apply(class_from_ptrs, ptr_field_tuple); } } catch (std::exception& e) { return Error(e.what()); diff --git a/include/rfl/parsing/FieldVariantParser.hpp b/include/rfl/parsing/FieldVariantParser.hpp index 24272224..5008c65f 100644 --- a/include/rfl/parsing/FieldVariantParser.hpp +++ b/include/rfl/parsing/FieldVariantParser.hpp @@ -7,6 +7,7 @@ #include #include "../Result.hpp" +#include "../Tuple.hpp" #include "../Variant.hpp" #include "../always_false.hpp" #include "../visit.hpp" @@ -34,7 +35,7 @@ struct FieldVariantParser { static ResultType read(const R& _r, const InputVarType& _var) noexcept { static_assert( - internal::no_duplicate_field_names>(), + internal::no_duplicate_field_names>(), "Externally tagged variants cannot have duplicate field " "names."); @@ -62,7 +63,7 @@ struct FieldVariantParser { static void write(const W& _w, const rfl::Variant& _v, const P& _parent) noexcept { static_assert( - internal::no_duplicate_field_names>(), + internal::no_duplicate_field_names>(), "Externally tagged variants cannot have duplicate field " "names."); diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index 7d2e8ade..5eb177a7 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -16,6 +16,7 @@ #include "Parser_rename.hpp" #include "Parser_result.hpp" #include "Parser_rfl_array.hpp" +#include "Parser_rfl_tuple.hpp" #include "Parser_rfl_variant.hpp" #include "Parser_shared_ptr.hpp" #include "Parser_skip.hpp" diff --git a/include/rfl/parsing/Parser_rfl_tuple.hpp b/include/rfl/parsing/Parser_rfl_tuple.hpp new file mode 100644 index 00000000..d536cc40 --- /dev/null +++ b/include/rfl/parsing/Parser_rfl_tuple.hpp @@ -0,0 +1,22 @@ +#ifndef RFL_PARSING_PARSER_RFL_TUPLE_HPP_ +#define RFL_PARSING_PARSER_RFL_TUPLE_HPP_ + +#include "../Tuple.hpp" +#include "Parser_base.hpp" +#include "TupleParser.hpp" + +namespace rfl { +namespace parsing { + +template +requires AreReaderAndWriter> +struct Parser, ProcessorsType> + : public TupleParser> { +}; + +} // namespace parsing +} // namespace rfl + +#endif diff --git a/include/rfl/parsing/Parser_tuple.hpp b/include/rfl/parsing/Parser_tuple.hpp index fd7fdc79..b4b592b9 100644 --- a/include/rfl/parsing/Parser_tuple.hpp +++ b/include/rfl/parsing/Parser_tuple.hpp @@ -1,6 +1,8 @@ #ifndef RFL_PARSING_PARSER_TUPLE_HPP_ #define RFL_PARSING_PARSER_TUPLE_HPP_ +#include + #include "Parser_base.hpp" #include "TupleParser.hpp" @@ -12,7 +14,7 @@ requires AreReaderAndWriter> struct Parser, ProcessorsType> : public TupleParser { + ProcessorsType, std::tuple> { }; } // namespace parsing diff --git a/include/rfl/parsing/TupleParser.hpp b/include/rfl/parsing/TupleParser.hpp index 6c3c5c35..bcce1617 100644 --- a/include/rfl/parsing/TupleParser.hpp +++ b/include/rfl/parsing/TupleParser.hpp @@ -1,11 +1,13 @@ #ifndef RFL_PARSING_TUPLEPARSER_HPP_ #define RFL_PARSING_TUPLEPARSER_HPP_ +#include #include -#include #include +#include #include "../Result.hpp" +#include "../Tuple.hpp" #include "../always_false.hpp" #include "../internal/nth_element_t.hpp" #include "Parent.hpp" @@ -15,8 +17,8 @@ namespace rfl::parsing { template -requires AreReaderAndWriter> + class ProcessorsType, class TupleType> +requires AreReaderAndWriter struct TupleParser { public: using InputArrayType = typename R::InputArrayType; @@ -27,15 +29,14 @@ struct TupleParser { using ParentType = Parent; - static Result> read(const R& _r, - const InputVarType& _var) noexcept { - const auto parse = - [&](const InputArrayType& _arr) -> Result> { - alignas(std::tuple) unsigned char buf[sizeof(std::tuple)]; - auto ptr = reinterpret_cast*>(buf); + static Result read(const R& _r, + const InputVarType& _var) noexcept { + const auto parse = [&](const InputArrayType& _arr) -> Result { + alignas(TupleType) unsigned char buf[sizeof(TupleType)]; + auto ptr = reinterpret_cast(buf); const auto tuple_reader = - TupleReader, _ignore_empty_containers, - _all_required, ProcessorsType>(&_r, ptr); + TupleReader(&_r, ptr); auto err = _r.read_array(tuple_reader, _arr); if (err) { tuple_reader.call_destructors_where_necessary(); @@ -53,19 +54,21 @@ struct TupleParser { } template - static void write(const W& _w, const std::tuple& _tup, + static void write(const W& _w, const TupleType& _tup, const P& _parent) noexcept { - auto arr = ParentType::add_array(_w, sizeof...(Ts), _parent); + constexpr auto size = rfl::tuple_size_v; + auto arr = ParentType::add_array(_w, size, _parent); const auto new_parent = typename ParentType::Array{&arr}; - to_array<0>(_w, _tup, new_parent); + to_array(_w, _tup, new_parent, std::make_integer_sequence()); _w.end_array(&arr); } static schema::Type to_schema( std::map* _definitions) { std::vector types; - build_schema(_definitions, &types, - std::make_integer_sequence()); + build_schema( + _definitions, &types, + std::make_integer_sequence>()); return schema::Type{schema::Type::Tuple{.types_ = std::move(types)}}; } @@ -73,7 +76,7 @@ struct TupleParser { template static void add_to_schema(std::map* _definitions, std::vector* _types) noexcept { - using U = std::remove_cvref_t>>; + using U = std::remove_cvref_t>; _types->push_back(Parser::to_schema(_definitions)); } @@ -85,15 +88,18 @@ struct TupleParser { } template - static void to_array(const W& _w, const std::tuple& _tup, - const P& _parent) noexcept { - if constexpr (_i < sizeof...(Ts)) { - using NewFieldType = - std::remove_cvref_t>; - Parser::write(_w, std::get<_i>(_tup), - _parent); - to_array<_i + 1>(_w, _tup, _parent); - } + static void add_to_array(const W& _w, const TupleType& _tup, + const P& _parent) noexcept { + using NewFieldType = + std::remove_cvref_t>; + Parser::write(_w, rfl::get<_i>(_tup), + _parent); + } + + template + static void to_array(const W& _w, const TupleType& _tup, const P& _parent, + std::integer_sequence) noexcept { + (add_to_array<_is>(_w, _tup, _parent), ...); } }; diff --git a/include/rfl/parsing/TupleReader.hpp b/include/rfl/parsing/TupleReader.hpp index a94fe446..25d76bd6 100644 --- a/include/rfl/parsing/TupleReader.hpp +++ b/include/rfl/parsing/TupleReader.hpp @@ -7,8 +7,8 @@ #include #include "../Result.hpp" +#include "../Tuple.hpp" #include "../internal/is_array.hpp" -#include "../internal/nth_tuple_element_t.hpp" namespace rfl::parsing { @@ -17,7 +17,7 @@ template ; + static constexpr size_t size_ = rfl::tuple_size_v; public: TupleReader(const R* _r, TupleType* _tuple) : i_(0), r_(_r), tuple_(_tuple) {} @@ -47,13 +47,13 @@ class TupleReader { return; } using CurrentType = - std::remove_cvref_t>; + std::remove_cvref_t>; if constexpr (!std::is_array_v && std::is_pointer_v && std::is_destructible_v) { - std::get<_i>(*tuple_)->~CurrentType(); + rfl::get<_i>(*tuple_)->~CurrentType(); } else if constexpr (std::is_array_v) { - auto ptr = std::get<_i>(*tuple_); + auto ptr = rfl::get<_i>(*tuple_); call_destructor_on_array(sizeof(*ptr) / sizeof(**ptr), *ptr); } call_destructors_where_necessary<_i + 1>(); @@ -66,7 +66,7 @@ class TupleReader { if constexpr (_i < size_) { if (i_ == _i) { using CurrentType = - std::remove_cvref_t>; + std::remove_cvref_t>; if constexpr (_all_required || is_required()) { @@ -74,7 +74,7 @@ class TupleReader { " was required, but missing."); return; } else { - ::new (&(std::get<_i>(*tuple_))) CurrentType(); + ::new (&(rfl::get<_i>(*tuple_))) CurrentType(); ++i_; } } @@ -87,12 +87,12 @@ class TupleReader { if constexpr (_i < size_) { if (i_ == _i) { using CurrentType = - std::remove_cvref_t>; + std::remove_cvref_t>; auto res = Parser::read(*r_, _var); if (res) { - move_to(&(std::get<_i>(*tuple_)), &(*res)); + move_to(&(rfl::get<_i>(*tuple_)), &(*res)); ++i_; } else { *_err = Error("Failed to parse field " + std::to_string(_i) + ": " + diff --git a/include/rfl/parsing/ViewReader.hpp b/include/rfl/parsing/ViewReader.hpp index 46991bcb..e7a2f9d9 100644 --- a/include/rfl/parsing/ViewReader.hpp +++ b/include/rfl/parsing/ViewReader.hpp @@ -8,8 +8,8 @@ #include #include "../Result.hpp" +#include "../Tuple.hpp" #include "../internal/is_array.hpp" -#include "../internal/nth_tuple_element_t.hpp" namespace rfl::parsing { @@ -49,8 +49,7 @@ class ViewReader { const auto& _var, auto* _view, auto* _errors, auto* _found, auto* _set, bool* _already_assigned) { - using FieldType = - internal::nth_tuple_element_t; + using FieldType = tuple_element_t; using OriginalType = typename FieldType::Type; using T = std::remove_cvref_t>; @@ -134,8 +133,7 @@ class ViewReader { template void call_destructor_on_one_if_necessary() const { - using FieldType = - internal::nth_tuple_element_t<_i, typename ViewType::Fields>; + using FieldType = tuple_element_t<_i, typename ViewType::Fields>; using OriginalType = std::remove_cvref_t; using ValueType = std::remove_cvref_t>; diff --git a/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp b/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp index f19d08c9..1ae18316 100644 --- a/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp +++ b/include/rfl/parsing/ViewReaderWithStrippedFieldNames.hpp @@ -8,8 +8,8 @@ #include #include "../Result.hpp" +#include "../Tuple.hpp" #include "../internal/is_array.hpp" -#include "../internal/nth_tuple_element_t.hpp" namespace rfl::parsing { @@ -60,8 +60,7 @@ class ViewReaderWithStrippedFieldNames { static void assign_if_field_is_field_i(const R& _r, const auto& _var, auto* _view, auto* _errors, auto* _found, auto* _set, int _i) { - using FieldType = - internal::nth_tuple_element_t; + using FieldType = tuple_element_t; using OriginalType = typename FieldType::Type; using T = std::remove_cvref_t>; @@ -107,8 +106,7 @@ class ViewReaderWithStrippedFieldNames { // TODO: Unnecessary code duplication. template void call_destructor_on_one_if_necessary() const { - using FieldType = - internal::nth_tuple_element_t<_i, typename ViewType::Fields>; + using FieldType = tuple_element_t<_i, typename ViewType::Fields>; using OriginalType = std::remove_cvref_t; using ValueType = std::remove_cvref_t>; diff --git a/include/rfl/tuple_cat.hpp b/include/rfl/tuple_cat.hpp new file mode 100644 index 00000000..8c0ab2cd --- /dev/null +++ b/include/rfl/tuple_cat.hpp @@ -0,0 +1,23 @@ +#ifndef RFL_TUPLE_CAT_HPP_ +#define RFL_TUPLE_CAT_HPP_ + +#include "Tuple.hpp" +#include "internal/tuple/concat.hpp" + +namespace rfl { + +template +auto tuple_cat(const Head& _head, const Tail&... _tail) { + return internal::tuple::concat(_head, _tail...); +} + +template +auto tuple_cat(Head&& _head, Tail&&... _tail) { + return internal::tuple::concat(std::move(_head), std::move(_tail)...); +} + +inline auto tuple_cat() { return rfl::Tuple(); } + +} // namespace rfl + +#endif diff --git a/tests/json/test_deep_nested.cpp b/tests/json/test_deep_nested.cpp new file mode 100644 index 00000000..b455e9cd --- /dev/null +++ b/tests/json/test_deep_nested.cpp @@ -0,0 +1,78 @@ +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_deep_nested { + +class LowHeightClassSub { + public: + double dummy1 = 0; + double dummy2 = 0; + double dummy3 = 0; + double dummy4 = 0; + double dummy5 = 0; + double dummy6 = 0; + double dummy7 = 0; + double dummy8 = 0; + double dummy9 = 0; + double dummy10 = 0; + double dummy11 = 0; + double dummy12 = 0; + double dummy13 = 0; + double dummy14 = 0; + double dummy15 = 0; + double dummy16 = 0; + double dummy17 = 0; + double dummy18 = 0; + double dummy19 = 0; + double dummy20 = 0; + double dummy21 = 0; +}; + +class LowHeightClass { + public: + double dummy1 = 0; + double dummy2 = 0; + double dummy3 = 0; + + double dummy4 = 0; + double dummy5 = 0; + double dummy6 = 0; + + double dummy7 = 0; + double dummy8 = 0; + + double dummy9 = 0; + double dummy10 = 0; + + double dummy11 = 0; + double dummy12 = 0; + double dummy13 = 0; + double dummy14 = 0; + + LowHeightClassSub dummy15{}; + LowHeightClassSub dummy16{}; + LowHeightClassSub dummy17{}; + LowHeightClassSub dummy18{}; +}; + +class DeepClass { + public: + LowHeightClass LHC; + + int dummy1; + int dummy2; + double dummy3; +}; + +TEST(json, test_deep_nested) { + DeepClass dc{}; + + write_and_read( + dc, + R"({"LHC":{"dummy1":0.0,"dummy2":0.0,"dummy3":0.0,"dummy4":0.0,"dummy5":0.0,"dummy6":0.0,"dummy7":0.0,"dummy8":0.0,"dummy9":0.0,"dummy10":0.0,"dummy11":0.0,"dummy12":0.0,"dummy13":0.0,"dummy14":0.0,"dummy15":{"dummy1":0.0,"dummy2":0.0,"dummy3":0.0,"dummy4":0.0,"dummy5":0.0,"dummy6":0.0,"dummy7":0.0,"dummy8":0.0,"dummy9":0.0,"dummy10":0.0,"dummy11":0.0,"dummy12":0.0,"dummy13":0.0,"dummy14":0.0,"dummy15":0.0,"dummy16":0.0,"dummy17":0.0,"dummy18":0.0,"dummy19":0.0,"dummy20":0.0,"dummy21":0.0},"dummy16":{"dummy1":0.0,"dummy2":0.0,"dummy3":0.0,"dummy4":0.0,"dummy5":0.0,"dummy6":0.0,"dummy7":0.0,"dummy8":0.0,"dummy9":0.0,"dummy10":0.0,"dummy11":0.0,"dummy12":0.0,"dummy13":0.0,"dummy14":0.0,"dummy15":0.0,"dummy16":0.0,"dummy17":0.0,"dummy18":0.0,"dummy19":0.0,"dummy20":0.0,"dummy21":0.0},"dummy17":{"dummy1":0.0,"dummy2":0.0,"dummy3":0.0,"dummy4":0.0,"dummy5":0.0,"dummy6":0.0,"dummy7":0.0,"dummy8":0.0,"dummy9":0.0,"dummy10":0.0,"dummy11":0.0,"dummy12":0.0,"dummy13":0.0,"dummy14":0.0,"dummy15":0.0,"dummy16":0.0,"dummy17":0.0,"dummy18":0.0,"dummy19":0.0,"dummy20":0.0,"dummy21":0.0},"dummy18":{"dummy1":0.0,"dummy2":0.0,"dummy3":0.0,"dummy4":0.0,"dummy5":0.0,"dummy6":0.0,"dummy7":0.0,"dummy8":0.0,"dummy9":0.0,"dummy10":0.0,"dummy11":0.0,"dummy12":0.0,"dummy13":0.0,"dummy14":0.0,"dummy15":0.0,"dummy16":0.0,"dummy17":0.0,"dummy18":0.0,"dummy19":0.0,"dummy20":0.0,"dummy21":0.0}},"dummy1":0,"dummy2":0,"dummy3":0.0})"); +} + +} // namespace test_deep_nested diff --git a/tests/json/test_rfl_tuple.cpp b/tests/json/test_rfl_tuple.cpp new file mode 100644 index 00000000..d9d342fe --- /dev/null +++ b/tests/json/test_rfl_tuple.cpp @@ -0,0 +1,35 @@ + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_rfl_tuple { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children = nullptr; +}; + +TEST(json, test_rfl_tuple) { + auto bart = Person{.first_name = "Bart"}; + + auto lisa = Person{.first_name = "Lisa"}; + + auto maggie = Person{.first_name = "Maggie"}; + + const auto homer = + Person{.first_name = "Homer", + .children = std::make_unique>( + rfl::Tuple{ + std::move(bart), std::move(lisa), std::move(maggie)})}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +} +} // namespace test_rfl_tuple diff --git a/tests/json/test_tuple.cpp b/tests/json/test_tuple.cpp new file mode 100644 index 00000000..bc1216b4 --- /dev/null +++ b/tests/json/test_tuple.cpp @@ -0,0 +1,35 @@ + +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_tuple { + +struct Person { + rfl::Rename<"firstName", std::string> first_name; + rfl::Rename<"lastName", std::string> last_name = "Simpson"; + std::unique_ptr> children = nullptr; +}; + +TEST(json, test_tuple) { + auto bart = Person{.first_name = "Bart"}; + + auto lisa = Person{.first_name = "Lisa"}; + + auto maggie = Person{.first_name = "Maggie"}; + + const auto homer = + Person{.first_name = "Homer", + .children = std::make_unique>( + std::tuple{ + std::move(bart), std::move(lisa), std::move(maggie)})}; + + write_and_read( + homer, + R"({"firstName":"Homer","lastName":"Simpson","children":[{"firstName":"Bart","lastName":"Simpson"},{"firstName":"Lisa","lastName":"Simpson"},{"firstName":"Maggie","lastName":"Simpson"}]})"); +} +} // namespace test_tuple