| 1327 | // Returns optional with enum value. |
| 1328 | template <typename E, detail::enum_subtype S = detail::subtype_v<E>> |
| 1329 | [[nodiscard]] constexpr auto enum_cast(underlying_type_t<E> value) noexcept -> detail::enable_if_t<E, optional<std::decay_t<E>>> { |
| 1330 | using D = std::decay_t<E>; |
| 1331 | static_assert(detail::is_reflected_v<D, S>, "magic_enum requires enum implementation and valid max and min."); |
| 1332 | |
| 1333 | if constexpr (detail::is_sparse_v<D, S> || (S == detail::enum_subtype::flags)) { |
| 1334 | #if defined(MAGIC_ENUM_ENABLE_HASH) |
| 1335 | return detail::constexpr_switch<&detail::values_v<D, S>, detail::case_call_t::value>( |
| 1336 | [](D v) { return optional<D>{v}; }, |
| 1337 | static_cast<D>(value), |
| 1338 | detail::default_result_type_lambda<optional<D>>); |
| 1339 | #else |
| 1340 | for (std::size_t i = 0; i < detail::count_v<D, S>; ++i) { |
| 1341 | if (value == static_cast<underlying_type_t<D>>(enum_value<D, S>(i))) { |
| 1342 | return static_cast<D>(value); |
| 1343 | } |
| 1344 | } |
| 1345 | return {}; // Invalid value or out of range. |
| 1346 | #endif |
| 1347 | } else { |
| 1348 | if (value >= detail::min_v<D, S> && value <= detail::max_v<D, S>) { |
| 1349 | return static_cast<D>(value); |
| 1350 | } |
| 1351 | return {}; // Invalid value or out of range. |
| 1352 | } |
| 1353 | } |
| 1354 | |
| 1355 | // Obtains enum value from name. |
| 1356 | // Returns optional with enum value. |
nothing calls this directly
no test coverage detected