Skip to content

Commit

Permalink
update usage
Browse files Browse the repository at this point in the history
  • Loading branch information
mguludag committed Mar 12, 2024
1 parent e37c31f commit 6348531
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"files.associations": {
"cmath": "cpp"
"cmath": "cpp",
"string_view": "cpp"
}
}
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ Converting (scoped)enum values to/from string names written in C++>=11.
* Changing enum range with template parameter <sub>(default range: `[-128, 128)`)</sub> on each call or with your special function for types or adding specialized `enum_range<Enum>` struct
* Supports `operator<<` for direct using with ostream objects
* Supports custom enum name output by explicit specialization of `constexpr inline auto mgutility::detail::enum_type::name<Enum, EnumValue>() noexcept` function
* Supports iterate over enum (names and values) with `mgutility::enum_for_each<T>()` class and it is compatible with standard ranges and views

## Limitations
* Compiler versions
* Wider range can increase compile time so user responsible to adjusting for enum's range


## Usage ([try it!](https://godbolt.org/z/96fqYE1M7))
## Usage ([try it!](https://godbolt.org/z/ojoY7YvGG))
```C++
#include <iostream>
#include "enum_name.hpp"
Expand Down Expand Up @@ -51,11 +52,29 @@ int main() {
auto x = rgb_color::blue;
auto y = mgutility::to_enum<rgb_color>("greenn");

#ifdef __cpp_lib_ranges
#include <ranges>

// enum_for_each<T> can usable with views and range algorithms
auto colors = mgutility::enum_for_each<rgb_color>() |
std::ranges::views::filter(
[](auto &&pair) { return pair.name != "UNKNOWN"; });

std::ranges::for_each(colors, [](auto &&color) {
std::cout << color.name << " \t: " << color.to_underlying()
<< '\n';
});

#else

for (auto&& e : mgutility::enum_for_each<rgb_color>()) {
std::cout << e.name << " " << e.to_underlying() << '\n';
if(e.name != "UNKNOWN"){
std::cout << e.name << " \t: " << e.to_underlying() << '\n';
}
// enum_pair<Enum> has two data members: name and value
// to_underlying() converts into underlying type of enum
}
#endif

// default signature: enum_name<min_value = -128, max_value = 128, Enum
// typename>(Enum&&) Changing max_value to not too much greater than enum's
Expand Down

0 comments on commit 6348531

Please sign in to comment.