diff --git a/.vscode/settings.json b/.vscode/settings.json index 1be854f..abcce64 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,6 @@ { "files.associations": { - "cmath": "cpp" + "cmath": "cpp", + "string_view": "cpp" } } \ No newline at end of file diff --git a/README.md b/README.md index f4c27a4..e6d24f5 100644 --- a/README.md +++ b/README.md @@ -13,13 +13,14 @@ Converting (scoped)enum values to/from string names written in C++>=11. * Changing enum range with template parameter (default range: `[-128, 128)`) on each call or with your special function for types or adding specialized `enum_range` 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() noexcept` function +* Supports iterate over enum (names and values) with `mgutility::enum_for_each()` 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/9MP9c91vb)) ```C++ #include #include "enum_name.hpp" @@ -51,11 +52,29 @@ int main() { auto x = rgb_color::blue; auto y = mgutility::to_enum("greenn"); +#ifdef __cpp_lib_ranges +#include + +// enum_for_each can usable with views and range algorithms +auto colors = mgutility::enum_for_each() | + 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()) { - std::cout << e.name << " " << e.to_underlying() << '\n'; + if(e.name != "UNKNOWN"){ + std::cout << e.name << " \t: " << e.to_underlying() << '\n'; + } // enum_pair has two data members: name and value // to_underlying() converts into underlying type of enum } +#endif // default signature: enum_name(Enum&&) Changing max_value to not too much greater than enum's