From 596f00c0db4977f5a6bcc6314c7d4dcc75c5deb2 Mon Sep 17 00:00:00 2001 From: ilobilo <68286835+ilobilo@users.noreply.github.com> Date: Fri, 4 Nov 2022 23:46:37 +0400 Subject: [PATCH] make streams and exceptions optional (#218) * magic_enum: make streams optional this commit adds support to disable streams with macro MAGIC_ENUM_NO_STREAMS * magic_enum_format: add support to disable exceptions this commit adds support to disable exceptions with macro MAGIC_ENUM_NO_EXCEPTIONS --- include/magic_enum.hpp | 8 +++++++- include/magic_enum_format.hpp | 10 ++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/include/magic_enum.hpp b/include/magic_enum.hpp index e984334..e6ee10c 100644 --- a/include/magic_enum.hpp +++ b/include/magic_enum.hpp @@ -40,7 +40,6 @@ #include #include #include -#include #include #include #include @@ -59,6 +58,9 @@ #if !defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW) #include #endif +#if !defined(MAGIC_ENUM_NO_STREAMS) +#include +#endif #if defined(__clang__) # pragma clang diagnostic push @@ -1381,6 +1383,8 @@ constexpr auto enum_for_each(Lambda&& lambda) { } } +#if !defined(MAGIC_ENUM_NO_STREAMS) + namespace ostream_operators { template = 0> @@ -1431,6 +1435,8 @@ using namespace istream_operators; } // namespace magic_enum::iostream_operators +#endif + namespace bitwise_operators { template = 0> diff --git a/include/magic_enum_format.hpp b/include/magic_enum_format.hpp index 1868c8a..c7968e2 100644 --- a/include/magic_enum_format.hpp +++ b/include/magic_enum_format.hpp @@ -43,8 +43,14 @@ # define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE #endif // MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT +#if !defined(MAGIC_ENUM_NO_EXCEPTIONS) && (defined(__cpp_exceptions) || defined(_EXCEPTIONS) || defined(_HAS_EXCEPTIONS)) +# define MAGIC_ENUM_THROW throw std::format_error +#else +# define MAGIC_ENUM_THROW std::terminate(); (void) +#endif + namespace magic_enum::customize { - // customize enum to enable/disable automatic std::format + // customize enum to enable/disable automatic std::format template constexpr bool enum_format_enabled() noexcept { return MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT; @@ -67,7 +73,7 @@ struct std::formatter && magic_enum::custo } } constexpr auto type_name = magic_enum::enum_type_name(); - throw std::format_error("Type of " + std::string{type_name.data(), type_name.size()} + " enum value: " + std::to_string(magic_enum::enum_integer(e)) + " is not exists."); + MAGIC_ENUM_THROW("Type of " + std::string{type_name.data(), type_name.size()} + " enum value: " + std::to_string(magic_enum::enum_integer(e)) + " is not exists."); } };