util: add helpers to define bitwise operators on enums for C++

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>
This commit is contained in:
Karol Herbst 2020-08-31 18:29:10 +02:00 committed by Marge Bot
parent 24dfd798d6
commit 76a1fb3b42
3 changed files with 68 additions and 0 deletions

View 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 \

66
src/util/enum_operators.h Normal file
View File

@ -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

View 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',