diff --git a/include/rfl/Field.hpp b/include/rfl/Field.hpp index 669cfec7..f239dfbf 100644 --- a/include/rfl/Field.hpp +++ b/include/rfl/Field.hpp @@ -99,7 +99,7 @@ struct Field { /// Assigns the underlying object. inline void operator=(Field<_name, Type>&& _field) noexcept { - value_ = _field.get(); + value_ = std::move(_field.value_); } /// Assigns the underlying object. diff --git a/include/rfl/Flatten.hpp b/include/rfl/Flatten.hpp index 6ae18f43..eaafdc39 100644 --- a/include/rfl/Flatten.hpp +++ b/include/rfl/Flatten.hpp @@ -17,11 +17,12 @@ struct Flatten { Flatten(const Type& _value) : value_(_value) {} - Flatten(Type&& _value) : value_(std::forward(_value)) {} + Flatten(Type&& _value) noexcept : value_(std::forward(_value)) {} Flatten(const Flatten& _f) : value_(_f.get()) {} - Flatten(Flatten&& _f) : value_(std::forward(_f.value_)) {} + Flatten(Flatten&& _f) noexcept + : value_(std::forward(_f.value_)) {} template Flatten(const Flatten& _f) : value_(_f.get()) {} @@ -72,7 +73,7 @@ struct Flatten { /// Assigns the underlying object. inline void operator=(Flatten&& _f) { - value_ = std::forward(_f); + value_ = std::forward(_f.value_); } /// Assigns the underlying object. diff --git a/include/rfl/internal/move_and_flatten_field_tuple.hpp b/include/rfl/internal/move_and_flatten_field_tuple.hpp index fded82f2..3ae84018 100644 --- a/include/rfl/internal/move_and_flatten_field_tuple.hpp +++ b/include/rfl/internal/move_and_flatten_field_tuple.hpp @@ -3,8 +3,8 @@ #include -#include "rfl/internal/copy_to_field_tuple.hpp" #include "rfl/internal/is_flatten_field.hpp" +#include "rfl/internal/move_to_field_tuple.hpp" namespace rfl { namespace internal { @@ -20,8 +20,7 @@ auto move_and_flatten_field_tuple(FieldTuple&& _t, Args&&... _args) { return move_and_flatten_field_tuple( std::move(_t), std::move(_args)..., move_and_flatten_field_tuple( - copy_to_field_tuple( // TODO: move_to_... - std::get(_t).get()))); + move_to_field_tuple(std::move(std::get(_t).value_)))); } else { return move_and_flatten_field_tuple( std::move(_t), std::move(_args)..., diff --git a/tests/test_replace_flatten.hpp b/tests/test_replace_flatten.hpp new file mode 100644 index 00000000..31e01e78 --- /dev/null +++ b/tests/test_replace_flatten.hpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +void test_replace_flatten() { + std::cout << "test_replace_flatten" << std::endl; + + struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", rfl::Box> last_name; + rfl::Field<"age", int> age; + }; + + struct Employee { + rfl::Flatten person; + rfl::Field<"employer", rfl::Box> employer; + rfl::Field<"salary", float> salary; + }; + + auto employee = Employee{ + .person = Person{.first_name = "Homer", + .last_name = rfl::make_box("Simpson"), + .age = 45}, + .employer = rfl::make_box("Mr. Burns"), + .salary = 60000.0}; + + auto employee2 = + rfl::replace(std::move(employee), rfl::make_field<"salary">(70000.0)); + + write_and_read( + employee2, + R"({"firstName":"Homer","lastName":"Simpson","age":45,"employer":"Mr. Burns","salary":70000.0})"); +} diff --git a/tests/tests.cpp b/tests/tests.cpp index f4a91ca8..dae6b359 100644 --- a/tests/tests.cpp +++ b/tests/tests.cpp @@ -12,6 +12,7 @@ #include "test_readme_example.hpp" #include "test_ref.hpp" #include "test_replace.hpp" +#include "test_replace_flatten.hpp" #include "test_tagged_union.hpp" #include "test_timestamp.hpp" #include "test_unique_ptr.hpp" @@ -40,6 +41,7 @@ int main() { test_custom_class4(); test_replace(); + test_replace_flatten(); return 0; }