diff --git a/CHANGELOG.md b/CHANGELOG.md index 0126e2b..12f4794 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. ## Next Release +- introduce `MSTD_ENUM_BITFLAG` for bitwise operations on enums + ## [0.0.3](https://github.com/repo/owner/releases/tag/0.0.3) - 2026-02-08 diff --git a/include/mstd/enum.hpp b/include/mstd/enum.hpp index e2cb3c3..ba55f88 100644 --- a/include/mstd/enum.hpp +++ b/include/mstd/enum.hpp @@ -27,6 +27,7 @@ #include // IWYU pragma: keep #include // IWYU pragma: keep #include // IWYU pragma: keep +#include // IWYU pragma: keep #include // IWYU pragma: keep #include // IWYU pragma: keep @@ -84,6 +85,14 @@ return {}; \ } \ \ + static constexpr std::string toString(EnumName e) \ + { \ + for (std::size_t i = 0; i < size; ++i) \ + if (values[i] == e) \ + return std::string(names[i]); \ + return {}; \ + } \ + \ static constexpr std::optional from_string( \ std::string_view s \ ) \ @@ -110,4 +119,21 @@ \ static constexpr EnumName##Meta enum_meta(EnumName) { return {}; } +#define MSTD_ENUM_BITFLAG(EnumName, Underlying, LIST) \ + MSTD_ENUM(EnumName, Underlying, LIST) \ + \ + inline EnumName operator|(EnumName lhs, EnumName rhs) \ + { \ + return static_cast( \ + static_cast(lhs) | static_cast(rhs) \ + ); \ + } \ + \ + inline EnumName operator&(EnumName lhs, EnumName rhs) \ + { \ + return static_cast( \ + static_cast(lhs) & static_cast(rhs) \ + ); \ + } + #endif // __MSTD__ENUM_HPP__ \ No newline at end of file