add rfl::NamedTuple::apply() for convenient compile-time iteration

This commit is contained in:
stu
2024-02-07 12:31:45 +00:00
parent 70b8c0b558
commit fb0dd371b3
5 changed files with 66 additions and 1 deletions
+19
View File
@@ -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