Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ enum flub { blub, glub };
static_assert(!wise_enum::is_wise_enum_v<flub>, "");
```

Check if a value defined as valid enumerator:


```cpp
static_assert(wise_enum::verify_enum(my_lib::Color::RED));
static_assert(!wise_enum::verify_enum(static_cast<my_lib::Color>(10)));
```

### Design

It has a few notable design choices.
Expand Down
3 changes: 3 additions & 0 deletions examples/example.x.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,8 @@ int main() {
o.reset();
assert(!o);

static_assert(wise_enum::verify_enum(my_lib::Color::RED));
static_assert(!wise_enum::verify_enum(static_cast<my_lib::Color>(10)));

return 0;
}
6 changes: 6 additions & 0 deletions wise_enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ constexpr string_type to_string(T t) {
return wise_enum_detail_to_string(t, detail::Tag<T>{});
}

// returns if enumerator is valid enumerator value
template <class T>
constexpr bool verify_enum(T t) {
return wise_enum_detail_verify(t, detail::Tag<T>{});
}

// Enumerators trait class. Each value is also available as a template variable
// for C++14 and on
template <class T>
Expand Down
15 changes: 15 additions & 0 deletions wise_enum_detail.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ WISE_ENUM_CONSTEXPR_14 bool compare(U u1, U u2) {
case name::WISE_ENUM_IMPL_ONLY_OR_FIRST(x): \
return WISE_ENUM_IMPL_ENUM_STR(x);

#define WISE_ENUM_IMPL_VERIFY_CASE(name, x) \
case name::WISE_ENUM_IMPL_ONLY_OR_FIRST(x): \
return true;

#define WISE_ENUM_IMPL_STORAGE_2(x, y) y

#define WISE_ENUM_IMPL_STORAGE(x) \
Expand Down Expand Up @@ -205,4 +209,15 @@ WISE_ENUM_CONSTEXPR_14 bool compare(U u1, U u2) {
WISE_ENUM_IMPL_NOTHING, __VA_ARGS__)) \
} \
return {}; \
} \
template <class T> \
friendly WISE_ENUM_CONSTEXPR_14 bool \
wise_enum_detail_verify(T e, ::wise_enum::detail::Tag<name>) { \
switch (e) { \
WISE_ENUM_IMPL_EXPAND(loop(WISE_ENUM_IMPL_VERIFY_CASE, name, \
WISE_ENUM_IMPL_NOTHING, __VA_ARGS__)) \
} \
return false; \
}