Merge branch 'main' of github.com:getml/reflect-cpp

This commit is contained in:
Patrick Urbanke
2023-10-07 13:53:27 +02:00
3 changed files with 61 additions and 20 deletions
+1 -1
View File
@@ -14,7 +14,7 @@
1.5) [std::variant and rfl::TaggedUnion](https://github.com/getml/reflect-cpp/blob/main/docs/variants_and_tagged_unions.md) - For structs that can be one of several formats. This is the equivalent of an OR statement or a sum type in type theory.
1.6) [rfl::Ref](https://github.com/getml/reflect-cpp/blob/main/docs/rfl_ref.md) - For defining recursive structures.
1.6) [rfl::Box and rfl::Ref](https://github.com/getml/reflect-cpp/blob/main/docs/rfl_ref.md) - For defining recursive structures.
1.7) [rfl::Timestamp](https://github.com/getml/reflect-cpp/blob/main/docs/timestamps.md) - For serializing and deserializing timestamps.
+1 -1
View File
@@ -10,7 +10,7 @@ we generated a JSON string like this:
Likewise, if we read the JSON string and the field "children" is missing, that will result in a runtime error.
But Bart doesn't have any children. After all, he's only 10 years old. To indicate to the library that the field "children" is optional,
we can use either `std::optional`, `std::shared_ptr` or `std::unique_ptr` (TODO: unique_ptr currently unsupported).
we can use either `std::optional`, `std::shared_ptr` or `std::unique_ptr`.
If `std::optional` is `std::nullopt` or any of the smart pointers is a `nullptr`,
then the field will not be written to the JSON string. Likewise, it is not required
+59 -18
View File
@@ -1,4 +1,4 @@
# `rfl::Ref`
# `rfl::Box` and `rfl::Ref`
In previous sections, we have defined the `Person` class recursively:
@@ -52,7 +52,7 @@ struct DecisionTree {
Again, this will not compile, because the compiler cannot figure out the intended size of the struct.
A possible solution might be to use `std::shared_ptr`:
A possible solution might be to use `std::unique_ptr`:
```cpp
// Will compile, but not an ideal design.
@@ -65,8 +65,8 @@ struct DecisionTree {
struct Node {
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"lesser", std::shared_ptr<DecisionTree>> lesser;
rfl::Field<"greater", std::shared_ptr<DecisionTree>> greater;
rfl::Field<"lesser", std::unique_ptr<DecisionTree>> lesser;
rfl::Field<"greater", std::unique_ptr<DecisionTree>> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
@@ -85,10 +85,9 @@ upfront as we possibly can. For a great theoretical discussion of this topic, ch
by Alexis King.
So how would we encode our assumptions that the fields "lesser" and "greater" must exist in the type system and
still have code that compiles? By using `rfl::Ref` instead of `std::shared_ptr`:
still have code that compiles? By using `rfl::Box` instead of `std::unique_ptr`:
```cpp
// Will compile, but not an ideal design.
struct DecisionTree {
struct Leaf {
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
@@ -98,8 +97,8 @@ struct DecisionTree {
struct Node {
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"lesser", rfl::Ref<DecisionTree>> lesser;
rfl::Field<"greater", rfl::Ref<DecisionTree>> greater;
rfl::Field<"lesser", rfl::Box<DecisionTree>> lesser;
rfl::Field<"greater", rfl::Box<DecisionTree>> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
@@ -108,27 +107,27 @@ struct DecisionTree {
};
```
`rfl::Ref` is a thin wrapper around `std::shared_ptr`, but it is guaranteed to **never be null**. It is a shared pointer without the `nullptr`.
`rfl::Box` is a thin wrapper around `std::unique_ptr`, but it is guaranteed to **never be null**. It is a `std::unique_ptr` without the `nullptr`.
If you want to learn more about the evils of null references, check out the
[Null References: The Billion Dollar Mistake](https://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare/)
by Tony Hoare, who invented the concept in the first place.
You **must** initialize `rfl::Ref` the moment you create it and it cannot be dereferenced until it is destroyed.
You **must** initialize `rfl::Box` the moment you create it and it cannot be dereferenced until it is destroyed.
`rfl::Ref` can be initialized using `rfl::make_ref<...>(...)`, just like `std::make_shared<...>(...)`:
`rfl::Box` can be initialized using `rfl::make_box<...>(...)`, just like `std::make_unique<...>(...)`:
```cpp
const auto leaf1 = DecisionTree::Leaf{.value = 3.0};
auto leaf1 = DecisionTree::Leaf{.value = 3.0};
const auto leaf2 = DecisionTree::Leaf{.value = 5.0};
auto leaf2 = DecisionTree::Leaf{.value = 5.0};
const auto node =
auto node =
DecisionTree::Node{.critical_value = 10.0,
.lesser = rfl::make_ref<DecisionTree>(leaf1),
.greater = rfl::make_ref<DecisionTree>(leaf2)};
.lesser = rfl::make_box<DecisionTree>(leaf1),
.greater = rfl::make_box<DecisionTree>(leaf2)};
const tree = DecisionTree{.leaf_or_node = node};
const DecisionTree tree{.leaf_or_node = std::move(node)};
const auto json_string = rfl::json::write(tree);
```
@@ -139,5 +138,47 @@ This will result in the following JSON string:
{"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}}
```
TODO: `rfl::Box` should get the job done as well, but is currently unsupported.
If you want to use reference-counted pointers, instead of unique pointers, you can use `rfl::Ref`.
`rfl::Ref` is the same concept as `rfl::Box`, but using `std::shared_ptr` under-the-hood.
```cpp
struct DecisionTree {
struct Leaf {
rfl::Field<"type", rfl::Literal<"Leaf">> type = rfl::default_value;
rfl::Field<"value", double> value;
};
struct Node {
rfl::Field<"type", rfl::Literal<"Node">> type = rfl::default_value;
rfl::Field<"criticalValue", double> critical_value;
rfl::Field<"left", rfl::Ref<DecisionTree>> lesser;
rfl::Field<"right", rfl::Ref<DecisionTree>> greater;
};
using LeafOrNode = rfl::TaggedUnion<"type", Leaf, Node>;
rfl::Field<"leafOrNode", LeafOrNode> leaf_or_node;
};
const auto leaf1 = DecisionTree::Leaf{.value = 3.0};
const auto leaf2 = DecisionTree::Leaf{.value = 5.0};
auto node =
DecisionTree::Node{.critical_value = 10.0,
.lesser = rfl::make_ref<DecisionTree>(leaf1),
.greater = rfl::make_ref<DecisionTree>(leaf2)};
const DecisionTree tree{.leaf_or_node = std::move(node)};
const auto json_string = rfl::json::write(tree);
```
The resulting JSON string is identical:
```json
{"leafOrNode":{"type":"Node","criticalValue":10.0,"lesser":{"leafOrNode":{"type":"Leaf","value":3.0}},"greater":{"leafOrNode":{"type":"Leaf","value":5.0}}}}
```