| 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_;
35 using MaskType = typename std::underlying_type<EnumType>::type;
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 // The following relational operators can be replaced with a defaulted
55 // spaceship operator post C++20.
56
57 constexpr bool operator<(const Mask<EnumType>& other) const {
58 return mask_ < other.mask_;
59 }
60
61 constexpr bool operator>(const Mask<EnumType>& other) const {
62 return mask_ > other.mask_;
63 }
64
65 constexpr bool operator>=(const Mask<EnumType>& other) const {
66 return mask_ >= other.mask_;
67 }
68
69 constexpr bool operator<=(const Mask<EnumType>& other) const {
70 return mask_ <= other.mask_;
71 }
72
73 constexpr bool operator==(const Mask<EnumType>& other) const {
74 return mask_ == other.mask_;
75 }
76
77 constexpr bool operator!=(const Mask<EnumType>& other) const {
78 return mask_ != other.mask_;
79 }
80
81 // Logical operators.
82
83 constexpr bool operator!() const { return !mask_; }
84
85 // Bitwise operators.
86
87 constexpr Mask<EnumType> operator&(const Mask<EnumType>& other) const {
88 return Mask<EnumType>{mask_ & other.mask_};
89 }
90
91 constexpr Mask<EnumType> operator|(const Mask<EnumType>& other) const {
92 return Mask<EnumType>{mask_ | other.mask_};
93 }
94
95 constexpr Mask<EnumType> operator^(const Mask<EnumType>& other) const {
96 return Mask<EnumType>{mask_ ^ other.mask_};
97 }
98
99 constexpr Mask<EnumType> operator~() const { return Mask<EnumType>{~mask_}; }
100
101 // Assignment operators.
102
103 constexpr Mask<EnumType>& operator=(const Mask<EnumType>&) = default;
104
105 constexpr Mask<EnumType>& operator=(Mask<EnumType>&&) = default;
106
107 constexpr Mask<EnumType>& operator|=(const Mask<EnumType>& other) {
108 mask_ |= other.mask_;
109 return *this;
110 }
111
112 constexpr Mask<EnumType>& operator&=(const Mask<EnumType>& other) {
113 mask_ &= other.mask_;
114 return *this;
115 }
116
117 constexpr Mask<EnumType>& operator^=(const Mask<EnumType>& other) {
118 mask_ ^= other.mask_;
119 return *this;
120 }
121
122 private:
123 MaskType mask_ = {};
124 };
125
126 // Construction from Enum Types
127
128 template <
129 typename EnumType,
130 typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
131 inline constexpr Mask<EnumType> operator|(const EnumType& lhs,
132 const EnumType& rhs) {
133 return Mask<EnumType>{lhs} | rhs;
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 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 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& other) {
156 return ~Mask<EnumType>{other};
157 }
158
159 template <
160 typename EnumType,
161 typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
162 inline constexpr Mask<EnumType> operator|(const EnumType& lhs,
163 const Mask<EnumType>& rhs) {
164 return Mask<EnumType>{lhs} | rhs;
165 }
166
167 template <
168 typename EnumType,
169 typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
170 inline constexpr Mask<EnumType> operator&(const EnumType& lhs,
171 const Mask<EnumType>& rhs) {
172 return Mask<EnumType>{lhs} & rhs;
173 }
174
175 template <
176 typename EnumType,
177 typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
178 inline constexpr Mask<EnumType> operator^(const EnumType& lhs,
179 const Mask<EnumType>& rhs) {
180 return Mask<EnumType>{lhs} ^ rhs;
181 }
182
183 // Relational operators with EnumType promotion. These can be replaced by a
184 // defaulted spaceship operator post C++20.
185
186 template <typename EnumType,
187 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
188 inline constexpr bool operator<(const EnumType& lhs,
189 const Mask<EnumType>& rhs) {
190 return Mask<EnumType>{lhs} < rhs;
191 }
192
193 template <typename EnumType,
194 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
195 inline constexpr bool operator>(const EnumType& lhs,
196 const Mask<EnumType>& rhs) {
197 return Mask<EnumType>{lhs} > rhs;
198 }
199
200 template <typename EnumType,
201 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
202 inline constexpr bool operator<=(const EnumType& lhs,
203 const Mask<EnumType>& rhs) {
204 return Mask<EnumType>{lhs} <= rhs;
205 }
206
207 template <typename EnumType,
208 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
209 inline constexpr bool operator>=(const EnumType& lhs,
210 const Mask<EnumType>& rhs) {
211 return Mask<EnumType>{lhs} >= rhs;
212 }
213
214 template <typename EnumType,
215 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
216 inline constexpr bool operator==(const EnumType& lhs,
217 const Mask<EnumType>& rhs) {
218 return Mask<EnumType>{lhs} == rhs;
219 }
220
221 template <typename EnumType,
222 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
223 inline constexpr bool operator!=(const EnumType& lhs,
224 const Mask<EnumType>& rhs) {
225 return Mask<EnumType>{lhs} != rhs;
226 }
227
228 } // namespace impeller
229
230 #endif // FLUTTER_IMPELLER_BASE_MASK_H_
type
GLenum type
Definition: blit_command_gles.cc:153
impeller
Definition: allocation.cc:12
impeller::operator>=
constexpr bool operator>=(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition: mask.h:209
impeller::operator|
constexpr Mask< EnumType > operator|(const EnumType &lhs, const EnumType &rhs)
Definition: mask.h:131
impeller::operator^
constexpr Mask< EnumType > operator^(const EnumType &lhs, const EnumType &rhs)
Definition: mask.h:147
impeller::operator>
constexpr bool operator>(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition: mask.h:195
impeller::operator<
constexpr bool operator<(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition: mask.h:188
impeller::operator==
constexpr bool operator==(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition: mask.h:216
impeller::operator<=
constexpr bool operator<=(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition: mask.h:202
impeller::operator~
constexpr Mask< EnumType > operator~(const EnumType &other)
Definition: mask.h:155
impeller::operator!=
constexpr bool operator!=(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition: mask.h:223
impeller::operator&
constexpr Mask< EnumType > operator&(const EnumType &lhs, const EnumType &rhs)
Definition: mask.h:139
impeller::Mask
A mask of typed enums.
Definition: mask.h:33
impeller::Mask::operator|
constexpr Mask< EnumType > operator|(const Mask< EnumType > &other) const
Definition: mask.h:91
impeller::Mask::EnumType
EnumType_ EnumType
Definition: mask.h:34
impeller::Mask::Mask
constexpr Mask(const Mask< EnumType > &other)=default
impeller::Mask::operator<=
constexpr bool operator<=(const Mask< EnumType > &other) const
Definition: mask.h:69
impeller::Mask::operator&=
constexpr Mask< EnumType > & operator&=(const Mask< EnumType > &other)
Definition: mask.h:112
impeller::Mask::operator!
constexpr bool operator!() const
Definition: mask.h:83
impeller::Mask::operator^=
constexpr Mask< EnumType > & operator^=(const Mask< EnumType > &other)
Definition: mask.h:117
impeller::Mask::operator^
constexpr Mask< EnumType > operator^(const Mask< EnumType > &other) const
Definition: mask.h:95
impeller::Mask::Mask
constexpr Mask()=default
impeller::Mask::operator>
constexpr bool operator>(const Mask< EnumType > &other) const
Definition: mask.h:61
impeller::Mask::Mask
constexpr Mask(Mask< EnumType > &&other)=default
impeller::Mask::operator~
constexpr Mask< EnumType > operator~() const
Definition: mask.h:99
impeller::Mask::operator&
constexpr Mask< EnumType > operator&(const Mask< EnumType > &other) const
Definition: mask.h:87
impeller::Mask::operator!=
constexpr bool operator!=(const Mask< EnumType > &other) const
Definition: mask.h:77
impeller::Mask::operator>=
constexpr bool operator>=(const Mask< EnumType > &other) const
Definition: mask.h:65
impeller::Mask::operator<
constexpr bool operator<(const Mask< EnumType > &other) const
Definition: mask.h:57
impeller::Mask::Mask
constexpr Mask(EnumType type)
Definition: mask.h:43
impeller::Mask::Mask
constexpr Mask(MaskType mask)
Definition: mask.h:46
impeller::Mask::operator==
constexpr bool operator==(const Mask< EnumType > &other) const
Definition: mask.h:73
impeller::Mask::MaskType
typename std::underlying_type< EnumType >::type MaskType
Definition: mask.h:35
impeller::Mask::operator|=
constexpr Mask< EnumType > & operator|=(const Mask< EnumType > &other)
Definition: mask.h:107
impeller::Mask::operator=
constexpr Mask< EnumType > & operator=(const Mask< EnumType > &)=default
impeller::Mask::operator=
constexpr Mask< EnumType > & operator=(Mask< EnumType > &&)=default
impeller::MaskTraits
Definition: mask.h:13
impeller::MaskTraits::kIsMask
static constexpr bool kIsMask
Definition: mask.h:14