blob: 4ad90567ae826e59c6d16835b8e77929e802c929 [file] [log] [blame]
Michael Wright44753b12020-07-08 13:48:11 +01001/*
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
Michael Wright8759d672020-07-21 00:46:45 +010019#include <array>
Michael Wright44753b12020-07-08 13:48:11 +010020#include <cstdint>
21#include <optional>
22#include <string>
23#include <type_traits>
24
25#include "utils/BitSet.h"
26
27#ifndef __UI_INPUT_FLAGS_H
28#define __UI_INPUT_FLAGS_H
29
30namespace android {
31
Michael Wright8759d672020-07-21 00:46:45 +010032namespace details {
33template <typename F, F V>
34constexpr std::optional<std::string_view> enum_value_name() {
35 // Should look something like (but all on one line):
36 // std::optional<std::string_view>
37 // android::details::enum_value_name()
38 // [F = android::test::TestFlags, V = android::test::TestFlags::ONE]
39 std::string_view view = __PRETTY_FUNCTION__;
40 size_t templateStart = view.rfind("[");
41 size_t templateEnd = view.rfind("]");
42 if (templateStart == std::string::npos || templateEnd == std::string::npos) {
43 return std::nullopt;
44 }
45
46 // Extract the template parameters without the enclosing braces.
47 // Example (cont'd): F = android::test::TestFlags, V = android::test::TestFlags::ONE
48 view = view.substr(templateStart + 1, templateEnd - templateStart - 1);
49 size_t valStart = view.rfind("V = ");
50 if (valStart == std::string::npos) {
51 return std::nullopt;
52 }
53
54 // Example (cont'd): V = android::test::TestFlags::ONE
55 view = view.substr(valStart);
56 size_t nameStart = view.rfind("::");
57 if (nameStart == std::string::npos) {
58 return std::nullopt;
59 }
60
61 // Chop off the initial "::"
62 nameStart += 2;
63 return view.substr(nameStart);
64}
65
66template <typename F>
67inline constexpr auto flag_count = sizeof(F) * __CHAR_BIT__;
68
69template <typename F, typename T, T... I>
70constexpr auto generate_flag_values(std::integer_sequence<T, I...> seq) {
Michael Wright74c82422020-07-23 15:30:21 +010071 constexpr size_t count = seq.size();
Michael Wright8759d672020-07-21 00:46:45 +010072
73 std::array<F, count> values{};
Michael Wright74c82422020-07-23 15:30:21 +010074 for (size_t i = 0, v = 0; v < count; ++i) {
Michael Wright8759d672020-07-21 00:46:45 +010075 values[v++] = static_cast<F>(T{1} << i);
76 }
77
78 return values;
79}
80
81template <typename F>
82inline constexpr auto flag_values = generate_flag_values<F>(
83 std::make_integer_sequence<std::underlying_type_t<F>, flag_count<F>>{});
84
85template <typename F, std::size_t... I>
86constexpr auto generate_flag_names(std::index_sequence<I...>) noexcept {
87 return std::array<std::optional<std::string_view>, sizeof...(I)>{
88 {enum_value_name<F, flag_values<F>[I]>()...}};
89}
90
91template <typename F>
92inline constexpr auto flag_names =
93 generate_flag_names<F>(std::make_index_sequence<flag_count<F>>{});
94
Michael Wright44753b12020-07-08 13:48:11 +010095// A trait for determining whether a type is specifically an enum class or not.
96template <typename T, bool = std::is_enum_v<T>>
97struct is_enum_class : std::false_type {};
98
99// By definition, an enum class is an enum that is not implicitly convertible to its underlying
100// type.
101template <typename T>
102struct is_enum_class<T, true>
103 : std::bool_constant<!std::is_convertible_v<T, std::underlying_type_t<T>>> {};
104
105template <typename T>
106inline constexpr bool is_enum_class_v = is_enum_class<T>::value;
Michael Wright8759d672020-07-21 00:46:45 +0100107} // namespace details
108
109template <auto V>
110constexpr auto flag_name() {
111 using F = decltype(V);
112 return details::enum_value_name<F, V>();
113}
114
115template <typename F>
116constexpr std::optional<std::string_view> flag_name(F flag) {
117 using U = std::underlying_type_t<F>;
118 auto idx = __builtin_ctzl(static_cast<U>(flag));
119 return details::flag_names<F>[idx];
120}
Michael Wright44753b12020-07-08 13:48:11 +0100121
122/* A class for handling flags defined by an enum or enum class in a type-safe way. */
Michael Wright8759d672020-07-21 00:46:45 +0100123template <typename F>
Michael Wright44753b12020-07-08 13:48:11 +0100124class Flags {
125 // F must be an enum or its underlying type is undefined. Theoretically we could specialize this
126 // further to avoid this restriction but in general we want to encourage the use of enums
127 // anyways.
Michael Wright8759d672020-07-21 00:46:45 +0100128 static_assert(std::is_enum_v<F>, "Flags type must be an enum");
Michael Wright44753b12020-07-08 13:48:11 +0100129 using U = typename std::underlying_type_t<F>;
130
131public:
Michael Wright75d9e662020-07-11 23:54:40 +0100132 constexpr Flags(F f) : mFlags(static_cast<U>(f)) {}
133 constexpr Flags() : mFlags(0) {}
134 constexpr Flags(const Flags<F>& f) : mFlags(f.mFlags) {}
Michael Wright44753b12020-07-08 13:48:11 +0100135
136 // Provide a non-explicit construct for non-enum classes since they easily convert to their
137 // underlying types (e.g. when used with bitwise operators). For enum classes, however, we
138 // should force them to be explicitly constructed from their underlying types to make full use
139 // of the type checker.
140 template <typename T = U>
Michael Wright8759d672020-07-21 00:46:45 +0100141 constexpr Flags(T t, typename std::enable_if_t<!details::is_enum_class_v<F>, T>* = nullptr)
Michael Wright75d9e662020-07-11 23:54:40 +0100142 : mFlags(t) {}
Michael Wright44753b12020-07-08 13:48:11 +0100143 template <typename T = U>
Michael Wright8759d672020-07-21 00:46:45 +0100144 explicit constexpr Flags(T t,
145 typename std::enable_if_t<details::is_enum_class_v<F>, T>* = nullptr)
Michael Wright75d9e662020-07-11 23:54:40 +0100146 : mFlags(t) {}
147
148 class Iterator {
149 // The type can't be larger than 64-bits otherwise it won't fit in BitSet64.
150 static_assert(sizeof(U) <= sizeof(uint64_t));
151
152 public:
153 Iterator(Flags<F> flags) : mRemainingFlags(flags.mFlags) { (*this)++; }
154 Iterator() : mRemainingFlags(0), mCurrFlag(static_cast<F>(0)) {}
155
156 // Pre-fix ++
157 Iterator& operator++() {
158 if (mRemainingFlags.isEmpty()) {
159 mCurrFlag = static_cast<F>(0);
160 } else {
161 uint64_t bit = mRemainingFlags.clearLastMarkedBit(); // counts from left
162 const U flag = 1 << (64 - bit - 1);
163 mCurrFlag = static_cast<F>(flag);
164 }
165 return *this;
166 }
167
168 // Post-fix ++
169 Iterator operator++(int) {
170 Iterator iter = *this;
171 ++*this;
172 return iter;
173 }
174
175 bool operator==(Iterator other) const {
176 return mCurrFlag == other.mCurrFlag && mRemainingFlags == other.mRemainingFlags;
177 }
178
179 bool operator!=(Iterator other) const { return !(*this == other); }
180
181 F operator*() { return mCurrFlag; }
182
183 // iterator traits
184
185 // In the future we could make this a bidirectional const iterator instead of a forward
186 // iterator but it doesn't seem worth the added complexity at this point. This could not,
187 // however, be made a non-const iterator as assigning one flag to another is a non-sensical
188 // operation.
189 using iterator_category = std::input_iterator_tag;
190 using value_type = F;
191 // Per the C++ spec, because input iterators are not assignable the iterator's reference
192 // type does not actually need to be a reference. In fact, making it a reference would imply
193 // that modifying it would change the underlying Flags object, which is obviously wrong for
194 // the same reason this can't be a non-const iterator.
195 using reference = F;
196 using difference_type = void;
197 using pointer = void;
198
199 private:
200 BitSet64 mRemainingFlags;
201 F mCurrFlag;
202 };
203
Michael Wright44753b12020-07-08 13:48:11 +0100204 /*
205 * Tests whether the given flag is set.
206 */
207 bool test(F flag) const {
208 U f = static_cast<U>(flag);
Michael Wright75d9e662020-07-11 23:54:40 +0100209 return (f & mFlags) == f;
Michael Wright44753b12020-07-08 13:48:11 +0100210 }
211
212 /* Tests whether any of the given flags are set */
Michael Wright75d9e662020-07-11 23:54:40 +0100213 bool any(Flags<F> f) { return (mFlags & f.mFlags) != 0; }
Michael Wright44753b12020-07-08 13:48:11 +0100214
215 /* Tests whether all of the given flags are set */
Michael Wright75d9e662020-07-11 23:54:40 +0100216 bool all(Flags<F> f) { return (mFlags & f.mFlags) == f.mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100217
Michael Wright75d9e662020-07-11 23:54:40 +0100218 Flags<F> operator|(Flags<F> rhs) const { return static_cast<F>(mFlags | rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100219 Flags<F>& operator|=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100220 mFlags = mFlags | rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100221 return *this;
222 }
223
Michael Wright75d9e662020-07-11 23:54:40 +0100224 Flags<F> operator&(Flags<F> rhs) const { return static_cast<F>(mFlags & rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100225 Flags<F>& operator&=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100226 mFlags = mFlags & rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100227 return *this;
228 }
229
Michael Wright75d9e662020-07-11 23:54:40 +0100230 Flags<F> operator^(Flags<F> rhs) const { return static_cast<F>(mFlags ^ rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100231 Flags<F>& operator^=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100232 mFlags = mFlags ^ rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100233 return *this;
234 }
235
Michael Wright75d9e662020-07-11 23:54:40 +0100236 Flags<F> operator~() { return static_cast<F>(~mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100237
Michael Wright75d9e662020-07-11 23:54:40 +0100238 bool operator==(Flags<F> rhs) const { return mFlags == rhs.mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100239 bool operator!=(Flags<F> rhs) const { return !operator==(rhs); }
240
241 Flags<F>& operator=(const Flags<F>& rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100242 mFlags = rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100243 return *this;
244 }
245
Michael Wright75d9e662020-07-11 23:54:40 +0100246 Iterator begin() const { return Iterator(*this); }
247
248 Iterator end() const { return Iterator(); }
249
Michael Wright44753b12020-07-08 13:48:11 +0100250 /*
251 * Returns the stored set of flags.
252 *
253 * Note that this returns the underlying type rather than the base enum class. This is because
254 * the value is no longer necessarily a strict member of the enum since the returned value could
255 * be multiple enum variants OR'd together.
256 */
Michael Wright75d9e662020-07-11 23:54:40 +0100257 U get() const { return mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100258
Michael Wright8759d672020-07-21 00:46:45 +0100259 std::string string() const {
Michael Wright44753b12020-07-08 13:48:11 +0100260 std::string result;
261 bool first = true;
262 U unstringified = 0;
Michael Wright75d9e662020-07-11 23:54:40 +0100263 for (const F f : *this) {
Michael Wright8759d672020-07-21 00:46:45 +0100264 std::optional<std::string_view> flagString = flag_name(f);
Michael Wright44753b12020-07-08 13:48:11 +0100265 if (flagString) {
266 appendFlag(result, flagString.value(), first);
267 } else {
Michael Wright75d9e662020-07-11 23:54:40 +0100268 unstringified |= static_cast<U>(f);
Michael Wright44753b12020-07-08 13:48:11 +0100269 }
270 }
271
272 if (unstringified != 0) {
273 appendFlag(result, base::StringPrintf("0x%08x", unstringified), first);
274 }
275
276 if (first) {
277 result += "0x0";
278 }
279
280 return result;
281 }
282
283private:
Michael Wright75d9e662020-07-11 23:54:40 +0100284 U mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100285
Michael Wright8759d672020-07-21 00:46:45 +0100286 static void appendFlag(std::string& str, const std::string_view& flag, bool& first) {
Michael Wright44753b12020-07-08 13:48:11 +0100287 if (first) {
288 first = false;
289 } else {
290 str += " | ";
291 }
292 str += flag;
293 }
294};
295
296// This namespace provides operator overloads for enum classes to make it easier to work with them
297// as flags. In order to use these, add them via a `using namespace` declaration.
298namespace flag_operators {
299
Michael Wright8759d672020-07-21 00:46:45 +0100300template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>>
Michael Wright44753b12020-07-08 13:48:11 +0100301inline Flags<F> operator~(F f) {
302 using U = typename std::underlying_type_t<F>;
303 return static_cast<F>(~static_cast<U>(f));
304}
Michael Wright8759d672020-07-21 00:46:45 +0100305template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>>
Michael Wright44753b12020-07-08 13:48:11 +0100306Flags<F> operator|(F lhs, F rhs) {
307 using U = typename std::underlying_type_t<F>;
308 return static_cast<F>(static_cast<U>(lhs) | static_cast<U>(rhs));
309}
310
311} // namespace flag_operators
312} // namespace android
313
314#endif // __UI_INPUT_FLAGS_H