util: add helpers to define bitwise operators on enums for C++
authorKarol Herbst <kherbst@redhat.com>
Mon, 31 Aug 2020 16:29:10 +0000 (18:29 +0200)
committerMarge Bot <eric+marge@anholt.net>
Tue, 1 Sep 2020 17:45:08 +0000 (17:45 +0000)
Signed-off-by: Karol Herbst <kherbst@redhat.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/6520>

src/util/Makefile.sources
src/util/enum_operators.h [new file with mode: 0644]
src/util/meson.build

index c1e6e34c46eacc06a3715651204083bc499ce2ad..6b01b137b274c90f1276d8833101384a5f186026 100644 (file)
@@ -20,6 +20,7 @@ MESA_UTIL_FILES := \
        disk_cache.h \
        double.c \
        double.h \
+       enum_operators.h \
        fast_idiv_by_const.c \
        fast_idiv_by_const.h \
        format/u_format.c \
diff --git a/src/util/enum_operators.h b/src/util/enum_operators.h
new file mode 100644 (file)
index 0000000..ec3b7f5
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * Copyright © 2020 Red Hat Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#ifdef __cplusplus
+
+// 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;                            \
+   return static_cast<Enum>(ua op ub);                 \
+}                                                      \
+                                                       \
+static inline                                          \
+Enum& operator op##= (Enum &a, Enum b)                 \
+{                                                      \
+   uint64_t ua = a, ub = b;                            \
+   ua op##= ub;                                        \
+   a = static_cast<Enum>(ua);                          \
+   return a;                                           \
+}                                                      \
+}
+
+#define MESA_DEFINE_CPP_ENUM_UNARY_OPERATOR(Enum, op) \
+extern "C++" {                                        \
+static inline                                         \
+Enum operator op (Enum a)                             \
+{                                                     \
+   uint64_t ua = a;                                   \
+   return static_cast<Enum>(op ua);                   \
+}                                                     \
+}
+
+#define MESA_DEFINE_CPP_ENUM_BITFIELD_OPERATORS(Enum) \
+MESA_DEFINE_CPP_ENUM_BINARY_OPERATOR(Enum, |)         \
+MESA_DEFINE_CPP_ENUM_BINARY_OPERATOR(Enum, &)         \
+MESA_DEFINE_CPP_ENUM_BINARY_OPERATOR(Enum, ^)         \
+MESA_DEFINE_CPP_ENUM_UNARY_OPERATOR(Enum, ~)
+
+#else
+
+#define MESA_DEFINE_CPP_ENUM_BITFIELD_OPERATORS(Enum)
+
+#endif
index 0893f64793b5d7a45ce835fff3794e74938c2f04..976034cc7297a3f8eb818b9c1ceccdd35db4da6c 100644 (file)
@@ -43,6 +43,7 @@ files_mesa_util = files(
   'disk_cache.h',
   'double.c',
   'double.h',
+  'enum_operators.h',
   'fast_idiv_by_const.c',
   'fast_idiv_by_const.h',
   'format_r11g11b10f.h',