From cf963cba28fcdbdc124337e89ed1791f6be3b932 Mon Sep 17 00:00:00 2001 From: neargye Date: Mon, 12 Jun 2023 17:44:50 +0400 Subject: [PATCH] add MAGIC_ENUM_NO_EXCEPTION --- include/magic_enum_containers.hpp | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/include/magic_enum_containers.hpp b/include/magic_enum_containers.hpp index dcd7012..6f3156b 100644 --- a/include/magic_enum_containers.hpp +++ b/include/magic_enum_containers.hpp @@ -33,10 +33,16 @@ #ifndef NEARGYE_MAGIC_ENUM_CONTAINERS_HPP #define NEARGYE_MAGIC_ENUM_CONTAINERS_HPP -#include - #include "magic_enum.hpp" +#if !defined(MAGIC_ENUM_NO_EXCEPTION) && (defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)) +# include +# define MAGIC_ENUM_THROW(...) throw (__VA_ARGS__) +#else +# include +# define MAGIC_ENUM_THROW(...) std::abort() +#endif + namespace magic_enum::containers { namespace detail { @@ -335,14 +341,14 @@ struct array { if (auto index = index_type{}(pos)) { return a[*index]; } - throw std::out_of_range("enum array::at: unrecognized position"); + MAGIC_ENUM_THROW(std::out_of_range("enum array::at: unrecognized position")); } constexpr const_reference at(E pos) const { if (auto index = index_type{}(pos)) { return a[*index]; } - throw std::out_of_range("enum array::at: unrecognized position"); + MAGIC_ENUM_THROW(std::out_of_range("enum array::at: unrecognized position")); } [[nodiscard]] constexpr reference operator[](E pos) noexcept { return a[*index_type{}(pos)]; } @@ -517,7 +523,7 @@ class bitset { for (std::size_t i = 0; i < size(); ++i, flag <<= 1) { if (const_reference{this, i}) { if (i >= sizeof(T) * 8) { - throw std::overflow_error("cannot represent enum in this type"); + MAGIC_ENUM_THROW(std::overflow_error("cannot represent enum in this type")); } res |= flag; } @@ -538,7 +544,7 @@ class bitset { for (std::size_t i = 0; i < (sizeof(val) * 8); ++i, bit <<= 1) { if ((val & bit) > 0) { if (i >= enum_count()) { - throw std::out_of_range("enum bitset::constructor: Upper bit set in raw number"); + MAGIC_ENUM_THROW(std::out_of_range("enum bitset::constructor: Upper bit set in raw number")); } reference{this, i} = true; @@ -586,7 +592,7 @@ class bitset { } } if (u != 0) { - throw std::invalid_argument("enum bitset::constructor: unrecognized enum value in flag"); + MAGIC_ENUM_THROW(std::invalid_argument("enum bitset::constructor: unrecognized enum value in flag")); } } @@ -596,14 +602,14 @@ class bitset { if (auto v = enum_cast(sv.substr(0, to), cmp)) { set(v); } else { - throw std::invalid_argument("enum bitset::constructor: unrecognized enum value in string"); + MAGIC_ENUM_THROW(std::invalid_argument("enum bitset::constructor: unrecognized enum value in string")); } } if (!sv.empty()) { if (auto v = enum_cast(sv, cmp)) { set(v); } else { - throw std::invalid_argument("enum bitset::constructor: unrecognized enum value in string"); + MAGIC_ENUM_THROW(std::invalid_argument("enum bitset::constructor: unrecognized enum value in string")); } } } @@ -620,7 +626,7 @@ class bitset { if (auto ix = index_type{}(pos)) { return static_cast(const_reference(this, *ix)); } - throw std::out_of_range("enum bitset::test: unrecognized position"); + MAGIC_ENUM_THROW(std::out_of_range("enum bitset::test: unrecognized position")); } [[nodiscard]] constexpr bool all() const noexcept { @@ -712,7 +718,7 @@ class bitset { reference{this, *ix} = value; return *this; } - throw std::out_of_range("enum bitset::set: unrecognized position"); + MAGIC_ENUM_THROW(std::out_of_range("enum bitset::set: unrecognized position")); } constexpr bitset& reset() noexcept { return *this = bitset{}; } @@ -722,7 +728,7 @@ class bitset { reference{this, *ix} = false; return *this; } - throw std::out_of_range("enum bitset::reset: unrecognized position"); + MAGIC_ENUM_THROW(std::out_of_range("enum bitset::reset: unrecognized position")); } constexpr bitset& flip() noexcept { return *this = ~*this; }