From 747f3421a2e2e84fcfac45fc2da07c8f4c849a13 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 20:06:42 +0200 Subject: [PATCH 1/7] Fixed typo in the code example --- docs/literals.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/literals.md b/docs/literals.md index 7d047607..8ed42f0b 100644 --- a/docs/literals.md +++ b/docs/literals.md @@ -22,10 +22,10 @@ Literals can be used inside switches: ```cpp switch(my_literal.value()) { - case MyLiteral::value_of<"option1">: + case MyLiteral::value_of<"option1">(): ... - case MyLiteral::value_of<"option2">: + case MyLiteral::value_of<"option2">(): ... ... From b546b1f189a4debd0bccaae376c99f896a199a0e Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 20:07:32 +0200 Subject: [PATCH 2/7] Literals --- docs/literals.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/literals.md b/docs/literals.md index 8ed42f0b..6671a490 100644 --- a/docs/literals.md +++ b/docs/literals.md @@ -32,8 +32,8 @@ switch(my_literal.value()) { } ``` -Literals are stored as `uint8_t` or `uint16_t` under-the-hood. But you can extract their name using `.name()` as a string -or the underlying value using `.value`. +Literals are stored as `uint8_t` or `uint16_t` under-the-hood. But you can extract their name as a string using `.name()` +or the underlying value using `.value()`. ```cpp using MyLiteral = rfl::Literal<"option1", "option2", ...>; From 4017487b8ab8371d3460e978fc203a6d2f50f64b Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 20:08:24 +0200 Subject: [PATCH 3/7] Removed redundancy from header --- docs/variants_and_tagged_unions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/variants_and_tagged_unions.md b/docs/variants_and_tagged_unions.md index d00a1545..bd04088a 100644 --- a/docs/variants_and_tagged_unions.md +++ b/docs/variants_and_tagged_unions.md @@ -36,7 +36,7 @@ several problems with this: 2) It leads to confusing error messages: If none of the alternatives can be matched, you will get an error message telling you why each of the alternatives couldn't be matched. Such error messages are very long-winding and hard to read. 3) It is dangerous. Imagine we had written `std::variant` instead of `std::variant`. This would mean that `Rectangle` could never be matched, because the fields in `Square` are a subset of `Rectangle`. This leads to very confusing bugs. -## Tagging `std::variant` (externally tagged) +## `std::variant` (externally tagged) From a functional programming point-of-view, the most straightforward way to handle these problems is to add tags. From 4fc826321d3df9ba818cd6ecefcec1ba90dd4567 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 20:11:29 +0200 Subject: [PATCH 4/7] Use names instead of types in the example --- docs/variants_and_tagged_unions.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/variants_and_tagged_unions.md b/docs/variants_and_tagged_unions.md index bd04088a..5e86a27d 100644 --- a/docs/variants_and_tagged_unions.md +++ b/docs/variants_and_tagged_unions.md @@ -136,12 +136,12 @@ const Shapes my_shape = rfl::make_field<"rectangle">(Rectangle{.height = 10, .width = 5}); const auto handle_shapes = [](const auto& field) { - using Type = typename std::decay_t(decltype(field))::Type; - if constexpr (std::is_same()) { + using Name = typename std::decay_t(decltype(field))::Name; + if constexpr (std::is_same>()) { std::cout << is circle, radius: << field.value().radius() << std::endl; - } else if constexpr (std::is_same()) { + } else if constexpr (std::is_same>()) { std::cout << is rectangle, width: << field.value().width() << ", height: " << field.value().height() << std::endl; - } else if constexpr (std::is_same()) { + } else if constexpr (std::is_same>()) { std::cout << is square, width: << field.value().width() << std::endl; } else { // reflect-cpp also provides this very useful helper that ensures From 538b3a6f7d39d7dda79cf89b350a6c5c6f349680 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 20:12:11 +0200 Subject: [PATCH 5/7] Fixed typo --- docs/variants_and_tagged_unions.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/variants_and_tagged_unions.md b/docs/variants_and_tagged_unions.md index 5e86a27d..a0d25d41 100644 --- a/docs/variants_and_tagged_unions.md +++ b/docs/variants_and_tagged_unions.md @@ -141,7 +141,7 @@ const auto handle_shapes = [](const auto& field) { std::cout << is circle, radius: << field.value().radius() << std::endl; } else if constexpr (std::is_same>()) { std::cout << is rectangle, width: << field.value().width() << ", height: " << field.value().height() << std::endl; - } else if constexpr (std::is_same>()) { + } else if constexpr (std::is_same>()) { std::cout << is square, width: << field.value().width() << std::endl; } else { // reflect-cpp also provides this very useful helper that ensures From 96987b09ef780a4af0efba6bf1ba4f4c7dd5801d Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 20:22:01 +0200 Subject: [PATCH 6/7] Added note on custom constructors --- docs/structs.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/docs/structs.md b/docs/structs.md index cc4ab153..09e17399 100644 --- a/docs/structs.md +++ b/docs/structs.md @@ -70,3 +70,18 @@ And you can parse it back into a struct: ```cpp const auto homer = rfl::json::read(json_string).value(); ``` + +## Important note + +**Do not create custom constructors on the structs.** + +reflect-cpp needs to be able to create the structs like this: + +```cpp +Person{rfl::Field<"firstName", std::string>("Bart"), rfl::Field<"lastName", std::string>("Simpson"), ...}; +``` + +But if you create a custom constructor, then C++ will no longer allow this kind of constructions. + +If you want to create the struct from one of your classes (the most like reason, you want to create custom constructors in the first place), +you might want to check out the section on [custom classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md) or [custom parsers](https://github.com/getml/reflect-cpp/blob/main/docs/custom_parser.md). From 174440722219d1e97c04b52a9ef93247078b3b74 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Wed, 4 Oct 2023 20:24:53 +0200 Subject: [PATCH 7/7] Seperate out the JSON in flatten structs --- docs/flatten_structs.md | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/docs/flatten_structs.md b/docs/flatten_structs.md index c1827b9d..a13ad43d 100644 --- a/docs/flatten_structs.md +++ b/docs/flatten_structs.md @@ -9,7 +9,7 @@ all fields will be flattened to a single JSON object. If there are duplicate field names, you will get a compile-time error. -## Example: Every Employee is a person. +## Example: Every employee is a person. ```cpp struct Person { @@ -28,7 +28,12 @@ const auto employee = Employee{ .salary = 60000.0 }; -// Flattens all fields into a single JSON object: -// {"firstName":"Homer","lastName":"Simpson","age":45,"salary":60000.0} const auto json_str = rfl::json::write(employee); ``` + +This flattens all fields into a single JSON object: + +```json +{"firstName":"Homer","lastName":"Simpson","age":45,"salary":60000.0} +``` +