1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00

Add enum fusing function (#127)

This commit is contained in:
Pavel I. Kryukov 2022-02-10 20:58:59 +03:00 committed by GitHub
parent 22242a613a
commit 1f8e29b140
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 0 deletions

View file

@ -609,6 +609,15 @@ struct underlying_type {};
template <typename T>
struct underlying_type<T, true> : std::underlying_type<std::decay_t<T>> {};
constexpr size_t cantor_pair(size_t v1, size_t v2) noexcept {
return (((v1 + v2) * (v1 + v2 + 1)) >> 1) + v2;
}
template<typename ... Ts>
constexpr size_t cantor_pair(size_t v1, size_t head, Ts ... tail) noexcept {
return cantor_pair(cantor_pair(v1, head), tail...);
}
} // namespace magic_enum::detail
// Checks is magic_enum supported compiler.
@ -878,6 +887,13 @@ template <typename E>
return {}; // Invalid value or out of range.
}
template<typename ... Es>
[[nodiscard]] constexpr size_t enum_fuse(Es ... enums) {
static_assert(sizeof...(Es) >= 2, "magic_enum::enum_fuse requires at least 2 enums");
// Add 1 to prevent matching 2D fusions with 3D fusions etc.
return detail::cantor_pair((enum_index(enums).value() + 1)...);
}
// Checks whether enum contains enumerator with such enum value.
template <typename E>
[[nodiscard]] constexpr auto enum_contains(E value) noexcept -> detail::enable_if_enum_t<E, bool> {