Skip to content

Commit f185250

Browse files
committed
COMMON: Add helper macros for bitwise enum struct
1 parent ba5bd05 commit f185250

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
2+
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
3+
// All rights not expressly granted are reserved.
4+
//
5+
// This software is distributed under the terms of the GNU General Public
6+
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
7+
//
8+
// In applying this license CERN does not waive the privileges and immunities
9+
// granted to it by virtue of its status as an Intergovernmental Organization
10+
// or submit itself to any jurisdiction.
11+
#ifndef O2_FRAMEWORK_ENUM_BIT_OPERATORS_H_
12+
#define O2_FRAMEWORK_ENUM_BIT_OPERATORS_H_
13+
14+
#include <type_traits>
15+
16+
#define O2_DEFINE_ENUM_BIT_OPERATORS(enum_t) \
17+
constexpr auto operator|(enum_t lhs, enum_t rhs) \
18+
{ \
19+
return static_cast<enum_t>( \
20+
static_cast<std::underlying_type_t<enum_t>>(lhs) | \
21+
static_cast<std::underlying_type_t<enum_t>>(rhs)); \
22+
} \
23+
\
24+
constexpr auto operator&(enum_t lhs, enum_t rhs) \
25+
{ \
26+
return static_cast<enum_t>( \
27+
static_cast<std::underlying_type_t<enum_t>>(lhs) & \
28+
static_cast<std::underlying_type_t<enum_t>>(rhs)); \
29+
} \
30+
\
31+
constexpr auto operator^(enum_t lhs, enum_t rhs) \
32+
{ \
33+
return static_cast<enum_t>( \
34+
static_cast<std::underlying_type_t<enum_t>>(lhs) ^ \
35+
static_cast<std::underlying_type_t<enum_t>>(rhs)); \
36+
} \
37+
\
38+
constexpr auto operator~(enum_t op) \
39+
{ \
40+
return static_cast<enum_t>( \
41+
~static_cast<std::underlying_type_t<enum_t>>(op)); \
42+
} \
43+
\
44+
constexpr auto& operator|=(enum_t& lhs, enum_t rhs) \
45+
{ \
46+
lhs = lhs | rhs; \
47+
return lhs; \
48+
} \
49+
\
50+
constexpr auto& operator&=(enum_t& lhs, enum_t rhs) \
51+
{ \
52+
lhs = lhs & rhs; \
53+
return lhs; \
54+
} \
55+
\
56+
constexpr enum_t& operator^=(enum_t& lhs, enum_t rhs) \
57+
{ \
58+
lhs = lhs ^ rhs; \
59+
return lhs; \
60+
}
61+
62+
#define O2_ENUM_TEST_BIT(mask, value) ((mask & value) == value)
63+
#define O2_ENUM_SET_BIT(bit) ((1 << bit))
64+
#define O2_ENUM_ANY_BIT(enum) ((static_cast<std::underlying_type_t<decltype(enum)>>(enum) != 0))
65+
66+
#endif

0 commit comments

Comments
 (0)