-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ed390c
commit 7de20db
Showing
12 changed files
with
123 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef RFL_TO_VIEW_HPP_ | ||
#define RFL_TO_VIEW_HPP_ | ||
|
||
#include <iostream> | ||
#include <tuple> | ||
#include <type_traits> | ||
|
||
#include "rfl/internal/to_ptr_named_tuple.hpp" | ||
|
||
namespace rfl { | ||
|
||
template <class T> | ||
auto to_view(T& _t) { | ||
return internal::to_ptr_named_tuple(_t); | ||
} | ||
|
||
} // namespace rfl | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#include <cassert> | ||
#include <iostream> | ||
#include <rfl.hpp> | ||
#include <rfl/json.hpp> | ||
#include <source_location> | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "test_replace.hpp" | ||
#include "write_and_read.hpp" | ||
|
||
namespace test_view { | ||
|
||
struct Person { | ||
std::string first_name; | ||
std::string last_name; | ||
int age; | ||
}; | ||
|
||
void test() { | ||
std::cout << std::source_location::current().function_name() << std::endl; | ||
|
||
auto lisa = Person{.first_name = "Lisa", .last_name = "Simpson", .age = 8}; | ||
|
||
const auto view = rfl::to_view(lisa); | ||
|
||
*view.get<0>() = "Maggie"; | ||
*std::get<1>(view.values()) = "Simpson"; | ||
*view.get<"age">() = 0; | ||
|
||
write_and_read(lisa, | ||
R"({"first_name":"Maggie","last_name":"Simpson","age":0})"); | ||
} | ||
} // namespace test_view |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
namespace test_view { | ||
void test(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters