1/*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef ES2PANDA_UTIL_INCLUDE_ENUM_BITOPS_H
17#define ES2PANDA_UTIL_INCLUDE_ENUM_BITOPS_H
18
19#include <type_traits>
20
21#define DEFINE_BITOPS(T)                                                          \
22    inline T operator~(T a)                                                       \
23    {                                                                             \
24        using utype = std::underlying_type_t<T>;                                  \
25        /* NOLINTNEXTLINE(hicpp-signed-bitwise) */                                \
26        return static_cast<T>(~static_cast<utype>(a));                            \
27    }                                                                             \
28                                                                                  \
29    inline bool operator!(T a)                                                    \
30    {                                                                             \
31        using utype = std::underlying_type_t<T>;                                  \
32        /* NOLINTNEXTLINE(hicpp-signed-bitwise) */                                \
33        return (!static_cast<utype>(a));                                          \
34    }                                                                             \
35                                                                                  \
36    inline T operator|(T a, T b)                                                  \
37    {                                                                             \
38        using utype = std::underlying_type_t<T>;                                  \
39        /* NOLINTNEXTLINE(hicpp-signed-bitwise) */                                \
40        return static_cast<T>(static_cast<utype>(a) | static_cast<utype>(b));     \
41    }                                                                             \
42                                                                                  \
43    inline std::underlying_type_t<T> operator&(T a, T b)                          \
44    {                                                                             \
45        using utype = std::underlying_type_t<T>;                                  \
46        /* NOLINTNEXTLINE(hicpp-signed-bitwise) */                                \
47        return static_cast<utype>(static_cast<utype>(a) & static_cast<utype>(b)); \
48    }                                                                             \
49                                                                                  \
50    inline T operator^(T a, T b)                                                  \
51    {                                                                             \
52        using utype = std::underlying_type_t<T>;                                  \
53        /* NOLINTNEXTLINE(hicpp-signed-bitwise) */                                \
54        return static_cast<T>(static_cast<utype>(a) ^ static_cast<utype>(b));     \
55    }                                                                             \
56                                                                                  \
57    inline T &operator|=(T &a, T b)                                               \
58    {                                                                             \
59        /* NOLINTNEXTLINE(hicpp-signed-bitwise) */                                \
60        return a = a | b;                                                         \
61    }                                                                             \
62                                                                                  \
63    inline T &operator&=(T &a, T b)                                               \
64    {                                                                             \
65        using utype = std::underlying_type_t<T>;                                  \
66        /* NOLINTNEXTLINE(hicpp-signed-bitwise) */                                \
67        return a = static_cast<T>(static_cast<utype>(a) & static_cast<utype>(b)); \
68    }                                                                             \
69                                                                                  \
70    inline T &operator^=(T &a, T b)                                               \
71    {                                                                             \
72        /* NOLINTNEXTLINE(hicpp-signed-bitwise) */                                \
73        return a = a ^ b;                                                         \
74    }
75
76#endif
77