mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-03 08:46:19 +08:00
Merge branch 'main' of github.com:getml/reflect-cpp
This commit is contained in:
@@ -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<std::map<...>> (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
|
||||
|
||||
|
||||
+3
-2
@@ -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.
|
||||
|
||||
+77
@@ -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<C>(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<C>(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> a;
|
||||
rfl::Flatten<B> 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<C>(a, b, rfl::make_field<"f5">(5));
|
||||
```
|
||||
|
||||
In this particular instance, `rfl::as<C>(...)` is basically equivalent to this:
|
||||
|
||||
```cpp
|
||||
const auto c = C{.a = a, .b = b, .f5 = rfl::make_field<"f5">(5)};
|
||||
```
|
||||
@@ -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:
|
||||
|
||||
|
||||
@@ -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> 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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user