• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <android-base/stringprintf.h>
18 
19 #include <array>
20 #include <cstdint>
21 #include <optional>
22 #include <string>
23 #include <type_traits>
24 
25 #include "NamedEnum.h"
26 #include "utils/BitSet.h"
27 
28 #ifndef __UI_INPUT_FLAGS_H
29 #define __UI_INPUT_FLAGS_H
30 
31 namespace android {
32 
33 namespace details {
34 
35 template <typename F>
36 inline constexpr auto flag_count = sizeof(F) * __CHAR_BIT__;
37 
38 template <typename F, typename T, T... I>
generate_flag_values(std::integer_sequence<T,I...> seq)39 constexpr auto generate_flag_values(std::integer_sequence<T, I...> seq) {
40     constexpr size_t count = seq.size();
41 
42     std::array<F, count> values{};
43     for (size_t i = 0, v = 0; v < count; ++i) {
44         values[v++] = static_cast<F>(T{1} << i);
45     }
46 
47     return values;
48 }
49 
50 template <typename F>
51 inline constexpr auto flag_values = generate_flag_values<F>(
52         std::make_integer_sequence<std::underlying_type_t<F>, flag_count<F>>{});
53 
54 template <typename F, std::size_t... I>
generate_flag_names(std::index_sequence<I...>)55 constexpr auto generate_flag_names(std::index_sequence<I...>) noexcept {
56     return std::array<std::optional<std::string_view>, sizeof...(I)>{
57             {enum_value_name<F, flag_values<F>[I]>()...}};
58 }
59 
60 template <typename F>
61 inline constexpr auto flag_names =
62         generate_flag_names<F>(std::make_index_sequence<flag_count<F>>{});
63 
64 // A trait for determining whether a type is specifically an enum class or not.
65 template <typename T, bool = std::is_enum_v<T>>
66 struct is_enum_class : std::false_type {};
67 
68 // By definition, an enum class is an enum that is not implicitly convertible to its underlying
69 // type.
70 template <typename T>
71 struct is_enum_class<T, true>
72       : std::bool_constant<!std::is_convertible_v<T, std::underlying_type_t<T>>> {};
73 
74 template <typename T>
75 inline constexpr bool is_enum_class_v = is_enum_class<T>::value;
76 } // namespace details
77 
78 template <auto V>
79 constexpr auto flag_name() {
80     using F = decltype(V);
81     return details::enum_value_name<F, V>();
82 }
83 
84 template <typename F>
85 constexpr std::optional<std::string_view> flag_name(F flag) {
86     using U = std::underlying_type_t<F>;
87     auto idx = static_cast<size_t>(__builtin_ctzl(static_cast<U>(flag)));
88     return details::flag_names<F>[idx];
89 }
90 
91 /* A class for handling flags defined by an enum or enum class in a type-safe way. */
92 template <typename F>
93 class Flags {
94     // F must be an enum or its underlying type is undefined. Theoretically we could specialize this
95     // further to avoid this restriction but in general we want to encourage the use of enums
96     // anyways.
97     static_assert(std::is_enum_v<F>, "Flags type must be an enum");
98     using U = typename std::underlying_type_t<F>;
99 
100 public:
101     constexpr Flags(F f) : mFlags(static_cast<U>(f)) {}
102     constexpr Flags() : mFlags(0) {}
103     constexpr Flags(const Flags<F>& f) : mFlags(f.mFlags) {}
104 
105     // Provide a non-explicit construct for non-enum classes since they easily convert to their
106     // underlying types (e.g. when used with bitwise operators). For enum classes, however, we
107     // should force them to be explicitly constructed from their underlying types to make full use
108     // of the type checker.
109     template <typename T = U>
110     constexpr Flags(T t, typename std::enable_if_t<!details::is_enum_class_v<F>, T>* = nullptr)
111           : mFlags(t) {}
112     template <typename T = U>
113     explicit constexpr Flags(T t,
114                              typename std::enable_if_t<details::is_enum_class_v<F>, T>* = nullptr)
115           : mFlags(t) {}
116 
117     class Iterator {
118         // The type can't be larger than 64-bits otherwise it won't fit in BitSet64.
119         static_assert(sizeof(U) <= sizeof(uint64_t));
120 
121     public:
122         Iterator(Flags<F> flags) : mRemainingFlags(flags.mFlags) { (*this)++; }
123         Iterator() : mRemainingFlags(0), mCurrFlag(static_cast<F>(0)) {}
124 
125         // Pre-fix ++
126         Iterator& operator++() {
127             if (mRemainingFlags.isEmpty()) {
128                 mCurrFlag = static_cast<F>(0);
129             } else {
130                 uint64_t bit = mRemainingFlags.clearLastMarkedBit(); // counts from left
131                 const U flag = 1 << (64 - bit - 1);
132                 mCurrFlag = static_cast<F>(flag);
133             }
134             return *this;
135         }
136 
137         // Post-fix ++
138         Iterator operator++(int) {
139             Iterator iter = *this;
140             ++*this;
141             return iter;
142         }
143 
144         bool operator==(Iterator other) const {
145             return mCurrFlag == other.mCurrFlag && mRemainingFlags == other.mRemainingFlags;
146         }
147 
148         bool operator!=(Iterator other) const { return !(*this == other); }
149 
150         F operator*() { return mCurrFlag; }
151 
152         // iterator traits
153 
154         // In the future we could make this a bidirectional const iterator instead of a forward
155         // iterator but it doesn't seem worth the added complexity at this point. This could not,
156         // however, be made a non-const iterator as assigning one flag to another is a non-sensical
157         // operation.
158         using iterator_category = std::input_iterator_tag;
159         using value_type = F;
160         // Per the C++ spec, because input iterators are not assignable the iterator's reference
161         // type does not actually need to be a reference. In fact, making it a reference would imply
162         // that modifying it would change the underlying Flags object, which is obviously wrong for
163         // the same reason this can't be a non-const iterator.
164         using reference = F;
165         using difference_type = void;
166         using pointer = void;
167 
168     private:
169         BitSet64 mRemainingFlags;
170         F mCurrFlag;
171     };
172 
173     /*
174      * Tests whether the given flag is set.
175      */
176     bool test(F flag) const {
177         U f = static_cast<U>(flag);
178         return (f & mFlags) == f;
179     }
180 
181     /* Tests whether any of the given flags are set */
182     bool any(Flags<F> f) { return (mFlags & f.mFlags) != 0; }
183 
184     /* Tests whether all of the given flags are set */
185     bool all(Flags<F> f) { return (mFlags & f.mFlags) == f.mFlags; }
186 
187     Flags<F> operator|(Flags<F> rhs) const { return static_cast<F>(mFlags | rhs.mFlags); }
188     Flags<F>& operator|=(Flags<F> rhs) {
189         mFlags = mFlags | rhs.mFlags;
190         return *this;
191     }
192 
193     Flags<F> operator&(Flags<F> rhs) const { return static_cast<F>(mFlags & rhs.mFlags); }
194     Flags<F>& operator&=(Flags<F> rhs) {
195         mFlags = mFlags & rhs.mFlags;
196         return *this;
197     }
198 
199     Flags<F> operator^(Flags<F> rhs) const { return static_cast<F>(mFlags ^ rhs.mFlags); }
200     Flags<F>& operator^=(Flags<F> rhs) {
201         mFlags = mFlags ^ rhs.mFlags;
202         return *this;
203     }
204 
205     Flags<F> operator~() { return static_cast<F>(~mFlags); }
206 
207     bool operator==(Flags<F> rhs) const { return mFlags == rhs.mFlags; }
208     bool operator!=(Flags<F> rhs) const { return !operator==(rhs); }
209 
210     Flags<F>& operator=(const Flags<F>& rhs) {
211         mFlags = rhs.mFlags;
212         return *this;
213     }
214 
215     Iterator begin() const { return Iterator(*this); }
216 
217     Iterator end() const { return Iterator(); }
218 
219     /*
220      * Returns the stored set of flags.
221      *
222      * Note that this returns the underlying type rather than the base enum class. This is because
223      * the value is no longer necessarily a strict member of the enum since the returned value could
224      * be multiple enum variants OR'd together.
225      */
226     U get() const { return mFlags; }
227 
228     std::string string() const {
229         std::string result;
230         bool first = true;
231         U unstringified = 0;
232         for (const F f : *this) {
233             std::optional<std::string_view> flagString = flag_name(f);
234             if (flagString) {
235                 appendFlag(result, flagString.value(), first);
236             } else {
237                 unstringified |= static_cast<U>(f);
238             }
239         }
240 
241         if (unstringified != 0) {
242             appendFlag(result, base::StringPrintf("0x%08x", unstringified), first);
243         }
244 
245         if (first) {
246             result += "0x0";
247         }
248 
249         return result;
250     }
251 
252 private:
253     U mFlags;
254 
255     static void appendFlag(std::string& str, const std::string_view& flag, bool& first) {
256         if (first) {
257             first = false;
258         } else {
259             str += " | ";
260         }
261         str += flag;
262     }
263 };
264 
265 // This namespace provides operator overloads for enum classes to make it easier to work with them
266 // as flags. In order to use these, add them via a `using namespace` declaration.
267 namespace flag_operators {
268 
269 template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>>
270 inline Flags<F> operator~(F f) {
271     using U = typename std::underlying_type_t<F>;
272     return static_cast<F>(~static_cast<U>(f));
273 }
274 template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>>
275 Flags<F> operator|(F lhs, F rhs) {
276     using U = typename std::underlying_type_t<F>;
277     return static_cast<F>(static_cast<U>(lhs) | static_cast<U>(rhs));
278 }
279 
280 } // namespace flag_operators
281 } // namespace android
282 
283 #endif // __UI_INPUT_FLAGS_H
284