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 | |
| 25 | #include "utils/BitSet.h" |
| 26 | |
| 27 | #ifndef __UI_INPUT_FLAGS_H |
| 28 | #define __UI_INPUT_FLAGS_H |
| 29 | |
| 30 | namespace android { |
| 31 | |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 32 | namespace details { |
| 33 | template <typename F, F V> |
| 34 | constexpr 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 | |
| 66 | template <typename F> |
| 67 | inline constexpr auto flag_count = sizeof(F) * __CHAR_BIT__; |
| 68 | |
| 69 | template <typename F, typename T, T... I> |
| 70 | constexpr auto generate_flag_values(std::integer_sequence<T, I...> seq) { |
Michael Wright | 74c8242 | 2020-07-23 15:30:21 +0100 | [diff] [blame] | 71 | constexpr size_t count = seq.size(); |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 72 | |
| 73 | std::array<F, count> values{}; |
Michael Wright | 74c8242 | 2020-07-23 15:30:21 +0100 | [diff] [blame] | 74 | for (size_t i = 0, v = 0; v < count; ++i) { |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 75 | values[v++] = static_cast<F>(T{1} << i); |
| 76 | } |
| 77 | |
| 78 | return values; |
| 79 | } |
| 80 | |
| 81 | template <typename F> |
| 82 | inline constexpr auto flag_values = generate_flag_values<F>( |
| 83 | std::make_integer_sequence<std::underlying_type_t<F>, flag_count<F>>{}); |
| 84 | |
| 85 | template <typename F, std::size_t... I> |
| 86 | constexpr 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 | |
| 91 | template <typename F> |
| 92 | inline constexpr auto flag_names = |
| 93 | generate_flag_names<F>(std::make_index_sequence<flag_count<F>>{}); |
| 94 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 95 | // A trait for determining whether a type is specifically an enum class or not. |
| 96 | template <typename T, bool = std::is_enum_v<T>> |
| 97 | struct 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. |
| 101 | template <typename T> |
| 102 | struct is_enum_class<T, true> |
| 103 | : std::bool_constant<!std::is_convertible_v<T, std::underlying_type_t<T>>> {}; |
| 104 | |
| 105 | template <typename T> |
| 106 | inline constexpr bool is_enum_class_v = is_enum_class<T>::value; |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 107 | } // namespace details |
| 108 | |
| 109 | template <auto V> |
| 110 | constexpr auto flag_name() { |
| 111 | using F = decltype(V); |
| 112 | return details::enum_value_name<F, V>(); |
| 113 | } |
| 114 | |
| 115 | template <typename F> |
| 116 | constexpr 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 Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 121 | |
| 122 | /* 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] | 123 | template <typename F> |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 124 | class 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 Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 128 | 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] | 129 | using U = typename std::underlying_type_t<F>; |
| 130 | |
| 131 | public: |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 132 | 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 Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 135 | |
| 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 Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 141 | 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] | 142 | : mFlags(t) {} |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 143 | template <typename T = U> |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 144 | explicit constexpr Flags(T t, |
| 145 | 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] | 146 | : 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 Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 204 | /* |
| 205 | * Tests whether the given flag is set. |
| 206 | */ |
| 207 | bool test(F flag) const { |
| 208 | U f = static_cast<U>(flag); |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 209 | return (f & mFlags) == f; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* Tests whether any of the given flags are set */ |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 213 | bool any(Flags<F> f) { return (mFlags & f.mFlags) != 0; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 214 | |
| 215 | /* Tests whether all of the given flags are set */ |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 216 | bool all(Flags<F> f) { return (mFlags & f.mFlags) == f.mFlags; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 217 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 218 | 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] | 219 | Flags<F>& operator|=(Flags<F> rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 220 | mFlags = mFlags | rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 221 | return *this; |
| 222 | } |
| 223 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 224 | 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] | 225 | Flags<F>& operator&=(Flags<F> rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 226 | mFlags = mFlags & rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 227 | return *this; |
| 228 | } |
| 229 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 230 | 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] | 231 | Flags<F>& operator^=(Flags<F> rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 232 | mFlags = mFlags ^ rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 233 | return *this; |
| 234 | } |
| 235 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 236 | Flags<F> operator~() { return static_cast<F>(~mFlags); } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 237 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 238 | bool operator==(Flags<F> rhs) const { return mFlags == rhs.mFlags; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 239 | bool operator!=(Flags<F> rhs) const { return !operator==(rhs); } |
| 240 | |
| 241 | Flags<F>& operator=(const Flags<F>& rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 242 | mFlags = rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 243 | return *this; |
| 244 | } |
| 245 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 246 | Iterator begin() const { return Iterator(*this); } |
| 247 | |
| 248 | Iterator end() const { return Iterator(); } |
| 249 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 250 | /* |
| 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 Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 257 | U get() const { return mFlags; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 258 | |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 259 | std::string string() const { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 260 | std::string result; |
| 261 | bool first = true; |
| 262 | U unstringified = 0; |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 263 | for (const F f : *this) { |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 264 | std::optional<std::string_view> flagString = flag_name(f); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 265 | if (flagString) { |
| 266 | appendFlag(result, flagString.value(), first); |
| 267 | } else { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 268 | unstringified |= static_cast<U>(f); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 269 | } |
| 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 | |
| 283 | private: |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame] | 284 | U mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 285 | |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 286 | 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] | 287 | 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. |
| 298 | namespace flag_operators { |
| 299 | |
Michael Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 300 | 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] | 301 | inline 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 Wright | 8759d67 | 2020-07-21 00:46:45 +0100 | [diff] [blame] | 305 | 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] | 306 | Flags<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 |