diff --git a/include/rfl/NamedTuple.hpp b/include/rfl/NamedTuple.hpp index f38145df..14f7f811 100644 --- a/include/rfl/NamedTuple.hpp +++ b/include/rfl/NamedTuple.hpp @@ -289,42 +289,38 @@ class NamedTuple { /// Template specialization for std::tuple, so we can pass fields from other /// named tuples. template - auto replace(std::tuple&& _tuple, Tail&&... _tail) const { + auto replace(std::tuple&& _tuple, Tail&&... _tail) { if constexpr (sizeof...(Tail) > 0) { return replace_tuple( - std::forward>(_tuple), - std::make_index_sequence{}) + std::forward>(_tuple)) .replace(std::forward(_tail)...); } else { return replace_tuple( - std::forward>(_tuple), - std::make_index_sequence{}); + std::forward>(_tuple)); } } /// Template specialization for std::tuple, so we can pass fields from other /// named tuples. template - auto replace(const std::tuple& _tuple, - Tail&&... _tail) const { + auto replace(std::tuple&& _tuple, Tail&&... _tail) const { if constexpr (sizeof...(Tail) > 0) { return replace_tuple( - std::forward>(_tuple), - std::make_index_sequence{}) + std::forward>(_tuple)) .replace(std::forward(_tail)...); } else { return replace_tuple( - std::forward>(_tuple), - std::make_index_sequence{}); + std::forward>(_tuple)); } } /// Template specialization for NamedTuple, so we can pass fields from other /// named tuples. template - auto replace(NamedTuple&& _named_tuple, - Tail&&... _tail) const { - return replace(_named_tuple.fields(), std::forward(_tail)...); + auto replace(NamedTuple&& _named_tuple, Tail&&... _tail) { + return replace( + std::forward>(_named_tuple).fields(), + std::forward(_tail)...); } /// Template specialization for NamedTuple, so we can pass fields from other @@ -332,7 +328,9 @@ class NamedTuple { template auto replace(const NamedTuple& _named_tuple, Tail&&... _tail) const { - return replace(_named_tuple.fields(), std::forward(_tail)...); + return replace( + std::forward>(_named_tuple).fields(), + std::forward(_tail)...); } /// Returns the underlying std::tuple. @@ -457,10 +455,22 @@ class NamedTuple { /// Adds the elements of a tuple to a newly created named tuple, /// and other elements to a newly created named tuple. - template - constexpr auto replace_tuple(Tuple&& _tuple, - const std::index_sequence) const { - return replace(std::get(_tuple)...); + template + auto replace_tuple(std::tuple&& _tuple) { + const auto r = [this](auto&&... _fields) { + return replace(std::forward(_fields)...); + }; + return std::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 { + const auto r = [this](auto&&... _fields) { + return replace(std::forward(_fields)...); + }; + return std::apply(r, std::forward>(_tuple)); } /// Retrieves the fields from another tuple. diff --git a/include/rfl/replace.hpp b/include/rfl/replace.hpp index b9df1d64..04e29ed0 100644 --- a/include/rfl/replace.hpp +++ b/include/rfl/replace.hpp @@ -12,10 +12,15 @@ namespace rfl { template auto replace(T&& _t, RField&& _field, OtherRFields&&... _other_fields) { if constexpr (internal::is_named_tuple_v) { - return std::forward(_t).replace(_field, _other_fields...); + return std::forward(_t).replace( + to_named_tuple(std::forward(_field)), + to_named_tuple(std::forward(_other_fields))...); } else { - return from_named_tuple(to_named_tuple(std::forward(_t)) - .replace(_field, _other_fields...)); + return from_named_tuple( + to_named_tuple(std::forward(_t)) + .replace(to_named_tuple(std::forward(_field)), + to_named_tuple( + std::forward(_other_fields))...)); } } @@ -24,10 +29,13 @@ auto replace(T&& _t, RField&& _field, OtherRFields&&... _other_fields) { template auto replace(const T& _t, RField&& _field, OtherRFields&&... _other_fields) { if constexpr (internal::is_named_tuple_v) { - return _t.replace(_field, _other_fields...); + return _t.replace( + to_named_tuple(std::forward(_field)), + to_named_tuple(std::forward(_other_fields))...); } else { - return from_named_tuple( - to_named_tuple(_t).replace(_field, _other_fields...)); + return from_named_tuple(to_named_tuple(_t).replace( + to_named_tuple(std::forward(_field)), + to_named_tuple(std::forward(_other_fields))...)); } } diff --git a/tests/test_replace_with_other_struct.hpp b/tests/test_replace_with_other_struct.hpp new file mode 100644 index 00000000..415b388a --- /dev/null +++ b/tests/test_replace_with_other_struct.hpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_replace_with_other_struct() { + std::cout << "test_replace_with_other_struct" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"age", int> age; + }; + + struct Employee { + rfl::Flatten person; + rfl::Field<"employer", std::string> employer; + rfl::Field<"salary", float> salary; + }; + + const auto employee = Employee{ + .person = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}, + .employer = std::string("Mr. Burns"), + .salary = 60000.0}; + + const auto carl = Person{.first_name = "Carl", .last_name = "", .age = 45}; + + const auto employee2 = rfl::replace(employee, carl); + + write_and_read( + employee2, + R"({"firstName":"Carl","lastName":"","age":45,"employer":"Mr. Burns","salary":60000.0})"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index 05227669..5be0b764 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -15,6 +15,7 @@ #include "test_ref.hpp" #include "test_replace.hpp" #include "test_replace_flatten.hpp" +#include "test_replace_with_other_struct.hpp" #include "test_tagged_union.hpp" #include "test_timestamp.hpp" #include "test_unique_ptr.hpp" @@ -44,6 +45,7 @@ int main() { test_replace(); test_replace_flatten(); + test_replace_with_other_struct(); test_as(); test_as_flatten();