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: |
| 53 | constexpr Flags(F f) : flags(static_cast<U>(f)) {} |
| 54 | constexpr Flags() : flags(0) {} |
| 55 | constexpr Flags(const Flags<F>& f) : flags(f.flags) {} |
| 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> |
| 62 | constexpr Flags(T t, typename std::enable_if_t<!is_enum_class_v<F>, T>* = nullptr) : flags(t) {} |
| 63 | template <typename T = U> |
| 64 | explicit constexpr Flags(T t, typename std::enable_if_t<is_enum_class_v<F>, T>* = nullptr) |
| 65 | : flags(t) {} |
| 66 | /* |
| 67 | * Tests whether the given flag is set. |
| 68 | */ |
| 69 | bool test(F flag) const { |
| 70 | U f = static_cast<U>(flag); |
| 71 | return (f & flags) == f; |
| 72 | } |
| 73 | |
| 74 | /* Tests whether any of the given flags are set */ |
| 75 | bool any(Flags<F> f) { return (flags & f.flags) != 0; } |
| 76 | |
| 77 | /* Tests whether all of the given flags are set */ |
| 78 | bool all(Flags<F> f) { return (flags & f.flags) == f.flags; } |
| 79 | |
| 80 | Flags<F> operator|(Flags<F> rhs) const { return static_cast<F>(flags | rhs.flags); } |
| 81 | Flags<F>& operator|=(Flags<F> rhs) { |
| 82 | flags = flags | rhs.flags; |
| 83 | return *this; |
| 84 | } |
| 85 | |
| 86 | Flags<F> operator&(Flags<F> rhs) const { return static_cast<F>(flags & rhs.flags); } |
| 87 | Flags<F>& operator&=(Flags<F> rhs) { |
| 88 | flags = flags & rhs.flags; |
| 89 | return *this; |
| 90 | } |
| 91 | |
| 92 | Flags<F> operator^(Flags<F> rhs) const { return static_cast<F>(flags ^ rhs.flags); } |
| 93 | Flags<F>& operator^=(Flags<F> rhs) { |
| 94 | flags = flags ^ rhs.flags; |
| 95 | return *this; |
| 96 | } |
| 97 | |
| 98 | Flags<F> operator~() { return static_cast<F>(~flags); } |
| 99 | |
| 100 | bool operator==(Flags<F> rhs) const { return flags == rhs.flags; } |
| 101 | bool operator!=(Flags<F> rhs) const { return !operator==(rhs); } |
| 102 | |
| 103 | Flags<F>& operator=(const Flags<F>& rhs) { |
| 104 | flags = rhs.flags; |
| 105 | return *this; |
| 106 | } |
| 107 | |
| 108 | /* |
| 109 | * Returns the stored set of flags. |
| 110 | * |
| 111 | * Note that this returns the underlying type rather than the base enum class. This is because |
| 112 | * the value is no longer necessarily a strict member of the enum since the returned value could |
| 113 | * be multiple enum variants OR'd together. |
| 114 | */ |
| 115 | U get() const { return flags; } |
| 116 | |
| 117 | std::string string() const { return string(defaultStringify); } |
| 118 | |
| 119 | std::string string(std::function<std::optional<std::string>(F)> stringify) const { |
| 120 | // The type can't be larger than 64-bits otherwise it won't fit in BitSet64. |
| 121 | static_assert(sizeof(U) <= sizeof(uint64_t)); |
| 122 | std::string result; |
| 123 | bool first = true; |
| 124 | U unstringified = 0; |
| 125 | for (BitSet64 bits(flags); !bits.isEmpty();) { |
| 126 | uint64_t bit = bits.clearLastMarkedBit(); // counts from left |
| 127 | const U flag = 1 << (64 - bit - 1); |
| 128 | std::optional<std::string> flagString = stringify(static_cast<F>(flag)); |
| 129 | if (flagString) { |
| 130 | appendFlag(result, flagString.value(), first); |
| 131 | } else { |
| 132 | unstringified |= flag; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (unstringified != 0) { |
| 137 | appendFlag(result, base::StringPrintf("0x%08x", unstringified), first); |
| 138 | } |
| 139 | |
| 140 | if (first) { |
| 141 | result += "0x0"; |
| 142 | } |
| 143 | |
| 144 | return result; |
| 145 | } |
| 146 | |
| 147 | private: |
| 148 | U flags; |
| 149 | |
| 150 | static std::optional<std::string> defaultStringify(F) { return std::nullopt; } |
| 151 | static void appendFlag(std::string& str, const std::string& flag, bool& first) { |
| 152 | if (first) { |
| 153 | first = false; |
| 154 | } else { |
| 155 | str += " | "; |
| 156 | } |
| 157 | str += flag; |
| 158 | } |
| 159 | }; |
| 160 | |
| 161 | // This namespace provides operator overloads for enum classes to make it easier to work with them |
| 162 | // as flags. In order to use these, add them via a `using namespace` declaration. |
| 163 | namespace flag_operators { |
| 164 | |
| 165 | template <typename F, typename = std::enable_if_t<is_enum_class_v<F>>> |
| 166 | inline Flags<F> operator~(F f) { |
| 167 | using U = typename std::underlying_type_t<F>; |
| 168 | return static_cast<F>(~static_cast<U>(f)); |
| 169 | } |
| 170 | template <typename F, typename = std::enable_if_t<is_enum_class_v<F>>> |
| 171 | Flags<F> operator|(F lhs, F rhs) { |
| 172 | using U = typename std::underlying_type_t<F>; |
| 173 | return static_cast<F>(static_cast<U>(lhs) | static_cast<U>(rhs)); |
| 174 | } |
| 175 | |
| 176 | } // namespace flag_operators |
| 177 | } // namespace android |
| 178 | |
| 179 | #endif // __UI_INPUT_FLAGS_H |