1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-09 23:34:23 +00:00

add MAGIC_ENUM_NO_ASSERT

This commit is contained in:
neargye 2023-06-12 17:43:19 +04:00
parent cf659c59c8
commit e7749da06b
7 changed files with 59 additions and 61 deletions

View file

@ -37,7 +37,6 @@
#define MAGIC_ENUM_VERSION_PATCH 2
#include <array>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <functional>
@ -59,6 +58,13 @@
# include <string_view>
#endif
#if defined(MAGIC_ENUM_NO_ASSERT)
# define MAGIC_ENUM_ASSERT(...) static_cast<void>(0)
#else
# include <cassert>
# define MAGIC_ENUM_ASSERT(...) assert((__VA_ARGS__))
#endif
#if defined(__clang__)
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunknown-warning-option"
@ -182,7 +188,7 @@ class customize_t : public std::pair<detail::customize_tag, string_view> {
constexpr customize_t(string_view srt) : std::pair<detail::customize_tag, string_view>{detail::customize_tag::custom_tag, srt} {}
constexpr customize_t(const char_type* srt) : customize_t{string_view{srt}} {}
constexpr customize_t(detail::customize_tag tag) : std::pair<detail::customize_tag, string_view>{tag, string_view{}} {
assert(tag != detail::customize_tag::custom_tag);
MAGIC_ENUM_ASSERT(tag != detail::customize_tag::custom_tag);
}
};
@ -248,11 +254,11 @@ template <std::uint16_t N>
class static_str {
public:
constexpr explicit static_str(str_view str) noexcept : static_str{str.str_, std::make_integer_sequence<std::uint16_t, N>{}} {
assert(str.size_ == N);
MAGIC_ENUM_ASSERT(str.size_ == N);
}
constexpr explicit static_str(string_view str) noexcept : static_str{str.data(), std::make_integer_sequence<std::uint16_t, N>{}} {
assert(str.size() == N);
MAGIC_ENUM_ASSERT(str.size() == N);
}
constexpr const char_type* data() const noexcept { return chars_; }
@ -387,7 +393,7 @@ constexpr I log2(I value) noexcept {
static_assert(std::is_integral_v<I>, "magic_enum::detail::log2 requires integral type.");
if constexpr (std::is_same_v<I, bool>) { // bool special case
return assert(false), value;
return MAGIC_ENUM_ASSERT(false), value;
} else {
auto ret = I{0};
for (; value > I{1}; value >>= I{1}, ++ret) {}
@ -1155,11 +1161,11 @@ template <typename E, detail::enum_subtype S = detail::subtype_v<E>>
using D = std::decay_t<E>;
if constexpr (detail::is_sparse_v<D, S>) {
return assert((index < detail::count_v<D, S>)), detail::values_v<D, S>[index];
return MAGIC_ENUM_ASSERT(index < detail::count_v<D, S>), detail::values_v<D, S>[index];
} else {
constexpr auto min = (S == detail::enum_subtype::flags) ? detail::log2(detail::min_v<D, S>) : detail::min_v<D, S>;
return assert((index < detail::count_v<D, S>)), detail::value<D, min, S>(index);
return MAGIC_ENUM_ASSERT(index < detail::count_v<D, S>), detail::value<D, min, S>(index);
}
}

View file

@ -398,7 +398,7 @@ struct array {
}
constexpr void swap(array& other) noexcept(std::is_nothrow_swappable_v<V>) {
for (std::size_t i{}; i < a.size(); ++i) {
for (std::size_t i = 0; i < a.size(); ++i) {
auto v = std::move(other.a[i]);
other.a[i] = std::move(a[i]);
a[i] = std::move(v);
@ -514,7 +514,7 @@ class bitset {
[[nodiscard]] constexpr T to_(detail::raw_access_t) const {
T res{};
T flag{1};
for (std::size_t i{}; i < size(); ++i, flag <<= 1) {
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");
@ -535,7 +535,7 @@ class bitset {
constexpr explicit bitset(detail::raw_access_t, unsigned long long val) : a{{}} {
unsigned long long bit{1};
for (std::size_t i{}; i < (sizeof(val) * 8); ++i, bit <<= 1) {
for (std::size_t i = 0; i < (sizeof(val) * 8); ++i, bit <<= 1) {
if ((val & bit) > 0) {
if (i >= enum_count<E>()) {
throw std::out_of_range("enum bitset::constructor: Upper bit set in raw number");
@ -548,21 +548,21 @@ class bitset {
constexpr explicit bitset(detail::raw_access_t, string_view sv, string_view::size_type pos = 0, string_view::size_type n = string_view::npos, char_type zero = static_cast<char_type>('0'), char_type one = static_cast<char_type>('1'))
: a{{}} {
std::size_t i{};
std::size_t i = 0;
for (auto c : sv.substr(pos, n)) {
if (c == one) {
if (i >= enum_count<E>()) {
throw std::out_of_range("enum bitset::constructor: Upper bit set in raw string");
MAGIC_ENUM_THROW(std::out_of_range("enum bitset::constructor: Upper bit set in raw string"));
}
reference{this, i} = true;
} else if (c != zero) {
throw std::invalid_argument("enum bitset::constructor: unrecognized character in raw string");
MAGIC_ENUM_THROW(std::invalid_argument("enum bitset::constructor: unrecognized character in raw string"));
}
++i;
}
}
constexpr explicit bitset(detail::raw_access_t, const char_type* str, std::size_t n = ~std::size_t{}, char_type zero = static_cast<char_type>('0'), char_type one = static_cast<char_type>('1'))
constexpr explicit bitset(detail::raw_access_t, const char_type* str, std::size_t n = ~std::size_t{0}, char_type zero = static_cast<char_type>('0'), char_type one = static_cast<char_type>('1'))
: bitset(string_view{str, (std::min)(std::char_traits<char_type>::length(str), n)}, 0, n, zero, one) {}
constexpr bitset(std::initializer_list<E> starters) : a{{}} {
@ -592,7 +592,7 @@ class bitset {
template <typename Cmp = std::equal_to<>>
constexpr explicit bitset(string_view sv, Cmp&& cmp = {}, char_type sep = static_cast<char_type>('|')) {
for (std::size_t to{}; (to = magic_enum::detail::find(sv, sep)) != string_view::npos; sv.remove_prefix(to + 1)) {
for (std::size_t to = 0; (to = magic_enum::detail::find(sv, sep)) != string_view::npos; sv.remove_prefix(to + 1)) {
if (auto v = enum_cast<E>(sv.substr(0, to), cmp)) {
set(v);
} else {
@ -628,7 +628,7 @@ class bitset {
return true;
}
for (std::size_t i{}; i < base_type_count - (not_interested > 0); ++i) {
for (std::size_t i = 0; i < base_type_count - (not_interested > 0); ++i) {
auto check = ~a[i];
if (check) {
return false;
@ -652,7 +652,7 @@ class bitset {
[[nodiscard]] constexpr bool none() const noexcept { return !any(); }
[[nodiscard]] constexpr std::size_t count() const noexcept {
std::size_t c{};
std::size_t c = 0;
for (auto& v : a) {
c += detail::popcount(v);
}
@ -664,21 +664,21 @@ class bitset {
[[nodiscard]] constexpr std::size_t max_size() const noexcept { return enum_count<E>(); }
constexpr bitset& operator&=(const bitset& other) noexcept {
for (std::size_t i{}; i < base_type_count; ++i) {
for (std::size_t i = 0; i < base_type_count; ++i) {
a[i] &= other.a[i];
}
return *this;
}
constexpr bitset& operator|=(const bitset& other) noexcept {
for (std::size_t i{}; i < base_type_count; ++i) {
for (std::size_t i = 0; i < base_type_count; ++i) {
a[i] |= other.a[i];
}
return *this;
}
constexpr bitset& operator^=(const bitset& other) noexcept {
for (std::size_t i{}; i < base_type_count; ++i) {
for (std::size_t i = 0; i < base_type_count; ++i) {
a[i] ^= other.a[i];
}
return *this;
@ -686,7 +686,7 @@ class bitset {
[[nodiscard]] constexpr bitset operator~() const noexcept {
bitset res;
for (std::size_t i{}; i < base_type_count - (not_interested > 0); ++i) {
for (std::size_t i = 0; i < base_type_count - (not_interested > 0); ++i) {
res.a[i] = ~a[i];
}
@ -697,8 +697,8 @@ class bitset {
}
constexpr bitset& set() noexcept {
for (std::size_t i{}; i < base_type_count - (not_interested > 0); ++i) {
a[i] = ~base_type{};
for (std::size_t i = 0; i < base_type_count - (not_interested > 0); ++i) {
a[i] = ~base_type{0};
}
if constexpr (not_interested > 0) {
@ -774,7 +774,7 @@ class bitset {
[[nodiscard]] string to_string(detail::raw_access_t, char_type zero = static_cast<char_type>('0'), char_type one = static_cast<char_type>('1')) const {
string name;
name.reserve(size());
for (std::size_t i{}; i < size(); ++i) {
for (std::size_t i = 0; i < size(); ++i) {
name.append(1, const_reference{this, i} ? one : zero);
}
return name;
@ -964,7 +964,7 @@ class set {
template <typename K, typename KC = key_compare>
constexpr std::enable_if_t<detail::is_transparent_v<KC>, size_type> erase(K&& x) noexcept {
size_type c{};
size_type c = 0;
for (auto [first, last] = detail::equal_range(index_type::values_v->begin(), index_type::values_v->end(), x, key_compare{}); first != last;) {
c += erase(*first++);
}
@ -981,7 +981,7 @@ class set {
template <typename K, typename KC = key_compare>
[[nodiscard]] constexpr std::enable_if_t<detail::is_transparent_v<KC>, size_type> count(const K& x) const {
size_type c{};
size_type c = 0;
for (auto [first, last] = detail::equal_range(index_type::values_v->begin(), index_type::values_v->end(), x, key_compare{}); first != last; ++first) {
c += count(*first);
}
@ -1091,7 +1091,7 @@ class set {
private:
container_type a;
std::size_t s{};
std::size_t s = 0;
};
template <typename V, int = 0>

View file

@ -84,7 +84,7 @@ template <detail::enum_subtype S, typename... Es>
#else
const auto fuse = detail::typesafe_fuse_enum<S, std::decay_t<Es>...>(values...);
#endif
return assert(fuse), fuse;
return MAGIC_ENUM_ASSERT(fuse), fuse;
}
// Returns a bijective mix of several enum values. This can be used to emulate 2D switch/case statements.

View file

@ -140,14 +140,14 @@ TEST_CASE("enum_cast") {
REQUIRE_FALSE(enum_cast<number>("four").has_value());
REQUIRE_FALSE(enum_cast<number>("None").has_value());
REQUIRE(magic_enum::enum_cast<crc_hack>("b5a7b602ab754d7ab30fb42c4fb28d82").has_value());
REQUIRE_FALSE(magic_enum::enum_cast<crc_hack>("d19f2e9e82d14b96be4fa12b8a27ee9f").has_value());
REQUIRE(enum_cast<crc_hack>("b5a7b602ab754d7ab30fb42c4fb28d82").has_value());
REQUIRE_FALSE(enum_cast<crc_hack>("d19f2e9e82d14b96be4fa12b8a27ee9f").has_value());
constexpr auto crc = magic_enum::enum_cast<crc_hack_2>("b5a7b602ab754d7ab30fb42c4fb28d82");
constexpr auto crc = enum_cast<crc_hack_2>("b5a7b602ab754d7ab30fb42c4fb28d82");
REQUIRE(crc.value() == crc_hack_2::b5a7b602ab754d7ab30fb42c4fb28d82);
REQUIRE(magic_enum::enum_cast<crc_hack_2>("d19f2e9e82d14b96be4fa12b8a27ee9f").value() == crc_hack_2::d19f2e9e82d14b96be4fa12b8a27ee9f);
REQUIRE(enum_cast<crc_hack_2>("d19f2e9e82d14b96be4fa12b8a27ee9f").value() == crc_hack_2::d19f2e9e82d14b96be4fa12b8a27ee9f);
REQUIRE(magic_enum::enum_cast<BoolTest>("Nay").has_value());
REQUIRE(enum_cast<BoolTest>("Nay").has_value());
}
SECTION("integer") {
@ -408,7 +408,7 @@ TEST_CASE("enum_value") {
}
TEST_CASE("enum_values") {
REQUIRE(std::is_same_v<decltype(magic_enum::enum_values<Color>()), const std::array<Color, 3>&>);
REQUIRE(std::is_same_v<decltype(enum_values<Color>()), const std::array<Color, 3>&>);
constexpr auto& s1 = enum_values<Color&>();
REQUIRE(s1 == std::array<Color, 3>{{Color::RED, Color::GREEN, Color::BLUE}});
@ -648,7 +648,7 @@ TEST_CASE("enum_name") {
}
TEST_CASE("enum_names") {
REQUIRE(std::is_same_v<decltype(magic_enum::enum_names<Color>()), const std::array<std::string_view, 3>&>);
REQUIRE(std::is_same_v<decltype(enum_names<Color>()), const std::array<std::string_view, 3>&>);
constexpr auto& s1 = enum_names<Color&>();
REQUIRE(s1 == std::array<std::string_view, 3>{{"red", "GREEN", "BLUE"}});
@ -664,7 +664,7 @@ TEST_CASE("enum_names") {
}
TEST_CASE("enum_entries") {
REQUIRE(std::is_same_v<decltype(magic_enum::enum_entries<Color>()), const std::array<std::pair<Color, std::string_view>, 3>&>);
REQUIRE(std::is_same_v<decltype(enum_entries<Color>()), const std::array<std::pair<Color, std::string_view>, 3>&>);
constexpr auto& s1 = enum_entries<Color&>();
REQUIRE(s1 == std::array<std::pair<Color, std::string_view>, 3>{{{Color::RED, "red"}, {Color::GREEN, "GREEN"}, {Color::BLUE, "BLUE"}}});
@ -879,7 +879,7 @@ TEST_CASE("extrema") {
};
REQUIRE(magic_enum::enum_name<BadColor>(BadColor::NONE).empty());
REQUIRE_FALSE(magic_enum::enum_cast<BadColor>(std::numeric_limits<std::uint64_t>::max()).has_value());
REQUIRE_FALSE(enum_cast<BadColor>(std::numeric_limits<std::uint64_t>::max()).has_value());
REQUIRE_FALSE(magic_enum::enum_contains<BadColor>(std::numeric_limits<std::uint64_t>::max()));
REQUIRE_FALSE(magic_enum::enum_contains<BadColor>(BadColor::NONE));
@ -1090,10 +1090,10 @@ TEST_CASE("constexpr_for") {
#endif
static int switch_case_2d(Color color, Directions direction) {
switch (magic_enum::enum_fuse(color, direction).value()) {
case magic_enum::enum_fuse(Color::RED, Directions::Up).value():
switch (enum_fuse(color, direction).value()) {
case enum_fuse(Color::RED, Directions::Up).value():
return 1;
case magic_enum::enum_fuse(Color::BLUE, Directions::Down).value():
case enum_fuse(Color::BLUE, Directions::Down).value():
return 2;
default:
return 0;
@ -1103,10 +1103,10 @@ static int switch_case_2d(Color color, Directions direction) {
enum class Index { zero = 0, one = 1, two = 2 };
static int switch_case_3d(Color color, Directions direction, Index index) {
switch (magic_enum::enum_fuse(color, direction, index).value()) {
case magic_enum::enum_fuse(Color::RED, Directions::Up, Index::zero).value():
switch (enum_fuse(color, direction, index).value()) {
case enum_fuse(Color::RED, Directions::Up, Index::zero).value():
return 1;
case magic_enum::enum_fuse(Color::BLUE, Directions::Up, Index::zero).value():
case enum_fuse(Color::BLUE, Directions::Up, Index::zero).value():
return 2;
default:
return 0;

View file

@ -295,14 +295,6 @@ TEST_CASE("containers_set") {
REQUIRE_FALSE(magic_enum::enum_count<Color>() == color_set_not_const.size());
}
TEST_CASE("containers_flat_set") {
// constexpr magic_enum::containers::flat_set color_flat_set_filled = {Color::RED, Color::GREEN, Color::BLUE};
// REQUIRE_FALSE(color_flat_set_filled.empty());
// REQUIRE(color_flat_set_filled.size() == 3);
// REQUIRE(magic_enum::enum_count<Color>() == color_flat_set_filled.size());
}
TEST_CASE("map_like_container") {
using namespace magic_enum::ostream_operators;

View file

@ -390,7 +390,7 @@ TEST_CASE("enum_value") {
}
TEST_CASE("enum_values") {
REQUIRE(std::is_same_v<decltype(magic_enum::enum_values<Color>()), const std::array<Color, 3>&>);
REQUIRE(std::is_same_v<decltype(enum_values<Color>()), const std::array<Color, 3>&>);
constexpr auto& s1 = enum_values<Color&>();
REQUIRE(s1 == std::array<Color, 3>{{Color::RED, Color::GREEN, Color::BLUE}});
@ -506,7 +506,7 @@ TEST_CASE("enum_flags_name") {
}
TEST_CASE("enum_names") {
REQUIRE(std::is_same_v<decltype(magic_enum::enum_names<Color>()), const std::array<std::string_view, 3>&>);
REQUIRE(std::is_same_v<decltype(enum_names<Color>()), const std::array<std::string_view, 3>&>);
constexpr auto& s1 = enum_names<Color&>();
REQUIRE(s1 == std::array<std::string_view, 3>{{"RED", "GREEN", "BLUE"}});
@ -522,7 +522,7 @@ TEST_CASE("enum_names") {
}
TEST_CASE("enum_entries") {
REQUIRE(std::is_same_v<decltype(magic_enum::enum_entries<Color>()), const std::array<std::pair<Color, std::string_view>, 3>&>);
REQUIRE(std::is_same_v<decltype(enum_entries<Color>()), const std::array<std::pair<Color, std::string_view>, 3>&>);
constexpr auto& s1 = enum_entries<Color&>();
REQUIRE(s1 == std::array<std::pair<Color, std::string_view>, 3>{{{Color::RED, "RED"}, {Color::GREEN, "GREEN"}, {Color::BLUE, "BLUE"}}});

View file

@ -68,7 +68,7 @@ TEST_CASE("enum_cast") {
}
TEST_CASE("enum_values") {
REQUIRE(std::is_same_v<decltype(magic_enum::enum_values<Color>()), const std::array<Color, 3>&>);
REQUIRE(std::is_same_v<decltype(enum_values<Color>()), const std::array<Color, 3>&>);
constexpr auto& s1 = enum_values<Color&>();
REQUIRE(s1 == std::array<Color, 3>{{Color::RED, Color::GREEN, Color::BLUE}});
@ -103,14 +103,14 @@ TEST_CASE("enum_name") {
}
TEST_CASE("enum_names") {
REQUIRE(std::is_same_v<decltype(magic_enum::enum_names<Color>()), const std::array<std::wstring_view, 3>&>);
REQUIRE(std::is_same_v<decltype(enum_names<Color>()), const std::array<std::wstring_view, 3>&>);
constexpr auto& s1 = enum_names<Color&>();
REQUIRE(s1 == std::array<std::wstring_view, 3>{{L"red", L"GREEN", L"BLUE"}});
}
TEST_CASE("enum_entries") {
REQUIRE(std::is_same_v<decltype(magic_enum::enum_entries<Color>()), const std::array<std::pair<Color, std::wstring_view>, 3>&>);
REQUIRE(std::is_same_v<decltype(enum_entries<Color>()), const std::array<std::pair<Color, std::wstring_view>, 3>&>);
constexpr auto& s1 = enum_entries<Color&>();
REQUIRE(s1 == std::array<std::pair<Color, std::wstring_view>, 3>{{{Color::RED, L"red"}, {Color::GREEN, L"GREEN"}, {Color::BLUE, L"BLUE"}}});