[Bf-blender-cvs] [13afc81f6da] master: BLI: Add MACRO for enum usage in C++

Clément Foucault noreply at git.blender.org
Sun Jul 26 17:30:01 CEST 2020


Commit: 13afc81f6da69e36b3d503fada0760dc07f607dc
Author: Clément Foucault
Date:   Sat Jul 25 18:19:49 2020 +0200
Branches: master
https://developer.blender.org/rB13afc81f6da69e36b3d503fada0760dc07f607dc

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.

===================================================================

M	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<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
  * \{ */



More information about the Bf-blender-cvs mailing list