Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<!-- insertion marker -->
## [0.0.3](https://github.com/repo/owner/releases/tag/0.0.3) - 2026-02-08

Expand Down
26 changes: 26 additions & 0 deletions include/mstd/enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <cstddef> // IWYU pragma: keep
#include <optional> // IWYU pragma: keep
#include <span> // IWYU pragma: keep
#include <string> // IWYU pragma: keep
#include <string_view> // IWYU pragma: keep
#include <type_traits> // IWYU pragma: keep

Expand Down Expand Up @@ -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<EnumName> from_string( \
std::string_view s \
) \
Expand All @@ -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<EnumName>( \
static_cast<Underlying>(lhs) | static_cast<Underlying>(rhs) \
); \
} \
\
inline EnumName operator&(EnumName lhs, EnumName rhs) \
{ \
return static_cast<EnumName>( \
static_cast<Underlying>(lhs) & static_cast<Underlying>(rhs) \
); \
}

#endif // __MSTD__ENUM_HPP__