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

Change contains_value to enum_contains (#31)

* change contains_value to enum_contains

* update test

* update doc
This commit is contained in:
Daniil Goncharov 2020-03-13 18:10:09 +05:00 committed by GitHub
parent aa24461613
commit a76480629c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 179 additions and 57 deletions

View file

@ -9,6 +9,7 @@
* [`enum_names` obtains string enum name sequence.](#enum_names)
* [`enum_entries` obtains pair (value enum, string enum name) sequence.](#enum_entries)
* [`enum_index` obtains index in enum value sequence from enum value.](#enum_index)
* [`enum_contains` checks whether enum contains enumerator with such value.](#enum_contains)
* [`is_unscoped_enum` checks whether type is an Unscoped enumeration.](#is_unscoped_enum)
* [`is_scoped_enum` checks whether type is an Scoped enumeration.](#is_scoped_enum)
* [`underlying_type` improved UB-free "SFINAE-friendly" std::underlying_type.](#underlying_type)
@ -43,7 +44,7 @@ constexpr optional<E> enum_cast(underlying_type_t<E> value) noexcept;
* String to enum value.
```cpp
std::string color_name{"GREEN"};
string color_name{"GREEN"};
auto color = magic_enum::enum_cast<Color>(color_name);
if (color.has_value()) {
// color.value() -> Color::GREEN
@ -108,7 +109,7 @@ constexpr size_t enum_count() noexcept;
* Examples
```cpp
constexpr std::size_t color_count = magic_enum::enum_count<Color>();
constexpr auto color_count = magic_enum::enum_count<Color>();
// color_count -> 3
```
@ -198,6 +199,48 @@ constexpr array<pair<E, string_view>, N> enum_entries() noexcept;
// color_entries[0].second -> "RED"
```
## `enum_index`
```cpp
template <typename E>
constexpr optional<size_t> enum_index() noexcept;
```
* Obtains index in enum value sequence from enum value.
* Returns `std::optional<std::size_t>` with index.
* Examples
```cpp
constexpr auto color_index = magic_enum::enum_index(Color::BLUE);
// color_index -> color_index.value() -> 1
// color_index -> color_index.has_value() -> true
```
## `enum_contains`
```cpp
template <typename E>
constexpr bool enum_contains(E value) noexcept;
constexpr bool enum_contains(underlying_type_t<E> value) noexcept;
constexpr bool enum_contains(string_view value) noexcept;
```
* Checks whether enum contains enumerator with such value.
* Returns true is enum contains value, otherwise false.
* Examples
```cpp
magic_enum::enum_contains(Color::GREEN); // -> true
magic_enum::enum_contains<Color>(2); // -> true
magic_enum::enum_contains<Color>(123); // -> false
magic_enum::enum_contains<Color>("GREEN"); // -> true
magic_enum::enum_contains<Color>("fda"); // -> false
```
## `is_unscoped_enum`
```cpp