mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-04 01:06:19 +08:00
add rfl::NamedTuple::apply() for convenient compile-time iteration
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <rfl/json.hpp>
|
||||
#include <source_location>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "test_replace.hpp"
|
||||
@@ -30,5 +31,23 @@ void test() {
|
||||
|
||||
write_and_read(lisa,
|
||||
R"({"first_name":"Maggie","last_name":"Simpson","age":0})");
|
||||
|
||||
view.apply([]<typename Field>(const Field& field) {
|
||||
using field_type = std::remove_pointer_t<typename Field::Type>;
|
||||
if constexpr (field.name() == "age") {
|
||||
static_assert(std::is_same_v<field_type, int>);
|
||||
} else {
|
||||
static_assert(std::is_same_v<field_type, std::string>);
|
||||
}
|
||||
});
|
||||
|
||||
rfl::to_view(lisa).apply([](auto&& field) {
|
||||
if constexpr (field.name() == "first_name") {
|
||||
*field.value() = "Bart";
|
||||
}
|
||||
});
|
||||
|
||||
write_and_read(lisa,
|
||||
R"({"first_name":"Bart","last_name":"Simpson","age":0})");
|
||||
}
|
||||
} // namespace test_view
|
||||
|
||||
Reference in New Issue
Block a user