Debian Codegraph @

src/Base/Bitmask.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 51 Franklin Street, *
21 * Fifth Floor, Boston, MA 02110-1301, USA *
22 * *
23 ***************************************************************************/
24
25
26 #ifndef BASE_BITMASK_H
27 #define BASE_BITMASK_H
28
29 #include <type_traits>
30
31 /*!
32 Using enum classes as type-safe bitmasks.
33 @code
34 enum class Color {
35 Red = 1 << 0,
36 Green = 1 << 1,
37 Blue = 1 << 2
38 };
39 ENABLE_BITMASK_OPERATORS(Color)
40
41 Color yellow = Color::Red | Color::Green;
42
43 Flags<Color> flags(yellow);
44 flags.testFlag(Color::Red);
45 flags.testFlag(Color::Green);
46 @endcode
47 */
48
49 // NOLINTBEGIN
50 // clang-format off
51 // Based on https://stackoverflow.com/questions/1448396/how-to-use-enums-as-flags-in-c
52 template<class T = void> struct enum_traits {};
53
54 template<> struct enum_traits<void> {
55 struct _allow_bitops {
56 static constexpr bool allow_bitops = true;
57 };
58 using allow_bitops = _allow_bitops;
59
60 template<class T, class R = T>
61 using t = typename std::enable_if<std::is_enum<T>::value &&
62 enum_traits<T>::allow_bitops, R>::type;
63
64 template<class T>
65 using u = typename std::underlying_type<T>::type;
66 };
67
68 template<class T>
69 constexpr enum_traits<>::t<T> operator~(T a) {
70 return static_cast<T>(~static_cast<enum_traits<>::u<T>>(a));
71 }
72 template<class T>
73 constexpr enum_traits<>::t<T> operator|(T a, T b) {
74 return static_cast<T>(
75 static_cast<enum_traits<>::u<T>>(a) |
76 static_cast<enum_traits<>::u<T>>(b));
77 }
78 template<class T>
79 constexpr enum_traits<>::t<T> operator&(T a, T b) {
80 return static_cast<T>(
81 static_cast<enum_traits<>::u<T>>(a) &
82 static_cast<enum_traits<>::u<T>>(b));
83 }
84 template<class T>
85 constexpr enum_traits<>::t<T> operator^(T a, T b) {
86 return static_cast<T>(
87 static_cast<enum_traits<>::u<T>>(a) ^
88 static_cast<enum_traits<>::u<T>>(b));
89 }
90 template<class T>
91 constexpr enum_traits<>::t<T, T&> operator|=(T& a, T b) {
92 a = a | b;
93 return a;
94 }
95 template<class T>
96 constexpr enum_traits<>::t<T, T&> operator&=(T& a, T b) {
97 a = a & b;
98 return a;
99 }
100 template<class T>
101 constexpr enum_traits<>::t<T, T&> operator^=(T& a, T b) {
102 a = a ^ b;
103 return a;
104 }
105
106 #define ENABLE_BITMASK_OPERATORS(x) \
107 template<> \
108 struct enum_traits<x> : \
109 enum_traits<>::allow_bitops {};
110
111 namespace Base {
112 template <typename Enum>
113 class Flags {
114 static_assert(std::is_enum<Enum>::value, "Flags is only usable on enumeration types.");
115 Enum i;
116
117 public:
118 // Linter seems wrong on next line, don't want explicit here forcing downstream changes
119 constexpr inline Flags(Enum f = Enum()) : i(f) {} // NOLINT (runtime/explicit)
120 constexpr bool testFlag(Enum f) const {
121 using u = typename std::underlying_type<Enum>::type;
122 return (i & f) == f && (static_cast<u>(f) != 0 || i == f);
123 }
124 constexpr inline void setFlag(Enum f, bool on = true) {
125 on ? (i |= f) : (i &= ~f);
126 }
127 constexpr bool isEqual(Flags f) const {
128 using u = typename std::underlying_type<Enum>::type;
129 return static_cast<u>(i) == static_cast<u>(f.i);
130 }
131 constexpr Enum getFlags() const {
132 return i;
133 }
134 constexpr Flags<Enum> &operator|=(const Flags<Enum> &other) {
135 i |= other.i;
136 return *this;
137 }
138 constexpr Flags<Enum> &operator|=(const Enum &f) {
139 i |= f;
140 return *this;
141 }
142 constexpr Flags<Enum> operator|(const Flags<Enum> &other) const {
143 return i | other.i;
144 }
145 constexpr Flags<Enum> operator|(const Enum &f) const {
146 return i | f;
147 }
148 constexpr Flags<Enum> &operator&=(const Flags<Enum> &other) {
149 i &= other.i;
150 return *this;
151 }
152 constexpr Flags<Enum> &operator&=(const Enum &f) {
153 i &= f;
154 return *this;
155 }
156 constexpr Flags<Enum> operator&(const Flags<Enum> &other) const {
157 return i & other.i;
158 }
159 constexpr Flags<Enum> operator&(const Enum &f) const {
160 return i & f;
161 }
162 constexpr Flags<Enum> operator~() const {
163 return ~i;
164 }
165
166 constexpr bool operator!() const {
167 return !i;
168 }
169
170 explicit operator bool() const {
171 return toUnderlyingType() != 0;
172 }
173 typename std::underlying_type<Enum>::type toUnderlyingType() const {
174 return static_cast<typename std::underlying_type<Enum>::type>(i);
175 }
176 };
177 }
178 // clang-format on
179 // NOLINTEND
180
181 #endif