diff options
author | Stanislaw Halik <sthalik@misaki.pl> | 2024-04-03 23:56:57 +0200 |
---|---|---|
committer | Stanislaw Halik <sthalik@misaki.pl> | 2024-04-04 00:16:57 +0200 |
commit | 437794834f64a2ef2111f9e9437ab876eff4df88 (patch) | |
tree | 3466e01b5a008b190e4f9bb8d70afc9552107598 | |
parent | 949a94fd81c3f5f1788c17bb986dd9dde2f9c735 (diff) |
entity: fix getting enum min/max
-rw-r--r-- | entity/constraints.hpp | 8 | ||||
-rw-r--r-- | test/entity.cpp | 12 |
2 files changed, 20 insertions, 0 deletions
diff --git a/entity/constraints.hpp b/entity/constraints.hpp index c807fddc..6a5587d5 100644 --- a/entity/constraints.hpp +++ b/entity/constraints.hpp @@ -26,6 +26,14 @@ struct limit_traits<T> }; template<typename T> +requires std::is_enum_v<T> +struct limit_traits<T> +{ + static constexpr T min() { return T(limits<std::underlying_type_t<T>>::min); } + static constexpr T max() { return T(limits<std::underlying_type_t<T>>::max); } +}; + +template<typename T> struct limit_traits { static_assert(std::is_nothrow_default_constructible_v<T>); diff --git a/test/entity.cpp b/test/entity.cpp index 36302662..783721f1 100644 --- a/test/entity.cpp +++ b/test/entity.cpp @@ -281,6 +281,17 @@ void test_range4() fm_assert(r.second == rʹ.max); } +constexpr bool test_enum_range() +{ + enum class E { foo, bar }; + constexpr auto x = TestAccessors{}; + constexpr auto f = entity::type<E>::field("vec"_s, constantly(E::bar), [](auto&&, auto&&) {}); + static_assert(f.read(x) == E::bar); + constexpr auto r = f.get_range(x); + static_assert(r.max != E{0}); + return true; +} + } // namespace void test_app::test_entity() @@ -300,6 +311,7 @@ void test_app::test_entity() test_range2(); static_assert(test_range3()); test_range4(); + static_assert(test_enum_range()); } } // namespace floormat |