From 13afc81f6da69e36b3d503fada0760dc07f607dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Foucault?= Date: Sat, 25 Jul 2020 18:19:49 +0200 Subject: 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. --- source/blender/blenlib/BLI_utildefines.h | 37 ++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'source/blender/blenlib/BLI_utildefines.h') diff --git a/source/blender/blenlib/BLI_utildefines.h b/source/blender/blenlib/BLI_utildefines.h index bc35e969e6a..2699f2498ac 100644 --- a/source/blender/blenlib/BLI_utildefines.h +++ b/source/blender/blenlib/BLI_utildefines.h @@ -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(a) | b); \ + } \ + inline constexpr _enum_type operator&(_enum_type a, _enum_type b) \ + { \ + return a = static_cast<_enum_type>(static_cast(a) & b); \ + } \ + inline constexpr _enum_type operator~(_enum_type a) \ + { \ + return a = static_cast<_enum_type>(~static_cast(a)); \ + } \ + inline _enum_type &operator|=(_enum_type &a, _enum_type b) \ + { \ + return a = static_cast<_enum_type>(static_cast(a) | b); \ + } \ + inline _enum_type &operator&=(_enum_type &a, _enum_type b) \ + { \ + return a = static_cast<_enum_type>(static_cast(a) & b); \ + } + +#else +/* Output nothing. */ +# define ENUM_OPERATORS(_type) +#endif + +/** \} */ + /* -------------------------------------------------------------------- */ /** \name Misc Macros * \{ */ -- cgit v1.2.3