util: add support for defining bitwise operators on strongly typed enums

Reviewed-by: Jesse Natalie <jenatali@microsoft.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/10784>
This commit is contained in:
Tony Wasserka 2021-05-13 11:21:16 +02:00 committed by Marge Bot
parent df5b14969f
commit c0f4bb9a22
1 changed files with 10 additions and 3 deletions

View File

@ -23,20 +23,26 @@
#ifdef __cplusplus
#include <type_traits>
// some enum helpers
#define MESA_DEFINE_CPP_ENUM_BINARY_OPERATOR(Enum, op) \
extern "C++" { \
static inline \
Enum operator op (Enum a, Enum b) \
{ \
uint64_t ua = a, ub = b; \
using IntType = std::underlying_type_t<Enum>; \
IntType ua = static_cast<IntType>(a); \
IntType ub = static_cast<IntType>(b); \
return static_cast<Enum>(ua op ub); \
} \
\
static inline \
Enum& operator op##= (Enum &a, Enum b) \
{ \
uint64_t ua = a, ub = b; \
using IntType = std::underlying_type_t<Enum>; \
IntType ua = static_cast<IntType>(a); \
IntType ub = static_cast<IntType>(b); \
ua op##= ub; \
a = static_cast<Enum>(ua); \
return a; \
@ -48,7 +54,8 @@ extern "C++" { \
static inline \
Enum operator op (Enum a) \
{ \
uint64_t ua = a; \
using IntType = std::underlying_type_t<Enum>; \
IntType ua = static_cast<IntType>(a); \
return static_cast<Enum>(op ua); \
} \
}