mirror of
https://github.com/Neargye/magic_enum.git
synced 2026-01-09 23:34:23 +00:00
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
This commit is contained in:
parent
159a35006d
commit
596f00c0db
2 changed files with 15 additions and 3 deletions
|
|
@ -40,7 +40,6 @@
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <iosfwd>
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
@ -59,6 +58,9 @@
|
||||||
#if !defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW)
|
#if !defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW)
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
#endif
|
#endif
|
||||||
|
#if !defined(MAGIC_ENUM_NO_STREAMS)
|
||||||
|
#include <iosfwd>
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(__clang__)
|
#if defined(__clang__)
|
||||||
# pragma clang diagnostic push
|
# pragma clang diagnostic push
|
||||||
|
|
@ -1381,6 +1383,8 @@ constexpr auto enum_for_each(Lambda&& lambda) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if !defined(MAGIC_ENUM_NO_STREAMS)
|
||||||
|
|
||||||
namespace ostream_operators {
|
namespace ostream_operators {
|
||||||
|
|
||||||
template <typename Char, typename Traits, typename E, detail::enable_if_t<E, int> = 0>
|
template <typename Char, typename Traits, typename E, detail::enable_if_t<E, int> = 0>
|
||||||
|
|
@ -1431,6 +1435,8 @@ using namespace istream_operators;
|
||||||
|
|
||||||
} // namespace magic_enum::iostream_operators
|
} // namespace magic_enum::iostream_operators
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace bitwise_operators {
|
namespace bitwise_operators {
|
||||||
|
|
||||||
template <typename E, detail::enable_if_t<E, int> = 0>
|
template <typename E, detail::enable_if_t<E, int> = 0>
|
||||||
|
|
|
||||||
|
|
@ -43,8 +43,14 @@
|
||||||
# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE
|
# define MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT_AUTO_DEFINE
|
||||||
#endif // MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT
|
#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 {
|
namespace magic_enum::customize {
|
||||||
// customize enum to enable/disable automatic std::format
|
// customize enum to enable/disable automatic std::format
|
||||||
template <typename E>
|
template <typename E>
|
||||||
constexpr bool enum_format_enabled() noexcept {
|
constexpr bool enum_format_enabled() noexcept {
|
||||||
return MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT;
|
return MAGIC_ENUM_DEFAULT_ENABLE_ENUM_FORMAT;
|
||||||
|
|
@ -67,7 +73,7 @@ struct std::formatter<E, std::enable_if_t<std::is_enum_v<E> && magic_enum::custo
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
constexpr auto type_name = magic_enum::enum_type_name<E>();
|
constexpr auto type_name = magic_enum::enum_type_name<E>();
|
||||||
throw std::format_error("Type of " + std::string{type_name.data(), type_name.size()} + " enum value: " + std::to_string(magic_enum::enum_integer<D>(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<D>(e)) + " is not exists.");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue