Added support for rfl::to_view; #32

This commit is contained in:
Patrick Urbanke
2023-12-24 13:50:23 +01:00
parent 8ed390c3bf
commit 7de20db53e
12 changed files with 123 additions and 15 deletions
+2
View File
@@ -50,6 +50,8 @@
4.3) [rfl::NamedTuple](https://github.com/getml/reflect-cpp/blob/main/docs/named_tuple.md) - For structural typing.
4.4) [rfl::to_view](https://github.com/getml/reflect-cpp/blob/main/docs/to_view.md) - For accessing fields of a struct by index or name.
## 5) Supported formats
5.1) [JSON](https://github.com/getml/reflect-cpp/blob/main/docs/json.md)
+30
View File
@@ -0,0 +1,30 @@
# `rfl::to_view`
`rfl::to_view` allows you to create views on structs using which you can access an modify the fields of the structs just like a tuple.
Under-the-hood, a view is a `rfl::NamedTuple` containing pointers to the original fields.
For example:
```cpp
auto lisa = Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8};
const auto view = rfl::to_view(lisa);
// Assigns the first field, thus modifying the struct 'lisa'.
*view.get<0>() = "Maggie";
// view.values() is a std::tuple containing
// pointers to the original fields.
*std::get<1>(view.values()) = "Simpson";
// You can also access fields by their name.
// The correctness will be ensured at compile time.
*view.get<"age">() = 0;
// You can also access fields like this.
*rfl::get<0>(view) = "Maggie";
// Or like this.
*rfl::get<"first_name">(view) = "Maggie";
```