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