Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 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 | |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 19 | #include <array> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 20 | #include <cstdint> |
| 21 | #include <optional> |
| 22 | #include <string> |
| 23 | #include <type_traits> |
| 24 | |
Chris Ye | 173871c | 2020-09-08 09:55:14 -0700 | [diff] [blame] | 25 | #include "NamedEnum.h" |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 26 | #include "utils/BitSet.h" |
| 27 | |
| 28 | #ifndef __UI_INPUT_FLAGS_H |
| 29 | #define __UI_INPUT_FLAGS_H |
| 30 | |
| 31 | namespace android { |
| 32 | |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 33 | namespace details { |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 34 | |
| 35 | template <typename F> |
| 36 | inline constexpr auto flag_count = sizeof(F) * __CHAR_BIT__; |
| 37 | |
| 38 | template <typename F, typename T, T... I> |
| 39 | constexpr auto generate_flag_values(std::integer_sequence<T, I...> seq) { |
Michael Wright | 74c8242 | 2020-07-23 15:30:21 +0100 | [diff] [blame] | 40 | constexpr size_t count = seq.size(); |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 41 | |
| 42 | std::array<F, count> values{}; |
Michael Wright | 74c8242 | 2020-07-23 15:30:21 +0100 | [diff] [blame] | 43 | for (size_t i = 0, v = 0; v < count; ++i) { |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 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> |
| 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 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 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; |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 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 = __builtin_ctzl(static_cast<U>(flag)); |
| 88 | return details::flag_names<F>[idx]; |
| 89 | } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 90 | |
| 91 | /* A class for handling flags defined by an enum or enum class in a type-safe way. */ |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 92 | template <typename F> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 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. |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 97 | static_assert(std::is_enum_v<F>, "Flags type must be an enum"); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 98 | using U = typename std::underlying_type_t<F>; |
| 99 | |
| 100 | public: |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 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) {} |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 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> |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 110 | constexpr Flags(T t, typename std::enable_if_t<!details::is_enum_class_v<F>, T>* = nullptr) |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 111 | : mFlags(t) {} |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 112 | template <typename T = U> |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 113 | explicit constexpr Flags(T t, |
| 114 | typename std::enable_if_t<details::is_enum_class_v<F>, T>* = nullptr) |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 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 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 173 | /* |
| 174 | * Tests whether the given flag is set. |
| 175 | */ |
| 176 | bool test(F flag) const { |
| 177 | U f = static_cast<U>(flag); |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 178 | return (f & mFlags) == f; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /* Tests whether any of the given flags are set */ |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 182 | bool any(Flags<F> f) { return (mFlags & f.mFlags) != 0; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 183 | |
| 184 | /* Tests whether all of the given flags are set */ |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 185 | bool all(Flags<F> f) { return (mFlags & f.mFlags) == f.mFlags; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 186 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 187 | Flags<F> operator|(Flags<F> rhs) const { return static_cast<F>(mFlags | rhs.mFlags); } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 188 | Flags<F>& operator|=(Flags<F> rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 189 | mFlags = mFlags | rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 190 | return *this; |
| 191 | } |
| 192 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 193 | Flags<F> operator&(Flags<F> rhs) const { return static_cast<F>(mFlags & rhs.mFlags); } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 194 | Flags<F>& operator&=(Flags<F> rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 195 | mFlags = mFlags & rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 196 | return *this; |
| 197 | } |
| 198 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 199 | Flags<F> operator^(Flags<F> rhs) const { return static_cast<F>(mFlags ^ rhs.mFlags); } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 200 | Flags<F>& operator^=(Flags<F> rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 201 | mFlags = mFlags ^ rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 202 | return *this; |
| 203 | } |
| 204 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 205 | Flags<F> operator~() { return static_cast<F>(~mFlags); } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 206 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 207 | bool operator==(Flags<F> rhs) const { return mFlags == rhs.mFlags; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 208 | bool operator!=(Flags<F> rhs) const { return !operator==(rhs); } |
| 209 | |
| 210 | Flags<F>& operator=(const Flags<F>& rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 211 | mFlags = rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 212 | return *this; |
| 213 | } |
| 214 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 215 | Iterator begin() const { return Iterator(*this); } |
| 216 | |
| 217 | Iterator end() const { return Iterator(); } |
| 218 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 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 | */ |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 226 | U get() const { return mFlags; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 227 | |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 228 | std::string string() const { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 229 | std::string result; |
| 230 | bool first = true; |
| 231 | U unstringified = 0; |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 232 | for (const F f : *this) { |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 233 | std::optional<std::string_view> flagString = flag_name(f); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 234 | if (flagString) { |
| 235 | appendFlag(result, flagString.value(), first); |
| 236 | } else { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 237 | unstringified |= static_cast<U>(f); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 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: |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 253 | U mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 254 | |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 255 | static void appendFlag(std::string& str, const std::string_view& flag, bool& first) { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 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 | |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 269 | template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 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 | } |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 274 | template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 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 |