Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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
10namespace impeller {
11
12template <typename EnumType_>
13struct 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///
32template <typename EnumType_>
33struct 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
128template <
129 typename EnumType,
130 typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
131inline constexpr Mask<EnumType> operator|(const EnumType& lhs,
132 const EnumType& rhs) {
133 return Mask<EnumType>{lhs} | rhs;
134}
135
136template <
137 typename EnumType,
138 typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
139inline constexpr Mask<EnumType> operator&(const EnumType& lhs,
140 const EnumType& rhs) {
141 return Mask<EnumType>{lhs} & rhs;
142}
143
144template <
145 typename EnumType,
146 typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
147inline constexpr Mask<EnumType> operator^(const EnumType& lhs,
148 const EnumType& rhs) {
149 return Mask<EnumType>{lhs} ^ rhs;
150}
151
152template <
153 typename EnumType,
154 typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
155inline constexpr Mask<EnumType> operator~(const EnumType& other) {
156 return ~Mask<EnumType>{other};
157}
158
159template <
160 typename EnumType,
161 typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
162inline constexpr Mask<EnumType> operator|(const EnumType& lhs,
163 const Mask<EnumType>& rhs) {
164 return Mask<EnumType>{lhs} | rhs;
165}
166
167template <
168 typename EnumType,
169 typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
170inline constexpr Mask<EnumType> operator&(const EnumType& lhs,
171 const Mask<EnumType>& rhs) {
172 return Mask<EnumType>{lhs} & rhs;
173}
174
175template <
176 typename EnumType,
177 typename std::enable_if<MaskTraits<EnumType>::kIsMask, bool>::type = true>
178inline 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
186template <typename EnumType,
187 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
188inline constexpr bool operator<(const EnumType& lhs,
189 const Mask<EnumType>& rhs) {
190 return Mask<EnumType>{lhs} < rhs;
191}
192
193template <typename EnumType,
194 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
195inline constexpr bool operator>(const EnumType& lhs,
196 const Mask<EnumType>& rhs) {
197 return Mask<EnumType>{lhs} > rhs;
198}
199
200template <typename EnumType,
201 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
202inline constexpr bool operator<=(const EnumType& lhs,
203 const Mask<EnumType>& rhs) {
204 return Mask<EnumType>{lhs} <= rhs;
205}
206
207template <typename EnumType,
208 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
209inline constexpr bool operator>=(const EnumType& lhs,
210 const Mask<EnumType>& rhs) {
211 return Mask<EnumType>{lhs} >= rhs;
212}
213
214template <typename EnumType,
215 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
216inline constexpr bool operator==(const EnumType& lhs,
217 const Mask<EnumType>& rhs) {
218 return Mask<EnumType>{lhs} == rhs;
219}
220
221template <typename EnumType,
222 typename std::enable_if_t<MaskTraits<EnumType>::kIsMask, bool> = true>
223inline 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_
constexpr Mask< EnumType > operator~(const EnumType &other)
Definition mask.h:155
constexpr bool operator>=(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition mask.h:209
constexpr bool operator>(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition mask.h:195
constexpr Mask< EnumType > operator|(const EnumType &lhs, const EnumType &rhs)
Definition mask.h:131
constexpr Mask< EnumType > operator&(const EnumType &lhs, const EnumType &rhs)
Definition mask.h:139
constexpr bool operator<(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition mask.h:188
constexpr bool operator==(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition mask.h:216
constexpr bool operator<=(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition mask.h:202
constexpr bool operator!=(const EnumType &lhs, const Mask< EnumType > &rhs)
Definition mask.h:223
constexpr Mask< EnumType > operator^(const EnumType &lhs, const EnumType &rhs)
Definition mask.h:147
static constexpr bool kIsMask
Definition mask.h:14
A mask of typed enums.
Definition mask.h:33
constexpr Mask< EnumType > & operator=(const Mask< EnumType > &)=default
constexpr Mask< EnumType > & operator&=(const Mask< EnumType > &other)
Definition mask.h:112
constexpr Mask< EnumType > operator|(const Mask< EnumType > &other) const
Definition mask.h:91
constexpr Mask< EnumType > operator~() const
Definition mask.h:99
EnumType_ EnumType
Definition mask.h:34
constexpr Mask(const Mask< EnumType > &other)=default
constexpr bool operator<=(const Mask< EnumType > &other) const
Definition mask.h:69
constexpr bool operator!() const
Definition mask.h:83
constexpr Mask< EnumType > & operator=(Mask< EnumType > &&)=default
constexpr Mask< EnumType > & operator|=(const Mask< EnumType > &other)
Definition mask.h:107
constexpr Mask()=default
constexpr bool operator>(const Mask< EnumType > &other) const
Definition mask.h:61
constexpr Mask(Mask< EnumType > &&other)=default
constexpr bool operator!=(const Mask< EnumType > &other) const
Definition mask.h:77
constexpr bool operator>=(const Mask< EnumType > &other) const
Definition mask.h:65
constexpr bool operator<(const Mask< EnumType > &other) const
Definition mask.h:57
constexpr Mask< EnumType > operator^(const Mask< EnumType > &other) const
Definition mask.h:95
constexpr Mask< EnumType > & operator^=(const Mask< EnumType > &other)
Definition mask.h:117
constexpr Mask(EnumType type)
Definition mask.h:43
constexpr Mask(MaskType mask)
Definition mask.h:46
constexpr bool operator==(const Mask< EnumType > &other) const
Definition mask.h:73
typename std::underlying_type< EnumType >::type MaskType
Definition mask.h:35
constexpr Mask< EnumType > operator&(const Mask< EnumType > &other) const
Definition mask.h:87