mirror of
https://codeberg.org/DawnMagnet/reflect-cpp
synced 2026-12-02 16:16:19 +08:00
add rfl::get_enumerators() and rfl::get_enumerator_array()
This commit is contained in:
@@ -26,7 +26,7 @@ void test() {
|
||||
|
||||
write_and_read(circle, R"({"radius":2.0,"color":"green"})");
|
||||
|
||||
auto mutable_circle = Circle{.radius = 2.0, .color = Color::green};
|
||||
auto mutable_circle = circle;
|
||||
if (auto color = rfl::string_to_enum<Color>("red"); color) {
|
||||
mutable_circle.color = *color;
|
||||
}
|
||||
@@ -42,6 +42,23 @@ void test() {
|
||||
mutable_circle.color = *color;
|
||||
}
|
||||
write_and_read(mutable_circle, R"({"radius":2.0,"color":"blue"})");
|
||||
|
||||
rfl::get_enumerators<Color>().apply([&](auto field) {
|
||||
if constexpr (decltype(field)::name() == "green") {
|
||||
mutable_circle.color = field.value();
|
||||
}
|
||||
});
|
||||
write_and_read(mutable_circle, R"({"radius":2.0,"color":"green"})");
|
||||
|
||||
constexpr auto enumerator_array = rfl::get_enumerator_array<Color>();
|
||||
static_assert(enumerator_array[0].first == "red");
|
||||
static_assert(enumerator_array[1].first == "green");
|
||||
static_assert(enumerator_array[2].first == "blue");
|
||||
static_assert(enumerator_array[3].first == "yellow");
|
||||
static_assert(enumerator_array[0].second == Color::red);
|
||||
static_assert(enumerator_array[1].second == Color::green);
|
||||
static_assert(enumerator_array[2].second == Color::blue);
|
||||
static_assert(enumerator_array[3].second == Color::yellow);
|
||||
}
|
||||
|
||||
} // namespace test_enum
|
||||
|
||||
Reference in New Issue
Block a user