Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions include/rfl/parsing/ViewReaderWithDefault.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_PARSING_VIEWREADERWITHDEFAULT_HPP_

#include <array>
#include <memory>
#include <sstream>
#include <string_view>
#include <type_traits>
Expand Down Expand Up @@ -145,11 +146,20 @@ class ViewReaderWithDefault {

template <class Target, class Source>
static void move_to(Target* _t, Source* _s) {
if constexpr (std::is_const_v<Target>) {
return move_to(const_cast<std::remove_const_t<Target>*>(_t), _s);
} else if constexpr (!rfl::internal::is_array_v<Source> &&
!std::is_array_v<Target>) {
*_t = Target(std::move(*_s));
if constexpr (!rfl::internal::is_array_v<Source> &&
!std::is_array_v<Target>) {
if constexpr (std::is_const_v<Target>) {
// 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<Target>;
auto* const ptr = const_cast<NonConstTarget*>(_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<Source>) {
static_assert(std::is_array_v<Target>,
"Expected target to be a c-array.");
Expand Down
20 changes: 15 additions & 5 deletions include/rfl/parsing/ViewReaderWithDefaultAndStrippedFieldNames.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define RFL_PARSING_VIEWREADERWITHDEFAULTANDSTRIPPEDFIELDNAMES_HPP_

#include <array>
#include <memory>
#include <optional>
#include <sstream>
#include <type_traits>
Expand Down Expand Up @@ -115,11 +116,20 @@ class ViewReaderWithDefaultAndStrippedFieldNames {
// TODO: Unnecessary code duplication.
template <class Target, class Source>
static void move_to(Target* _t, Source* _s) {
if constexpr (std::is_const_v<Target>) {
return move_to(const_cast<std::remove_const_t<Target>*>(_t), _s);
} else if constexpr (!rfl::internal::is_array_v<Source> &&
!std::is_array_v<Target>) {
*_t = Target(std::move(*_s));
if constexpr (!rfl::internal::is_array_v<Source> &&
!std::is_array_v<Target>) {
if constexpr (std::is_const_v<Target>) {
// 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<Target>;
auto* const ptr = const_cast<NonConstTarget*>(_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<Source>) {
static_assert(std::is_array_v<Target>,
"Expected target to be a c-array.");
Expand Down
95 changes: 95 additions & 0 deletions tests/json/test_const_field_with_default.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <rfl.hpp>
#include <rfl/json.hpp>
#include <string>

#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<std::string> last_name = "Simpson";
const int age = 45;
};

TEST(json, test_const_field_with_default_val) {
const auto bart = rfl::json::read<Person>(
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<Person, rfl::DefaultIfMissing>(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<Town, rfl::DefaultIfMissing>(
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<Town, rfl::DefaultIfMissing>(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<Person, rfl::NoFieldNames>(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<Town, rfl::DefaultIfMissing, rfl::NoFieldNames>(
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<std::string> name = "unnamed";
};

TEST(json, test_const_field_without_assignment) {
const auto shape =
rfl::json::read<Shape>(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
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <string>

#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<int> n = 1;
};

TEST(json, test_c_array_const_with_default) {
const auto test =
rfl::json::read<Names>(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<Names, rfl::DefaultIfMissing>(R"({"n":2})").value();
EXPECT_EQ(defaults.names[1][1], "d");
EXPECT_EQ(defaults.n.value(), 2);
}

} // namespace test_c_array_const_with_default