Skip to content

Commit

Permalink
fix enum_fuse
Browse files Browse the repository at this point in the history
  • Loading branch information
Neargye committed Dec 9, 2023
1 parent 804738b commit 41f674b
Showing 1 changed file with 12 additions and 21 deletions.
33 changes: 12 additions & 21 deletions include/magic_enum/magic_enum_fuse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,30 @@ namespace magic_enum {

namespace detail {

template <typename E, typename... Es>
inline constexpr auto subtypes_v = subtype_v<E>;

template <enum_subtype S, typename E>
template <typename E>
constexpr optional<std::uintmax_t> fuse_one_enum(optional<std::uintmax_t> hash, E value) noexcept {
if (hash) {
if (const auto index = enum_index(value)) {
return (*hash << log2(enum_count<E, S>() + 1)) | *index;
return (*hash << log2(enum_count<E>() + 1)) | *index;
}
}
return {};
}

template <enum_subtype S, typename E>
template <typename E>
constexpr optional<std::uintmax_t> fuse_enum(E value) noexcept {
return fuse_one_enum<S>(0, value);
return fuse_one_enum(0, value);
}

template <enum_subtype S, typename E, typename... Es>
template <typename E, typename... Es>
constexpr optional<std::uintmax_t> fuse_enum(E head, Es... tail) noexcept {
return fuse_one_enum<S>(fuse_enum<S>(tail...), head);
return fuse_one_enum(fuse_enum(tail...), head);
}

template <enum_subtype S, typename... Es>
template <typename... Es>
constexpr auto typesafe_fuse_enum(Es... values) noexcept {
enum class enum_fuse_t : std::uintmax_t;
const auto fuse = fuse_enum<S>(values...);
const auto fuse = fuse_enum(values...);
if (fuse) {
return optional<enum_fuse_t>{static_cast<enum_fuse_t>(*fuse)};
}
Expand All @@ -74,25 +71,19 @@ constexpr auto typesafe_fuse_enum(Es... values) noexcept {
} // namespace magic_enum::detail

// Returns a bijective mix of several enum values. This can be used to emulate 2D switch/case statements.
template <detail::enum_subtype S, typename... Es>
template <typename... Es>
[[nodiscard]] constexpr auto enum_fuse(Es... values) noexcept {
static_assert((std::is_enum_v<std::decay_t<Es>> && ...), "magic_enum::enum_fuse requires enum type.");
static_assert(sizeof...(Es) >= 2, "magic_enum::enum_fuse requires at least 2 values.");
static_assert((detail::log2(enum_count<std::decay_t<Es>, S>() + 1) + ...) <= (sizeof(std::uintmax_t) * 8), "magic_enum::enum_fuse does not work for large enums");
static_assert((detail::log2(enum_count<std::decay_t<Es>>() + 1) + ...) <= (sizeof(std::uintmax_t) * 8), "magic_enum::enum_fuse does not work for large enums");
#if defined(MAGIC_ENUM_NO_TYPESAFE_ENUM_FUSE)
const auto fuse = detail::fuse_enum<S, std::decay_t<Es>...>(values...);
const auto fuse = detail::fuse_enum<std::decay_t<Es>...>(values...);
#else
const auto fuse = detail::typesafe_fuse_enum<S, std::decay_t<Es>...>(values...);
const auto fuse = detail::typesafe_fuse_enum<std::decay_t<Es>...>(values...);
#endif
return MAGIC_ENUM_ASSERT(fuse), fuse;
}

// Returns a bijective mix of several enum values. This can be used to emulate 2D switch/case statements.
template <typename... Es>
[[nodiscard]] constexpr auto enum_fuse(Es... values) noexcept {
return enum_fuse<detail::subtypes_v<std::decay_t<Es>...>>(values...);
}

} // namespace magic_enum

#endif // NEARGYE_MAGIC_ENUM_FUSE_HPP

0 comments on commit 41f674b

Please sign in to comment.