Debian Codegraph @

src/base/SkEnumBitMask.h

raw
1 /*
2 * Copyright 2021 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #ifndef SkEnumBitMask_DEFINED
9 #define SkEnumBitMask_DEFINED
10
11 #include "include/private/base/SkAttributes.h"
12
13 #include <type_traits>
14
15 /**
16 * Wraps an enum that is used for flags, and enables masking with type safety. Example:
17 *
18 * enum class MyFlags {
19 * kNone = 0,
20 * kA = 1,
21 * kB = 2,
22 * kC = 4,
23 * };
24 *
25 * SK_MAKE_BITMASK_OPS(MyFlags)
26 *
27 * ...
28 *
29 * SkEnumBitMask<MyFlags> flags = MyFlags::kA | MyFlags::kB;
30 *
31 * if (flags & MyFlags::kB) {}
32 *
33 * ...
34 */
35 template<typename E>
36 class SkEnumBitMask {
37 using I = std::underlying_type_t<E>;
38 public:
39 SK_ALWAYS_INLINE constexpr SkEnumBitMask() : SkEnumBitMask(I(0)) {}
40 SK_ALWAYS_INLINE constexpr SkEnumBitMask(E e) : SkEnumBitMask(static_cast<I>(e)) {}
41
42 SK_ALWAYS_INLINE constexpr explicit operator bool() const { return fValue; }
43 SK_ALWAYS_INLINE constexpr I value() const { return fValue; }
44
45 SK_ALWAYS_INLINE constexpr bool operator==(SkEnumBitMask m) const { return fValue == m.fValue; }
46 SK_ALWAYS_INLINE constexpr bool operator!=(SkEnumBitMask m) const { return fValue != m.fValue; }
47
48 SK_ALWAYS_INLINE constexpr SkEnumBitMask operator|(SkEnumBitMask m) const {
49 return SkEnumBitMask(fValue | m.fValue);
50 }
51 SK_ALWAYS_INLINE constexpr SkEnumBitMask operator&(SkEnumBitMask m) const {
52 return SkEnumBitMask(fValue & m.fValue);
53 }
54 SK_ALWAYS_INLINE constexpr SkEnumBitMask operator^(SkEnumBitMask m) const {
55 return SkEnumBitMask(fValue ^ m.fValue);
56 }
57 SK_ALWAYS_INLINE constexpr SkEnumBitMask operator~() const { return SkEnumBitMask(~fValue); }
58
59 SK_ALWAYS_INLINE SkEnumBitMask& operator|=(SkEnumBitMask m) { return *this = *this | m; }
60 SK_ALWAYS_INLINE SkEnumBitMask& operator&=(SkEnumBitMask m) { return *this = *this & m; }
61 SK_ALWAYS_INLINE SkEnumBitMask& operator^=(SkEnumBitMask m) { return *this = *this ^ m; }
62
63 private:
64 SK_ALWAYS_INLINE constexpr explicit SkEnumBitMask(I value) : fValue(value) {}
65
66 I fValue;
67 };
68
69 /**
70 * Defines functions that make it possible to use bitwise operators on an enum.
71 */
72 #define SK_MAKE_BITMASK_OPS(E) \
73 [[maybe_unused]] constexpr SkEnumBitMask<E> operator|(E a, E b) { \
74 return SkEnumBitMask<E>(a) | b; \
75 } \
76 [[maybe_unused]] constexpr SkEnumBitMask<E> operator&(E a, E b) { \
77 return SkEnumBitMask<E>(a) & b; \
78 } \
79 [[maybe_unused]] constexpr SkEnumBitMask<E> operator^(E a, E b) { \
80 return SkEnumBitMask<E>(a) ^ b; \
81 } \
82 [[maybe_unused]] constexpr SkEnumBitMask<E> operator~(E e) { \
83 return ~SkEnumBitMask<E>(e); \
84 }
85
86 #define SK_DECL_BITMASK_OPS_FRIENDS(E) \
87 friend constexpr SkEnumBitMask<E> operator|(E, E); \
88 friend constexpr SkEnumBitMask<E> operator&(E, E); \
89 friend constexpr SkEnumBitMask<E> operator^(E, E); \
90 friend constexpr SkEnumBitMask<E> operator~(E);
91
92 #endif // SkEnumBitMask_DEFINED