diff --git a/docs/README.md b/docs/README.md index 41029c1d..5c03f4f1 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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. diff --git a/docs/optional_fields.md b/docs/optional_fields.md index 91c32040..fd9d4064 100644 --- a/docs/optional_fields.md +++ b/docs/optional_fields.md @@ -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 diff --git a/docs/rfl_ref.md b/docs/rfl_ref.md index 3b99cb37..53c0eb26 100644 --- a/docs/rfl_ref.md +++ b/docs/rfl_ref.md @@ -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> lesser; - rfl::Field<"greater", std::shared_ptr> greater; + rfl::Field<"lesser", std::unique_ptr> lesser; + rfl::Field<"greater", std::unique_ptr> 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> lesser; - rfl::Field<"greater", rfl::Ref> greater; + rfl::Field<"lesser", rfl::Box> lesser; + rfl::Field<"greater", rfl::Box> 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(leaf1), - .greater = rfl::make_ref(leaf2)}; + .lesser = rfl::make_box(leaf1), + .greater = rfl::make_box(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> lesser; + rfl::Field<"right", rfl::Ref> 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(leaf1), + .greater = rfl::make_ref(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}}}} +``` + +