Made sure that automatic field retrieval works in the context of tagged unions

This commit is contained in:
Patrick Urbanke
2023-11-27 20:04:30 +01:00
parent ac244c082d
commit abe7bd3546
4 changed files with 18 additions and 16 deletions
+3 -1
View File
@@ -115,7 +115,9 @@ with open("generated_code5.cpp", "w", encoding="utf-8") as codefile:
template <class T>
auto get_field_names() {
if constexpr (has_n_fields<T, 0>) {
if constexpr (std::is_pointer_v<std::decay_t<T>>) {
return get_field_names<std::remove_pointer_t<T>>();
} else if constexpr (has_n_fields<T, 0>) {
return std::array<std::string, 0>{};
} else if constexpr (has_n_fields<T, 1>) {
auto& [f1] = fake_object<T>;
+5 -5
View File
@@ -12,14 +12,14 @@ namespace test_box {
struct DecisionTree {
struct Leaf {
using Tag = rfl::Literal<"Leaf">;
rfl::Field<"value", double> value;
double value;
};
struct Node {
using Tag = rfl::Literal<"Node">;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"left", rfl::Box<DecisionTree>> lesser;
rfl::Field<"right", rfl::Box<DecisionTree>> greater;
rfl::Rename<"criticalValue", double> critical_value;
rfl::Box<DecisionTree> lesser;
rfl::Box<DecisionTree> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
@@ -43,6 +43,6 @@ void test() {
write_and_read(
tree,
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"left":{"leafOrNode":{"type":"Leaf","value":3.0}},"right":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
}
} // namespace test_box
+5 -5
View File
@@ -12,14 +12,14 @@ namespace test_ref {
struct DecisionTree {
struct Leaf {
using Tag = rfl::Literal<"Leaf">;
rfl::Field<"value", double> value;
double value;
};
struct Node {
using Tag = rfl::Literal<"Node">;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"left", rfl::Ref<DecisionTree>> lesser;
rfl::Field<"right", rfl::Ref<DecisionTree>> greater;
rfl::Rename<"criticalValue", double> critical_value;
rfl::Ref<DecisionTree> lesser;
rfl::Ref<DecisionTree> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
@@ -43,6 +43,6 @@ void test() {
write_and_read(
tree,
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"left":{"leafOrNode":{"type":"Leaf","value":3.0}},"right":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
}
} // namespace test_ref
+5 -5
View File
@@ -12,14 +12,14 @@ namespace test_unique_ptr2 {
struct DecisionTree {
struct Leaf {
using Tag = rfl::Literal<"Leaf">;
rfl::Field<"value", double> value;
double value;
};
struct Node {
using Tag = rfl::Literal<"Node">;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"left", std::unique_ptr<DecisionTree>> lesser;
rfl::Field<"right", std::unique_ptr<DecisionTree>> greater;
rfl::Rename<"criticalValue", double> critical_value;
std::unique_ptr<DecisionTree> lesser;
std::unique_ptr<DecisionTree> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
@@ -43,6 +43,6 @@ void test() {
write_and_read(
tree,
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"left":{"leafOrNode":{"type":"Leaf","value":3.0}},"right":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
R"({"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}})");
}
} // namespace test_unique_ptr2