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