diff --git a/include/rfl/parsing/ViewReaderWithDefault.hpp b/include/rfl/parsing/ViewReaderWithDefault.hpp index 5faf08f21..c2e034322 100644 --- a/include/rfl/parsing/ViewReaderWithDefault.hpp +++ b/include/rfl/parsing/ViewReaderWithDefault.hpp @@ -2,6 +2,7 @@ #define RFL_PARSING_VIEWREADERWITHDEFAULT_HPP_ #include +#include #include #include #include @@ -145,11 +146,20 @@ class ViewReaderWithDefault { template static void move_to(Target* _t, Source* _s) { - if constexpr (std::is_const_v) { - return move_to(const_cast*>(_t), _s); - } else if constexpr (!rfl::internal::is_array_v && - !std::is_array_v) { - *_t = Target(std::move(*_s)); + if constexpr (!rfl::internal::is_array_v && + !std::is_array_v) { + if constexpr (std::is_const_v) { + // The struct was default-constructed, so *_t is a live const object + // and must not be assigned to. Re-create it in place instead. If the + // constructor throws, we are inside a noexcept function, so it cannot + // be destroyed twice. + using NonConstTarget = std::remove_const_t; + auto* const ptr = const_cast(_t); + std::destroy_at(ptr); + ::new (ptr) NonConstTarget(std::move(*_s)); + } else { + *_t = Target(std::move(*_s)); + } } else if constexpr (rfl::internal::is_array_v) { static_assert(std::is_array_v, "Expected target to be a c-array."); diff --git a/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp b/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp index 264380265..72512ff66 100644 --- a/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp +++ b/include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp @@ -2,6 +2,7 @@ #define RFL_PARSING_VIEWREADERWITHDEFAULTANDSTRIPPEDFIELDNAMES_HPP_ #include +#include #include #include #include @@ -115,11 +116,20 @@ class ViewReaderWithDefaultAndStrippedFieldNames { // TODO: Unnecessary code duplication. template static void move_to(Target* _t, Source* _s) { - if constexpr (std::is_const_v) { - return move_to(const_cast*>(_t), _s); - } else if constexpr (!rfl::internal::is_array_v && - !std::is_array_v) { - *_t = Target(std::move(*_s)); + if constexpr (!rfl::internal::is_array_v && + !std::is_array_v) { + if constexpr (std::is_const_v) { + // The struct was default-constructed, so *_t is a live const object + // and must not be assigned to. Re-create it in place instead. If the + // constructor throws, we are inside a noexcept function, so it cannot + // be destroyed twice. + using NonConstTarget = std::remove_const_t; + auto* const ptr = const_cast(_t); + std::destroy_at(ptr); + ::new (ptr) NonConstTarget(std::move(*_s)); + } else { + *_t = Target(std::move(*_s)); + } } else if constexpr (rfl::internal::is_array_v) { static_assert(std::is_array_v, "Expected target to be a c-array."); diff --git a/tests/json/test_const_field_with_default.cpp b/tests/json/test_const_field_with_default.cpp new file mode 100644 index 000000000..c94ef16cf --- /dev/null +++ b/tests/json/test_const_field_with_default.cpp @@ -0,0 +1,95 @@ +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_const_field_with_default { + +// Structs with rfl::DefaultVal fields, or read with rfl::DefaultIfMissing, +// are default-constructed first and then filled in. The const fields in them +// must be re-created in place rather than assigned to. + +struct Person { + const std::string first_name = "Homer"; + rfl::DefaultVal last_name = "Simpson"; + const int age = 45; +}; + +TEST(json, test_const_field_with_default_val) { + const auto bart = rfl::json::read( + R"({"first_name":"Bart","last_name":"S","age":10})") + .value(); + EXPECT_EQ(bart.first_name, "Bart"); + EXPECT_EQ(bart.last_name.value(), "S"); + EXPECT_EQ(bart.age, 10); +} + +TEST(json, test_const_field_with_default_val_missing) { + const auto homer = + rfl::json::read(R"({"last_name":"S"})") + .value(); + EXPECT_EQ(homer.first_name, "Homer"); + EXPECT_EQ(homer.last_name.value(), "S"); + EXPECT_EQ(homer.age, 45); +} + +struct Town { + const std::string name = "Springfield"; + const int population = 100; + std::string state; +}; + +TEST(json, test_const_field_with_default_if_missing) { + const auto shelbyville = + rfl::json::read( + R"({"name":"Shelbyville","population":200})") + .value(); + EXPECT_EQ(shelbyville.name, "Shelbyville"); + EXPECT_EQ(shelbyville.population, 200); + EXPECT_EQ(shelbyville.state, ""); + + const auto springfield = + rfl::json::read(R"({})").value(); + EXPECT_EQ(springfield.name, "Springfield"); + EXPECT_EQ(springfield.population, 100); +} + +TEST(json, test_const_field_with_default_val_no_field_names) { + const auto bart = + rfl::json::read(R"(["Bart","S",10])") + .value(); + EXPECT_EQ(bart.first_name, "Bart"); + EXPECT_EQ(bart.last_name.value(), "S"); + EXPECT_EQ(bart.age, 10); + + const auto shelbyville = + rfl::json::read( + R"(["Shelbyville",200])") + .value(); + EXPECT_EQ(shelbyville.name, "Shelbyville"); + EXPECT_EQ(shelbyville.population, 200); + EXPECT_EQ(shelbyville.state, ""); +} + +// Point has no assignment operator, so a const Point field can only be +// filled in by re-creating it. +struct Point { + const int x = 0; + const int y = 0; +}; + +struct Shape { + const Point origin = {}; + rfl::DefaultVal name = "unnamed"; +}; + +TEST(json, test_const_field_without_assignment) { + const auto shape = + rfl::json::read(R"({"origin":{"x":1,"y":2}})").value(); + EXPECT_EQ(shape.origin.x, 1); + EXPECT_EQ(shape.origin.y, 2); + EXPECT_EQ(shape.name.value(), "unnamed"); +} + +} // namespace test_const_field_with_default diff --git a/tests/json_c_arrays_and_inheritance/test_c_array_const_with_default.cpp b/tests/json_c_arrays_and_inheritance/test_c_array_const_with_default.cpp new file mode 100644 index 000000000..8039a8ed6 --- /dev/null +++ b/tests/json_c_arrays_and_inheritance/test_c_array_const_with_default.cpp @@ -0,0 +1,29 @@ +#include + +#include "write_and_read.hpp" + +namespace test_c_array_const_with_default { + +// The elements of a const c-array field are re-created in place, like any +// other const field of a struct that is default-constructed first. +struct Names { + const std::string names[2][2] = {{"a", "b"}, {"c", "d"}}; + rfl::DefaultVal n = 1; +}; + +TEST(json, test_c_array_const_with_default) { + const auto test = + rfl::json::read(R"({"names":[["e","f"],["g","h"]]})").value(); + EXPECT_EQ(test.names[0][0], "e"); + EXPECT_EQ(test.names[0][1], "f"); + EXPECT_EQ(test.names[1][0], "g"); + EXPECT_EQ(test.names[1][1], "h"); + EXPECT_EQ(test.n.value(), 1); + + const auto defaults = + rfl::json::read(R"({"n":2})").value(); + EXPECT_EQ(defaults.names[1][1], "d"); + EXPECT_EQ(defaults.n.value(), 2); +} + +} // namespace test_c_array_const_with_default