From 76a1fb3b42c245a425fde55b97a393d3e1984112 Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Mon, 31 Aug 2020 18:29:10 +0200 Subject: [PATCH] util: add helpers to define bitwise operators on enums for C++ Signed-off-by: Karol Herbst Reviewed-by: Jason Ekstrand Part-of: --- src/util/Makefile.sources | 1 + src/util/enum_operators.h | 66 +++++++++++++++++++++++++++++++++++++++ src/util/meson.build | 1 + 3 files changed, 68 insertions(+) create mode 100644 src/util/enum_operators.h diff --git a/src/util/Makefile.sources b/src/util/Makefile.sources index c1e6e34c46e..6b01b137b27 100644 --- a/src/util/Makefile.sources +++ b/src/util/Makefile.sources @@ -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 index 00000000000..ec3b7f5ee5f --- /dev/null +++ b/src/util/enum_operators.h @@ -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(ua op ub); \ +} \ + \ +static inline \ +Enum& operator op##= (Enum &a, Enum b) \ +{ \ + uint64_t ua = a, ub = b; \ + ua op##= ub; \ + a = static_cast(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(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 diff --git a/src/util/meson.build b/src/util/meson.build index 0893f64793b..976034cc729 100644 --- a/src/util/meson.build +++ b/src/util/meson.build @@ -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', -- 2.30.2