Flutter Impeller
mask.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_IMPELLER_BASE_MASK_H_
6 #define FLUTTER_IMPELLER_BASE_MASK_H_
7 
8 #include <type_traits>
9 
10 namespace impeller {
11 
12 template <typename EnumType_>
13 struct MaskTraits {
14  static constexpr bool kIsMask = false;
15 };
16 
17 //------------------------------------------------------------------------------
18 /// @brief Declare this in the "impeller" namespace to make the enum
19 /// maskable.
20 ///
21 #define IMPELLER_ENUM_IS_MASK(enum_name) \
22  template <> \
23  struct MaskTraits<enum_name> { \
24  static constexpr bool kIsMask = true; \
25  };
26 
27 //------------------------------------------------------------------------------
28 /// @brief A mask of typed enums.
29 ///
30 /// @tparam EnumType_ The type of the enum. Must be an enum class.
31 ///
32 template <typename EnumType_>
33 struct Mask {
34  using EnumType = EnumType_;
36 
37  constexpr Mask() = default;
38 
39  constexpr Mask(const Mask<EnumType>& other) = default;
40 
41  constexpr Mask(Mask<EnumType>&& other) = default;
42 
43  constexpr Mask(EnumType type) // NOLINT(google-explicit-constructor)
44  : mask_(static_cast<MaskType>(type)) {}
45 
46  explicit constexpr Mask(MaskType mask) : mask_(static_cast<MaskType>(mask)) {}
47 
48  // All casts must be explicit.
49 
50  explicit constexpr operator MaskType() const { return mask_; }
51 
52  explicit constexpr operator bool() const { return !!mask_; }
53 
54  // Comparison operators.
55 
56  constexpr auto operator<=>(const Mask<EnumType>& other) const = default;
57 
58  // Logical operators.
59 
60  constexpr bool operator!() const { return !mask_; }
61 
62  // Bitwise operators.
63 
64  constexpr Mask<EnumType> operator&(const Mask<EnumType>& other) const {
65  return Mask<EnumType>{mask_ & other.mask_};
66  }
67 
68  constexpr Mask<EnumType> operator|(const Mask<EnumType>& other) const {
69  return Mask<EnumType>{mask_ | other.mask_};
70  }
71 
72  constexpr Mask<EnumType> operator^(const Mask<EnumType>& other) const {
73  return Mask<EnumType>{mask_ ^ other.mask_};
74  }
75 
76  constexpr Mask<EnumType> operator~() const { return Mask<EnumType>{~mask_}; }
77 
78  // Assignment operators.
79 
80  constexpr Mask<EnumType>& operator=(const Mask<EnumType>&) = default;
81 
82  constexpr Mask<EnumType>& operator=(Mask<EnumType>&&) = default;
83 
84  constexpr Mask<EnumType>& operator|=(const Mask<EnumType>& other) {
85  mask_ |= other.mask_;
86  return *this;
87  }
88 
89  constexpr Mask<EnumType>& operator&=(const Mask<EnumType>& other) {
90  mask_ &= other.mask_;
91  return *this;
92  }
93 
94  constexpr Mask<EnumType>& operator^=(const Mask<EnumType>& other) {
95  mask_ ^= other.mask_;
96  return *this;
97  }
98 
99  private:
100  MaskType mask_ = {};
101 };
102 
103 // Construction from Enum Types
104 
105 template <
106  typename EnumType,
107  typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
108 inline constexpr Mask<EnumType> operator|(const EnumType& lhs,
109  const EnumType& rhs) {
110  return Mask<EnumType>{lhs} | rhs;
111 }
112 
113 template <
114  typename EnumType,
115  typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
116 inline constexpr Mask<EnumType> operator&(const EnumType& lhs,
117  const EnumType& rhs) {
118  return Mask<EnumType>{lhs} & rhs;
119 }
120 
121 template <
122  typename EnumType,
123  typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
124 inline constexpr Mask<EnumType> operator^(const EnumType& lhs,
125  const EnumType& rhs) {
126  return Mask<EnumType>{lhs} ^ rhs;
127 }
128 
129 template <
130  typename EnumType,
131  typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
132 inline constexpr Mask<EnumType> operator~(const EnumType& other) {
133  return ~Mask<EnumType>{other};
134 }
135 
136 template <
137  typename EnumType,
138  typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
139 inline constexpr Mask<EnumType> operator|(const EnumType& lhs,
140  const Mask<EnumType>& rhs) {
141  return Mask<EnumType>{lhs} | rhs;
142 }
143 
144 template <
145  typename EnumType,
146  typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
147 inline constexpr Mask<EnumType> operator&(const EnumType& lhs,
148  const Mask<EnumType>& rhs) {
149  return Mask<EnumType>{lhs} & rhs;
150 }
151 
152 template <
153  typename EnumType,
154  typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
155 inline constexpr Mask<EnumType> operator^(const EnumType& lhs,
156  const Mask<EnumType>& rhs) {
157  return Mask<EnumType>{lhs} ^ rhs;
158 }
159 
160 // Relational operators with EnumType promotion.
161 
162 template <typename EnumType,
163  typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
164 inline constexpr auto operator<=>(const EnumType& lhs,
165  const Mask<EnumType>& rhs) {
166  return Mask<EnumType>{lhs} <=> rhs;
167 }
168 
169 } // namespace impeller
170 
171 #endif // FLUTTER_IMPELLER_BASE_MASK_H_
GLenum type
constexpr Mask< EnumType > operator|(const EnumType &lhs, const EnumType &rhs)
Definition: mask.h:108
constexpr Mask< EnumType > operator^(const EnumType &lhs, const EnumType &rhs)
Definition: mask.h:124
constexpr Mask< EnumType > operator~(const EnumType &other)
Definition: mask.h:132
constexpr auto operator<=>(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition: mask.h:164
constexpr Mask< EnumType > operator&(const EnumType &lhs, const EnumType &rhs)
Definition: mask.h:116
A mask of typed enums.
Definition: mask.h:33
constexpr Mask< EnumType > operator|(const Mask< EnumType > &other) const
Definition: mask.h:68
EnumType_ EnumType
Definition: mask.h:34
constexpr Mask(const Mask< EnumType > &other)=default
constexpr Mask< EnumType > & operator&=(const Mask< EnumType > &other)
Definition: mask.h:89
constexpr bool operator!() const
Definition: mask.h:60
constexpr Mask< EnumType > & operator^=(const Mask< EnumType > &other)
Definition: mask.h:94
constexpr Mask< EnumType > operator^(const Mask< EnumType > &other) const
Definition: mask.h:72
constexpr Mask()=default
constexpr Mask(Mask< EnumType > &&other)=default
constexpr Mask< EnumType > operator~() const
Definition: mask.h:76
constexpr Mask< EnumType > operator&(const Mask< EnumType > &other) const
Definition: mask.h:64
constexpr auto operator<=>(const Mask< EnumType > &other) const =default
constexpr Mask(EnumType type)
Definition: mask.h:43
constexpr Mask(MaskType mask)
Definition: mask.h:46
typename std::underlying_type< EnumType >::type MaskType
Definition: mask.h:35
constexpr Mask< EnumType > & operator|=(const Mask< EnumType > &other)
Definition: mask.h:84
constexpr Mask< EnumType > & operator=(const Mask< EnumType > &)=default
constexpr Mask< EnumType > & operator=(Mask< EnumType > &&)=default
static constexpr bool kIsMask
Definition: mask.h:14