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 | |
| 19 | #include <cstdint> |
| 20 | #include <optional> |
| 21 | #include <string> |
| 22 | #include <type_traits> |
| 23 | |
| 24 | #include "utils/BitSet.h" |
| 25 | |
| 26 | #ifndef __UI_INPUT_FLAGS_H |
| 27 | #define __UI_INPUT_FLAGS_H |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | // A trait for determining whether a type is specifically an enum class or not. |
| 32 | template <typename T, bool = std::is_enum_v<T>> |
| 33 | struct is_enum_class : std::false_type {}; |
| 34 | |
| 35 | // By definition, an enum class is an enum that is not implicitly convertible to its underlying |
| 36 | // type. |
| 37 | template <typename T> |
| 38 | struct is_enum_class<T, true> |
| 39 | : std::bool_constant<!std::is_convertible_v<T, std::underlying_type_t<T>>> {}; |
| 40 | |
| 41 | template <typename T> |
| 42 | inline constexpr bool is_enum_class_v = is_enum_class<T>::value; |
| 43 | |
| 44 | /* A class for handling flags defined by an enum or enum class in a type-safe way. */ |
| 45 | template <class F, typename = std::enable_if_t<std::is_enum_v<F>>> |
| 46 | class Flags { |
| 47 | // F must be an enum or its underlying type is undefined. Theoretically we could specialize this |
| 48 | // further to avoid this restriction but in general we want to encourage the use of enums |
| 49 | // anyways. |
| 50 | using U = typename std::underlying_type_t<F>; |
| 51 | |
| 52 | public: |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 53 | constexpr Flags(F f) : mFlags(static_cast<U>(f)) {} |
| 54 | constexpr Flags() : mFlags(0) {} |
| 55 | constexpr Flags(const Flags<F>& f) : mFlags(f.mFlags) {} |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 56 | |
| 57 | // Provide a non-explicit construct for non-enum classes since they easily convert to their |
| 58 | // underlying types (e.g. when used with bitwise operators). For enum classes, however, we |
| 59 | // should force them to be explicitly constructed from their underlying types to make full use |
| 60 | // of the type checker. |
| 61 | template <typename T = U> |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 62 | constexpr Flags(T t, typename std::enable_if_t<!is_enum_class_v<F>, T>* = nullptr) |
| 63 | : mFlags(t) {} |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 64 | template <typename T = U> |
| 65 | explicit constexpr Flags(T t, typename std::enable_if_t<is_enum_class_v<F>, T>* = nullptr) |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 66 | : mFlags(t) {} |
| 67 | |
| 68 | class Iterator { |
| 69 | // The type can't be larger than 64-bits otherwise it won't fit in BitSet64. |
| 70 | static_assert(sizeof(U) <= sizeof(uint64_t)); |
| 71 | |
| 72 | public: |
| 73 | Iterator(Flags<F> flags) : mRemainingFlags(flags.mFlags) { (*this)++; } |
| 74 | Iterator() : mRemainingFlags(0), mCurrFlag(static_cast<F>(0)) {} |
| 75 | |
| 76 | // Pre-fix ++ |
| 77 | Iterator& operator++() { |
| 78 | if (mRemainingFlags.isEmpty()) { |
| 79 | mCurrFlag = static_cast<F>(0); |
| 80 | } else { |
| 81 | uint64_t bit = mRemainingFlags.clearLastMarkedBit(); // counts from left |
| 82 | const U flag = 1 << (64 - bit - 1); |
| 83 | mCurrFlag = static_cast<F>(flag); |
| 84 | } |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | // Post-fix ++ |
| 89 | Iterator operator++(int) { |
| 90 | Iterator iter = *this; |
| 91 | ++*this; |
| 92 | return iter; |
| 93 | } |
| 94 | |
| 95 | bool operator==(Iterator other) const { |
| 96 | return mCurrFlag == other.mCurrFlag && mRemainingFlags == other.mRemainingFlags; |
| 97 | } |
| 98 | |
| 99 | bool operator!=(Iterator other) const { return !(*this == other); } |
| 100 | |
| 101 | F operator*() { return mCurrFlag; } |
| 102 | |
| 103 | // iterator traits |
| 104 | |
| 105 | // In the future we could make this a bidirectional const iterator instead of a forward |
| 106 | // iterator but it doesn't seem worth the added complexity at this point. This could not, |
| 107 | // however, be made a non-const iterator as assigning one flag to another is a non-sensical |
| 108 | // operation. |
| 109 | using iterator_category = std::input_iterator_tag; |
| 110 | using value_type = F; |
| 111 | // Per the C++ spec, because input iterators are not assignable the iterator's reference |
| 112 | // type does not actually need to be a reference. In fact, making it a reference would imply |
| 113 | // that modifying it would change the underlying Flags object, which is obviously wrong for |
| 114 | // the same reason this can't be a non-const iterator. |
| 115 | using reference = F; |
| 116 | using difference_type = void; |
| 117 | using pointer = void; |
| 118 | |
| 119 | private: |
| 120 | BitSet64 mRemainingFlags; |
| 121 | F mCurrFlag; |
| 122 | }; |
| 123 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 124 | /* |
| 125 | * Tests whether the given flag is set. |
| 126 | */ |
| 127 | bool test(F flag) const { |
| 128 | U f = static_cast<U>(flag); |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 129 | return (f & mFlags) == f; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* Tests whether any of the given flags are set */ |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 133 | bool any(Flags<F> f) { return (mFlags & f.mFlags) != 0; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 134 | |
| 135 | /* Tests whether all of the given flags are set */ |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 136 | bool all(Flags<F> f) { return (mFlags & f.mFlags) == f.mFlags; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 137 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 138 | 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] | 139 | Flags<F>& operator|=(Flags<F> rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 140 | mFlags = mFlags | rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 141 | return *this; |
| 142 | } |
| 143 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 144 | 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] | 145 | Flags<F>& operator&=(Flags<F> rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 146 | mFlags = mFlags & rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 147 | return *this; |
| 148 | } |
| 149 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 150 | 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] | 151 | Flags<F>& operator^=(Flags<F> rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 152 | mFlags = mFlags ^ rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 153 | return *this; |
| 154 | } |
| 155 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 156 | Flags<F> operator~() { return static_cast<F>(~mFlags); } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 157 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 158 | bool operator==(Flags<F> rhs) const { return mFlags == rhs.mFlags; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 159 | bool operator!=(Flags<F> rhs) const { return !operator==(rhs); } |
| 160 | |
| 161 | Flags<F>& operator=(const Flags<F>& rhs) { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 162 | mFlags = rhs.mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 163 | return *this; |
| 164 | } |
| 165 | |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 166 | Iterator begin() const { return Iterator(*this); } |
| 167 | |
| 168 | Iterator end() const { return Iterator(); } |
| 169 | |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 170 | /* |
| 171 | * Returns the stored set of flags. |
| 172 | * |
| 173 | * Note that this returns the underlying type rather than the base enum class. This is because |
| 174 | * the value is no longer necessarily a strict member of the enum since the returned value could |
| 175 | * be multiple enum variants OR'd together. |
| 176 | */ |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 177 | U get() const { return mFlags; } |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 178 | |
| 179 | std::string string() const { return string(defaultStringify); } |
| 180 | |
| 181 | std::string string(std::function<std::optional<std::string>(F)> stringify) const { |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 182 | std::string result; |
| 183 | bool first = true; |
| 184 | U unstringified = 0; |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 185 | for (const F f : *this) { |
| 186 | std::optional<std::string> flagString = stringify(f); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 187 | if (flagString) { |
| 188 | appendFlag(result, flagString.value(), first); |
| 189 | } else { |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 190 | unstringified |= static_cast<U>(f); |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | |
| 194 | if (unstringified != 0) { |
| 195 | appendFlag(result, base::StringPrintf("0x%08x", unstringified), first); |
| 196 | } |
| 197 | |
| 198 | if (first) { |
| 199 | result += "0x0"; |
| 200 | } |
| 201 | |
| 202 | return result; |
| 203 | } |
| 204 | |
| 205 | private: |
Michael Wright | 75d9e66 | 2020-07-11 23:54:40 +0100 | [diff] [blame^] | 206 | U mFlags; |
Michael Wright | 44753b1 | 2020-07-08 13:48:11 +0100 | [diff] [blame] | 207 | |
| 208 | static std::optional<std::string> defaultStringify(F) { return std::nullopt; } |
| 209 | static void appendFlag(std::string& str, const std::string& flag, bool& first) { |
| 210 | if (first) { |
| 211 | first = false; |
| 212 | } else { |
| 213 | str += " | "; |
| 214 | } |
| 215 | str += flag; |
| 216 | } |
| 217 | }; |
| 218 | |
| 219 | // This namespace provides operator overloads for enum classes to make it easier to work with them |
| 220 | // as flags. In order to use these, add them via a `using namespace` declaration. |
| 221 | namespace flag_operators { |
| 222 | |
| 223 | template <typename F, typename = std::enable_if_t<is_enum_class_v<F>>> |
| 224 | inline Flags<F> operator~(F f) { |
| 225 | using U = typename std::underlying_type_t<F>; |
| 226 | return static_cast<F>(~static_cast<U>(f)); |
| 227 | } |
| 228 | template <typename F, typename = std::enable_if_t<is_enum_class_v<F>>> |
| 229 | Flags<F> operator|(F lhs, F rhs) { |
| 230 | using U = typename std::underlying_type_t<F>; |
| 231 | return static_cast<F>(static_cast<U>(lhs) | static_cast<U>(rhs)); |
| 232 | } |
| 233 | |
| 234 | } // namespace flag_operators |
| 235 | } // namespace android |
| 236 | |
| 237 | #endif // __UI_INPUT_FLAGS_H |