1
0
Fork 0
mirror of https://github.com/Neargye/magic_enum.git synced 2026-01-10 23:44:29 +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>
@ -46,17 +45,24 @@
#include <utility>
#if defined(MAGIC_ENUM_CONFIG_FILE)
#include MAGIC_ENUM_CONFIG_FILE
# include MAGIC_ENUM_CONFIG_FILE
#endif
#if !defined(MAGIC_ENUM_USING_ALIAS_OPTIONAL)
#include <optional>
# include <optional>
#endif
#if !defined(MAGIC_ENUM_USING_ALIAS_STRING)
#include <string>
# include <string>
#endif
#if !defined(MAGIC_ENUM_USING_ALIAS_STRING_VIEW)
#include <string_view>
# 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__)
@ -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.