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

update doc

This commit is contained in:
neargye 2024-11-20 00:07:21 +02:00
parent 84990c60b2
commit 06741ec03f
2 changed files with 60 additions and 7 deletions

View file

@ -22,11 +22,24 @@ If you like this project, please consider donating to one of the funds that help
## [Features & Examples](example/)
* Enum value to string
* Basic
```cpp
#include <magic_enum/magic_enum.hpp>
//....
#include <iostream>
enum class Color : { RED = -10, BLUE = 0, GREEN = 10 };
int main() {
Color c1 = Color::RED;
std::cout << magic_enum::enum_name(c1) << std::endl; // RED
return 0;
}
```
* Enum value to string
```cpp
Color color = Color::RED;
auto color_name = magic_enum::enum_name(color);
// color_name -> "RED"

View file

@ -76,6 +76,8 @@ template <typename E, typename BinaryPredicate>
constexpr optional<E> enum_cast(string_view value, BinaryPredicate p) noexcept(is_nothrow_invocable_v<BinaryPredicate>);
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Obtains enum value from string or integer.
* Returns `optional<E>`, using `has_value()` to check contains enum value and `value()` to get the enum value.
@ -125,6 +127,8 @@ template <typename E, size_t I>
constexpr E enum_value() noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns enum value at specified index.
* `enum_value(value)` no bounds checking is performed: the behavior is undefined if `index >= number of enum values`.
* `enum_value<value>()` check if `I >= number of enum values`, occurs the compilation error `magic_enum::enum_value out of range`.
@ -149,6 +153,8 @@ template <typename E>
constexpr array<E, N> enum_values() noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns `array<E, N>` with all enum values where `N = number of enum values`, sorted by enum value.
* Examples
@ -166,6 +172,8 @@ template <typename E>
constexpr size_t enum_count() noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns number of enum values.
* Examples
@ -185,6 +193,8 @@ template <typename E>
constexpr underlying_type_t<E> enum_underlying(E value) noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns integer value from enum value.
* Examples
@ -205,6 +215,8 @@ template <auto V>
constexpr string_view enum_name() noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns name from enum value as `string_view` with null-terminated string.
* If enum value does not have name or [out of range](limitations.md), `enum_name(value)` returns empty string.
* If enum value does not have name, `enum_name<value>()` occurs the compilation error `magic_enum::enum_name enum value does not have a name`.
@ -232,6 +244,8 @@ template <typename E>
constexpr array<string_view, N> enum_names() noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns `array<string_view, N>` with all names where `N = number of enum values`, sorted by enum value.
* Examples
@ -249,6 +263,8 @@ template <typename E>
constexpr array<pair<E, string_view>, N> enum_entries() noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns `array<pair<E, string_view>, N>` with all pairs (value, name) where `N = number of enum values`, sorted by enum value.
* Examples
@ -270,6 +286,8 @@ template <auto V>
constexpr size_t enum_index() noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Obtains index in enum values from enum value.
* `enum_index(value)` returns `optional<size_t>` with index.
* `enum_index<value>()` returns index. If enum value does not have a index, occurs the compilation error `magic_enum::enum_index enum value does not have a index`.
@ -303,6 +321,8 @@ template <typename E, typename BinaryPredicate>
constexpr bool enum_contains(string_view value, BinaryPredicate p) noexcept(is_nothrow_invocable_v<BinaryPredicate>);
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Checks whether enum contains enumerator with such value.
* Returns true is enum contains value, otherwise false.
@ -327,6 +347,8 @@ template <typename E>
constexpr bool enum_reflected(underlying_type_t<E> value) noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns true if the enum value is in the range of values that can be reflected.
## `enum_type_name`
@ -336,6 +358,8 @@ template <typename E>
constexpr string_view enum_type_name() noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Returns type name of enum as `string_view` null-terminated string.
* Examples
@ -353,7 +377,7 @@ template <typename... Es>
constexpr optional<enum_fuse_t> enum_fuse(Es... values) noexcept;
```
* You should add the required file `<magic_enum_fuse.hpp>`.
* Defined in header `<magic_enum/magic_enum_fuse.hpp>`
* Returns a typesafe bijective mix of several enum values. This can be used to emulate 2D switch/case statements.
@ -384,7 +408,7 @@ template <typename Result, typename E, typename Lambda>
constexpr Result enum_switch(Lambda&& lambda, E value, Result&& result);
```
* You should add the required file `<magic_enum_switch.hpp>`.
* Defined in header `<magic_enum/magic_enum_switch.hpp>`
* Examples
@ -404,6 +428,8 @@ template <typename E, typename Lambda>
constexpr auto enum_for_each(Lambda&& lambda);
```
* Defined in header `<magic_enum/magic_enum_utility.hpp>`
* Examples
```cpp
@ -442,7 +468,7 @@ template <typename E, typename BinaryPredicate>
constexpr bool enum_flags_contains(string_view value, BinaryPredicate p) noexcept(is_nothrow_invocable_v<BinaryPredicate>);
```
* You should add the required file `<magic_enum_flags.hpp>`.
* Defined in header `<magic_enum/magic_enum_flags.hpp>`
* For enum-flags add `is_flags` to specialization `enum_range` for necessary enum type. Specialization of `enum_range` must be injected in `namespace magic_enum::customize`.
```cpp
@ -496,6 +522,8 @@ template <typename T>
inline constexpr bool is_unscoped_enum_v = is_unscoped_enum<T>::value;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Checks whether type is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration).
* Provides the member constant value which is equal to true, if T is an [Unscoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Unscoped_enumeration) type.</br>
@ -521,6 +549,8 @@ template <typename T>
inline constexpr bool is_scoped_enum_v = is_scoped_enum<T>::value;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Checks whether type is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations).
* Provides the member constant value which is equal to true, if T is an [Scoped enumeration](https://en.cppreference.com/w/cpp/language/enum#Scoped_enumerations) type.</br>
@ -546,6 +576,8 @@ template <typename T>
using underlying_type_t = typename underlying_type<T>::type;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Improved UB-free "SFINAE-friendly" [underlying_type](https://en.cppreference.com/w/cpp/types/underlying_type).
* If T is a complete enumeration type, provides a member typedef type that names the underlying type of T.</br>
@ -571,7 +603,7 @@ template <typename Char, typename Traits, typename E>
basic_ostream<Char, Traits>& operator<<(basic_ostream<Char, Traits>& os, optional<E> value);
```
* You should add the required file `<magic_enum_iostream.hpp>`.
* Defined in header `<magic_enum/magic_enum_iostream.hpp>`
* Out-of-the-box ostream operators for all enums.
@ -590,7 +622,7 @@ template <typename Char, typename Traits, typename E>
basic_istream<Char, Traits>& operator>>(basic_istream<Char, Traits>& is, E& value);
```
* You should add the required file `<magic_enum_iostream.hpp>`.
* Defined in header `<magic_enum/magic_enum_iostream.hpp>`
* Out-of-the-box istream operators for all enums.
@ -627,6 +659,8 @@ template <typename E>
constexpr E& operator^=(E& lhs, E rhs) noexcept;
```
* Defined in header `<magic_enum/magic_enum.hpp>`
* Out-of-the-box bitwise operators for all enums.
* Examples
@ -712,6 +746,8 @@ struct array {
}
```
* Defined in header `<magic_enum/magic_enum_containers.hpp>`
* STL like array for all enums.
* Examples
@ -824,6 +860,8 @@ class bitset {
}
```
* Defined in header `<magic_enum/magic_enum_containers.hpp>`
* STL like bitset for all enums.
* Examples
@ -975,6 +1013,8 @@ class set {
}
```
* Defined in header `<magic_enum/magic_enum_containers.hpp>`
* STL like set for all enums.
* Examples