From 244ea2e019d0c3193cf060b43f0f52617d8a43e2 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 8 Oct 2023 11:35:01 +0200 Subject: [PATCH 1/5] Replacing structs with other structs --- docs/replace.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/docs/replace.md b/docs/replace.md index 6332a7d7..b8c624a8 100644 --- a/docs/replace.md +++ b/docs/replace.md @@ -92,3 +92,40 @@ auto employee2 = In this case `age` is part of `Person` and `salary` part ot `Employee`, but because you have flattened them, you can treat them as if they were on the same level. + +You can also replace structs with other structs. Consider the following example: + +```cpp +struct Person { + rfl::Field<"firstName", std::string> first_name; + rfl::Field<"lastName", std::string> last_name; + rfl::Field<"age", int> age; +}; + +struct Employee { + rfl::Flatten person; + rfl::Field<"employer", std::string> employer; + rfl::Field<"salary", float> salary; +}; + +const auto employee = Employee{ + .person = + Person{.first_name = "Homer", .last_name = "Simpson", .age = 45}, + .employer = std::string("Mr. Burns"), + .salary = 60000.0}; + +const auto carl = Person{.first_name = "Carl", .last_name = "", .age = 45}; + +const auto employee2 = rfl::replace(employee, carl); +``` + +This code flattens the employee structs and then replaces all relevant fields with their counterparts contained in `carl`. + +Finally, we get this JSON string: + +```json +{"firstName":"Carl","lastName":"","age":45,"employer":"Mr. Burns","salary":60000.0} +``` + +Don't worry, this is fairly optimized and makes heavy use of forwarding. It does not make any copies than it absolutely has to. + From 120f2cba92696989bc40185b45f0f7f72d2888d1 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 8 Oct 2023 11:49:21 +0200 Subject: [PATCH 2/5] Added documentation for rfl::as --- docs/as.md | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 docs/as.md diff --git a/docs/as.md b/docs/as.md new file mode 100644 index 00000000..b0451986 --- /dev/null +++ b/docs/as.md @@ -0,0 +1,77 @@ +# `rfl::as` + +`rfl::as` allows you to cast structs as other structs. This is useful if the fields in `B` are a subset of the fields in `A` +or we need the fields in `A` and `B` to produce `C`. + +For example: + +```cpp +struct A { + rfl::Field<"f1", std::string> f1; + rfl::Field<"f2", std::string> f2; +}; + +struct B { + rfl::Field<"f3", std::string> f3; + rfl::Field<"f4", std::string> f4; +}; + +struct C { + rfl::Field<"f1", std::string> f1; + rfl::Field<"f2", std::string> f2; + rfl::Field<"f4", std::string> f4; +}; + +const auto a = A{.f1 = "Hello", .f2 = "World"}; + +const auto b = B{.f3 = "Hello", .f4 = "World"}; + +const auto c = rfl::as(a, b); +``` + +In this case, `f1`, `f2` and `f4` are now copied into the newly created `c` of type `C`. + +Much like `rfl::replace`, `rfl::as` supports move semantics: + +```cpp +auto a = A{.f1 = "Hello", .f2 = "World"}; + +auto b = B{.f3 = "Hello", .f4 = "World"}; + +const auto c = rfl::as(std::move(a), std::move(b)); +``` + +In this case, `f1`, `f2` and `f4` are now moved into the newly created `c` of type `C`. This is a zero-copy operation. `f3` is discarded. + +`rfl::as` also supports `rfl::Flatten`. For instance: + +```cpp +struct A { + rfl::Field<"f1", std::string> f1; + rfl::Field<"f2", std::string> f2; +}; + +struct B { + rfl::Field<"f3", std::string> f3; + rfl::Field<"f4", std::string> f4; +}; + +struct C { + rfl::Flatten a; + rfl::Flatten b; + rfl::Field<"f5", int> f5; +}; + +const auto a = A{.f1 = "Hello", .f2 = "World"}; + +const auto b = B{.f3 = "Hello", .f4 = "World"}; + +const auto c = + rfl::as(a, b, rfl::make_field<"f5">(5)); +``` + +In this particular instance, `rfl::as(...)` is basically equivalent to this: + +```cpp +const auto c = C{.a = a, .b = b, .f5 = rfl::make_field<"f5">(5)}; +``` From b10c81dcfa94a451898004dee46f7cbf5222aaa5 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 8 Oct 2023 11:50:47 +0200 Subject: [PATCH 3/5] Update README.md --- docs/README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/README.md b/docs/README.md index 6e514a51..649a67f6 100644 --- a/docs/README.md +++ b/docs/README.md @@ -18,13 +18,14 @@ 1.7) [rfl::Timestamp](https://github.com/getml/reflect-cpp/blob/main/docs/timestamps.md) - For serializing and deserializing timestamps. - ## 2) Custom classes 2.1) [Custom classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_classes.md) - For custom classes with private fields. 2.2) [Custom parsers for your classes](https://github.com/getml/reflect-cpp/blob/main/docs/custom_parser.md) - For custom classes with private fields that you want to leave absolutely untouched. -## 3) Useful helper functions +## 3) Useful helper functions and classes 3.1) [rfl::replace](https://github.com/getml/reflect-cpp/blob/main/docs/replace.md) - For replacing one or several fields in a struct. + +3.2) [rfl::as](https://github.com/getml/reflect-cpp/blob/main/docs/as.md) - For casting structs as other structs. From 6c2d947063ce231a6420dd07f367675e14d2616e Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 8 Oct 2023 11:54:03 +0200 Subject: [PATCH 4/5] Update the current status section --- README.md | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 21639d9d..8fa70e5a 100644 --- a/README.md +++ b/README.md @@ -147,12 +147,9 @@ Click [here](https://github.com/getml/reflect-cpp/tree/main/docs). At the moment, this is work-in-progress. Here are some things that are still missing: -- Extensive testing -- Benchmarks -- Documentation is unfinished -- Support for std::array, std::unique_ptr and full support for rfl::Box -- Support for rfl::Flatten> (serde supports this as well) -- Support for std::any (whether we want to support this as all is still being debated). +- Benchmarks and optimization +- Documentation is unfinished (missing rfl::Result, rfl::NamedTuple as well as extension to other serialization formats) +- Support for Clang and MSVC ## Installation From 575d36307e12f94153abddc613a6579bbf4d2015 Mon Sep 17 00:00:00 2001 From: liuzicheng1987 Date: Sun, 8 Oct 2023 16:27:33 +0200 Subject: [PATCH 5/5] Update optional_fields.md, fixed typo --- docs/optional_fields.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/optional_fields.md b/docs/optional_fields.md index fd9d4064..19fb4ce8 100644 --- a/docs/optional_fields.md +++ b/docs/optional_fields.md @@ -13,8 +13,7 @@ But Bart doesn't have any children. After all, he's only 10 years old. To indica 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 -when you read the JSON string, the fields will not be required. +then the field will not be written to the JSON string. Likewise, when you read the JSON string, the fields will not be required. So this is how we could rewrite the previous example: