diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index ee29b215..f38145df 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -56,7 +56,8 @@ class NamedTuple { /// Construct from a tuple containing fields. NamedTuple(std::tuple&& _tup) - : NamedTuple(std::make_from_tuple>(_tup)) { + : NamedTuple(std::make_from_tuple>( + std::forward>(_tup))) { static_assert(no_duplicate_field_names(), "Duplicate field names are not allowed"); } @@ -129,12 +130,10 @@ class NamedTuple { template auto add(std::tuple&& _tuple, Tail&&... _tail) { if constexpr (sizeof...(Tail) > 0) { - return add_tuple(std::forward>(_tuple), - std::make_index_sequence{}) - .replace(_tail...); + return add_tuple(std::forward>(_tuple)) + .add(std::forward(_tail)...); } else { - return add_tuple(std::forward>(_tuple), - std::make_index_sequence{}); + return add_tuple(std::forward>(_tuple)); } } @@ -143,12 +142,10 @@ class NamedTuple { template auto add(std::tuple&& _tuple, Tail&&... _tail) const { if constexpr (sizeof...(Tail) > 0) { - return add_tuple(std::forward>(_tuple), - std::make_index_sequence{}) - .replace(_tail...); + return add_tuple(std::forward>(_tuple)) + .add(std::forward(_tail)...); } else { - return add_tuple(std::forward>(_tuple), - std::make_index_sequence{}); + return add_tuple(std::forward>(_tuple)); } } @@ -157,12 +154,9 @@ class NamedTuple { template auto add(const std::tuple& _tuple, Tail&&... _tail) const { if constexpr (sizeof...(Tail) > 0) { - return add_tuple(_tuple, - std::make_index_sequence{}) - .replace(_tail...); + return add_tuple(_tuple).add(std::forward(_tail)...); } else { - return add_tuple(std::forward>(_tuple), - std::make_index_sequence{}); + return add_tuple(std::forward>(_tuple)); } } @@ -170,14 +164,18 @@ class NamedTuple { /// named tuples. template auto add(NamedTuple&& _named_tuple, Tail&&... _tail) { - return add(_named_tuple.fields(), std::forward(_tail)...); + return add( + std::forward>(_named_tuple.fields()), + std::forward(_tail)...); } /// Template specialization for NamedTuple, so we can pass fields from other /// named tuples. template auto add(NamedTuple&& _named_tuple, Tail&&... _tail) const { - return add(_named_tuple.fields(), std::forward(_tail)...); + return add( + std::forward>(_named_tuple.fields()), + std::forward(_tail)...); } /// Template specialization for NamedTuple, so we can pass fields from other @@ -346,17 +344,22 @@ class NamedTuple { 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(Tuple&& _tuple, const std::index_sequence) { - return add(std::get(_tuple)...); + template + constexpr auto add_tuple(std::tuple&& _tuple) { + const auto a = [this](auto&&... _fields) { + return add(std::forward(_fields)...); + }; + return std::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(Tuple&& _tuple, - const std::index_sequence) const { - return add(std::get(_tuple)...); + template + constexpr auto add_tuple(std::tuple&& _tuple) const { + const auto a = [this](auto&&... _fields) { + return add(std::forward(_fields)...); + }; + return std::apply(a, std::forward>(_tuple)); } /// Generates the fields. @@ -480,10 +483,13 @@ class NamedTuple { using FieldType = typename std::tuple_element::type; + using T = std::decay_t; + return retrieve_fields( std::forward>(_other_fields), std::forward(_args)..., - FieldType(std::get(_other_fields).value_)); + FieldType( + std::forward(std::get(_other_fields).value_))); } } diff --git a/tests/test_as.hpp b/tests/test_as.hpp new file mode 100644 index 00000000..a6f39395 --- /dev/null +++ b/tests/test_as.hpp @@ -0,0 +1,36 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_as() { + std::cout << "test_as" << std::endl; + + struct A { + rfl::Field<"f1", std::string> f1; + rfl::Field<"f2", rfl::Box> f2; + }; + + struct B { + rfl::Field<"f3", std::string> f3; + rfl::Field<"f4", rfl::Box> f4; + }; + + struct C { + rfl::Field<"f1", std::string> f1; + rfl::Field<"f2", rfl::Box> f2; + rfl::Field<"f4", rfl::Box> f4; + }; + + auto a = A{.f1 = "Hello", .f2 = rfl::make_box("World")}; + + auto b = B{.f3 = "Hello", .f4 = rfl::make_box("World")}; + + const auto c = rfl::as(std::move(a), std::move(b)); + + write_and_read(c, R"({"f1":"Hello","f2":"World","f4":"World"})"); +} diff --git a/tests/test_as_flatten.hpp b/tests/test_as_flatten.hpp new file mode 100644 index 00000000..a50c7f68 --- /dev/null +++ b/tests/test_as_flatten.hpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_as_flatten() { + std::cout << "test_as_flatten" << std::endl; + + struct A { + rfl::Field<"f1", std::string> f1; + rfl::Field<"f2", rfl::Box> f2; + }; + + struct B { + rfl::Field<"f3", std::string> f3; + rfl::Field<"f4", rfl::Box> f4; + }; + + struct C { + rfl::Flatten a; + rfl::Flatten b; + rfl::Field<"f5", int> f5; + }; + + auto a = A{.f1 = "Hello", .f2 = rfl::make_box("World")}; + + auto b = B{.f3 = "Hello", .f4 = rfl::make_box("World")}; + + const auto c = + rfl::as(std::move(a), std::move(b), rfl::make_field<"f5">(5)); + + write_and_read( + c, R"({"f1":"Hello","f2":"World","f3":"Hello","f4":"World","f5":5})"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index dae6b359..05227669 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -1,4 +1,6 @@ #include "test_array.hpp" +#include "test_as.hpp" +#include "test_as_flatten.hpp" #include "test_box.hpp" #include "test_custom_class1.hpp" #include "test_custom_class2.hpp" @@ -42,6 +44,8 @@ int main() { test_replace(); test_replace_flatten(); + test_as(); + test_as_flatten(); return 0; }