blob: 072dd18cec672c006291723bf77c1e3fd16327ee [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
Chris Ye173871c2020-09-08 09:55:14 -070025#include "NamedEnum.h"
Michael Wright44753b12020-07-08 13:48:11 +010026#include "utils/BitSet.h"
27
28#ifndef __UI_INPUT_FLAGS_H
29#define __UI_INPUT_FLAGS_H
30
31namespace android {
32
Michael Wright8759d672020-07-21 00:46:45 +010033namespace details {
Michael Wright8759d672020-07-21 00:46:45 +010034
35template <typename F>
36inline constexpr auto flag_count = sizeof(F) * __CHAR_BIT__;
37
38template <typename F, typename T, T... I>
39constexpr auto generate_flag_values(std::integer_sequence<T, I...> seq) {
Michael Wright74c82422020-07-23 15:30:21 +010040 constexpr size_t count = seq.size();
Michael Wright8759d672020-07-21 00:46:45 +010041
42 std::array<F, count> values{};
Michael Wright74c82422020-07-23 15:30:21 +010043 for (size_t i = 0, v = 0; v < count; ++i) {
Michael Wright8759d672020-07-21 00:46:45 +010044 values[v++] = static_cast<F>(T{1} << i);
45 }
46
47 return values;
48}
49
50template <typename F>
51inline constexpr auto flag_values = generate_flag_values<F>(
52 std::make_integer_sequence<std::underlying_type_t<F>, flag_count<F>>{});
53
54template <typename F, std::size_t... I>
55constexpr 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
60template <typename F>
61inline constexpr auto flag_names =
62 generate_flag_names<F>(std::make_index_sequence<flag_count<F>>{});
63
Michael Wright44753b12020-07-08 13:48:11 +010064// A trait for determining whether a type is specifically an enum class or not.
65template <typename T, bool = std::is_enum_v<T>>
66struct 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.
70template <typename T>
71struct is_enum_class<T, true>
72 : std::bool_constant<!std::is_convertible_v<T, std::underlying_type_t<T>>> {};
73
74template <typename T>
75inline constexpr bool is_enum_class_v = is_enum_class<T>::value;
Michael Wright8759d672020-07-21 00:46:45 +010076} // namespace details
77
78template <auto V>
79constexpr auto flag_name() {
80 using F = decltype(V);
81 return details::enum_value_name<F, V>();
82}
83
84template <typename F>
85constexpr std::optional<std::string_view> flag_name(F flag) {
86 using U = std::underlying_type_t<F>;
87 auto idx = __builtin_ctzl(static_cast<U>(flag));
88 return details::flag_names<F>[idx];
89}
Michael Wright44753b12020-07-08 13:48:11 +010090
91/* A class for handling flags defined by an enum or enum class in a type-safe way. */
Michael Wright8759d672020-07-21 00:46:45 +010092template <typename F>
Michael Wright44753b12020-07-08 13:48:11 +010093class 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.
Michael Wright8759d672020-07-21 00:46:45 +010097 static_assert(std::is_enum_v<F>, "Flags type must be an enum");
Michael Wright44753b12020-07-08 13:48:11 +010098 using U = typename std::underlying_type_t<F>;
99
100public:
Michael Wright75d9e662020-07-11 23:54:40 +0100101 constexpr Flags(F f) : mFlags(static_cast<U>(f)) {}
102 constexpr Flags() : mFlags(0) {}
103 constexpr Flags(const Flags<F>& f) : mFlags(f.mFlags) {}
Michael Wright44753b12020-07-08 13:48:11 +0100104
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>
Michael Wright8759d672020-07-21 00:46:45 +0100110 constexpr Flags(T t, typename std::enable_if_t<!details::is_enum_class_v<F>, T>* = nullptr)
Michael Wright75d9e662020-07-11 23:54:40 +0100111 : mFlags(t) {}
Michael Wright44753b12020-07-08 13:48:11 +0100112 template <typename T = U>
Michael Wright8759d672020-07-21 00:46:45 +0100113 explicit constexpr Flags(T t,
114 typename std::enable_if_t<details::is_enum_class_v<F>, T>* = nullptr)
Michael Wright75d9e662020-07-11 23:54:40 +0100115 : 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
Michael Wright44753b12020-07-08 13:48:11 +0100173 /*
174 * Tests whether the given flag is set.
175 */
176 bool test(F flag) const {
177 U f = static_cast<U>(flag);
Michael Wright75d9e662020-07-11 23:54:40 +0100178 return (f & mFlags) == f;
Michael Wright44753b12020-07-08 13:48:11 +0100179 }
180
181 /* Tests whether any of the given flags are set */
Michael Wright75d9e662020-07-11 23:54:40 +0100182 bool any(Flags<F> f) { return (mFlags & f.mFlags) != 0; }
Michael Wright44753b12020-07-08 13:48:11 +0100183
184 /* Tests whether all of the given flags are set */
Michael Wright75d9e662020-07-11 23:54:40 +0100185 bool all(Flags<F> f) { return (mFlags & f.mFlags) == f.mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100186
Michael Wright75d9e662020-07-11 23:54:40 +0100187 Flags<F> operator|(Flags<F> rhs) const { return static_cast<F>(mFlags | rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100188 Flags<F>& operator|=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100189 mFlags = mFlags | rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100190 return *this;
191 }
192
Michael Wright75d9e662020-07-11 23:54:40 +0100193 Flags<F> operator&(Flags<F> rhs) const { return static_cast<F>(mFlags & rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100194 Flags<F>& operator&=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100195 mFlags = mFlags & rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100196 return *this;
197 }
198
Michael Wright75d9e662020-07-11 23:54:40 +0100199 Flags<F> operator^(Flags<F> rhs) const { return static_cast<F>(mFlags ^ rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100200 Flags<F>& operator^=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100201 mFlags = mFlags ^ rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100202 return *this;
203 }
204
Michael Wright75d9e662020-07-11 23:54:40 +0100205 Flags<F> operator~() { return static_cast<F>(~mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100206
Michael Wright75d9e662020-07-11 23:54:40 +0100207 bool operator==(Flags<F> rhs) const { return mFlags == rhs.mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100208 bool operator!=(Flags<F> rhs) const { return !operator==(rhs); }
209
210 Flags<F>& operator=(const Flags<F>& rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100211 mFlags = rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100212 return *this;
213 }
214
Michael Wright75d9e662020-07-11 23:54:40 +0100215 Iterator begin() const { return Iterator(*this); }
216
217 Iterator end() const { return Iterator(); }
218
Michael Wright44753b12020-07-08 13:48:11 +0100219 /*
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 */
Michael Wright75d9e662020-07-11 23:54:40 +0100226 U get() const { return mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100227
Michael Wright8759d672020-07-21 00:46:45 +0100228 std::string string() const {
Michael Wright44753b12020-07-08 13:48:11 +0100229 std::string result;
230 bool first = true;
231 U unstringified = 0;
Michael Wright75d9e662020-07-11 23:54:40 +0100232 for (const F f : *this) {
Michael Wright8759d672020-07-21 00:46:45 +0100233 std::optional<std::string_view> flagString = flag_name(f);
Michael Wright44753b12020-07-08 13:48:11 +0100234 if (flagString) {
235 appendFlag(result, flagString.value(), first);
236 } else {
Michael Wright75d9e662020-07-11 23:54:40 +0100237 unstringified |= static_cast<U>(f);
Michael Wright44753b12020-07-08 13:48:11 +0100238 }
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
252private:
Michael Wright75d9e662020-07-11 23:54:40 +0100253 U mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100254
Michael Wright8759d672020-07-21 00:46:45 +0100255 static void appendFlag(std::string& str, const std::string_view& flag, bool& first) {
Michael Wright44753b12020-07-08 13:48:11 +0100256 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.
267namespace flag_operators {
268
Michael Wright8759d672020-07-21 00:46:45 +0100269template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>>
Michael Wright44753b12020-07-08 13:48:11 +0100270inline Flags<F> operator~(F f) {
271 using U = typename std::underlying_type_t<F>;
272 return static_cast<F>(~static_cast<U>(f));
273}
Michael Wright8759d672020-07-21 00:46:45 +0100274template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>>
Michael Wright44753b12020-07-08 13:48:11 +0100275Flags<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