BLI: Add MACRO for enum usage in C++

This is handy to add support for enums used in both C and C++
files. This removes the need to typecast each time for every
operation.

Only support bitwise operators for now.
This commit is contained in:
Clément Foucault
2020-07-25 18:19:49 +02:00
parent a77175c9b6
commit 13afc81f6d

View File

@@ -754,6 +754,43 @@ extern bool BLI_memory_is_zero(const void *arr, const size_t arr_size);
/** \} */
/* -------------------------------------------------------------------- */
/** \name C++ Macros
* \{ */
#ifdef __cplusplus
/* Useful to port C code using enums to C++ where enums are strongly typed.
* To use after the enum declaration. */
# define ENUM_OPERATORS(_enum_type) \
inline constexpr _enum_type operator|(_enum_type a, _enum_type b) \
{ \
return a = static_cast<_enum_type>(static_cast<int>(a) | b); \
} \
inline constexpr _enum_type operator&(_enum_type a, _enum_type b) \
{ \
return a = static_cast<_enum_type>(static_cast<int>(a) & b); \
} \
inline constexpr _enum_type operator~(_enum_type a) \
{ \
return a = static_cast<_enum_type>(~static_cast<int>(a)); \
} \
inline _enum_type &operator|=(_enum_type &a, _enum_type b) \
{ \
return a = static_cast<_enum_type>(static_cast<int>(a) | b); \
} \
inline _enum_type &operator&=(_enum_type &a, _enum_type b) \
{ \
return a = static_cast<_enum_type>(static_cast<int>(a) & b); \
}
#else
/* Output nothing. */
# define ENUM_OPERATORS(_type)
#endif
/** \} */
/* -------------------------------------------------------------------- */
/** \name Misc Macros
* \{ */