mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-06 02:36:18 +08:00
Added support for rfl::to_view; #32
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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";
|
||||
```
|
||||
Reference in New Issue
Block a user