blob: 27c84769cb08f3bd2f5de1fd624f388c4a22fe0a [file] [log] [blame]
Michael Wright44753b12020-07-08 13:48:11 +01001/*
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 Wright8759d672020-07-21 00:46:45 +010019#include <array>
Michael Wright44753b12020-07-08 13:48:11 +010020#include <cstdint>
21#include <optional>
22#include <string>
23#include <type_traits>
24
chaviw98318de2021-05-19 16:45:23 -050025#include <ftl/NamedEnum.h>
Michael Wright44753b12020-07-08 13:48:11 +010026#include "utils/BitSet.h"
27
chaviw98318de2021-05-19 16:45:23 -050028#pragma once
Michael Wright44753b12020-07-08 13:48:11 +010029
30namespace android {
31
Michael Wright8759d672020-07-21 00:46:45 +010032namespace details {
Michael Wright8759d672020-07-21 00:46:45 +010033
34template <typename F>
35inline constexpr auto flag_count = sizeof(F) * __CHAR_BIT__;
36
37template <typename F, typename T, T... I>
38constexpr auto generate_flag_values(std::integer_sequence<T, I...> seq) {
Michael Wright74c82422020-07-23 15:30:21 +010039 constexpr size_t count = seq.size();
Michael Wright8759d672020-07-21 00:46:45 +010040
41 std::array<F, count> values{};
Michael Wright74c82422020-07-23 15:30:21 +010042 for (size_t i = 0, v = 0; v < count; ++i) {
Michael Wright8759d672020-07-21 00:46:45 +010043 values[v++] = static_cast<F>(T{1} << i);
44 }
45
46 return values;
47}
48
49template <typename F>
50inline constexpr auto flag_values = generate_flag_values<F>(
51 std::make_integer_sequence<std::underlying_type_t<F>, flag_count<F>>{});
52
53template <typename F, std::size_t... I>
54constexpr 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
59template <typename F>
60inline constexpr auto flag_names =
61 generate_flag_names<F>(std::make_index_sequence<flag_count<F>>{});
62
Michael Wright44753b12020-07-08 13:48:11 +010063// A trait for determining whether a type is specifically an enum class or not.
64template <typename T, bool = std::is_enum_v<T>>
65struct 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.
69template <typename T>
70struct is_enum_class<T, true>
71 : std::bool_constant<!std::is_convertible_v<T, std::underlying_type_t<T>>> {};
72
73template <typename T>
74inline constexpr bool is_enum_class_v = is_enum_class<T>::value;
Michael Wright8759d672020-07-21 00:46:45 +010075} // namespace details
76
77template <auto V>
78constexpr auto flag_name() {
79 using F = decltype(V);
80 return details::enum_value_name<F, V>();
81}
82
83template <typename F>
84constexpr std::optional<std::string_view> flag_name(F flag) {
85 using U = std::underlying_type_t<F>;
Dan Stoza269dc4d2021-01-15 15:07:43 -080086 auto idx = static_cast<size_t>(__builtin_ctzl(static_cast<U>(flag)));
Michael Wright8759d672020-07-21 00:46:45 +010087 return details::flag_names<F>[idx];
88}
Michael Wright44753b12020-07-08 13:48:11 +010089
90/* A class for handling flags defined by an enum or enum class in a type-safe way. */
Michael Wright8759d672020-07-21 00:46:45 +010091template <typename F>
Michael Wright44753b12020-07-08 13:48:11 +010092class 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 Wright8759d672020-07-21 00:46:45 +010096 static_assert(std::is_enum_v<F>, "Flags type must be an enum");
Michael Wright44753b12020-07-08 13:48:11 +010097 using U = typename std::underlying_type_t<F>;
98
99public:
Michael Wright75d9e662020-07-11 23:54:40 +0100100 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 Wright44753b12020-07-08 13:48:11 +0100103
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 Wright8759d672020-07-21 00:46:45 +0100109 constexpr Flags(T t, typename std::enable_if_t<!details::is_enum_class_v<F>, T>* = nullptr)
Michael Wright75d9e662020-07-11 23:54:40 +0100110 : mFlags(t) {}
Michael Wright44753b12020-07-08 13:48:11 +0100111 template <typename T = U>
Michael Wright8759d672020-07-21 00:46:45 +0100112 explicit constexpr Flags(T t,
113 typename std::enable_if_t<details::is_enum_class_v<F>, T>* = nullptr)
Michael Wright75d9e662020-07-11 23:54:40 +0100114 : 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 Wright44753b12020-07-08 13:48:11 +0100172 /*
173 * Tests whether the given flag is set.
174 */
175 bool test(F flag) const {
176 U f = static_cast<U>(flag);
Michael Wright75d9e662020-07-11 23:54:40 +0100177 return (f & mFlags) == f;
Michael Wright44753b12020-07-08 13:48:11 +0100178 }
179
180 /* Tests whether any of the given flags are set */
Michael Wright75d9e662020-07-11 23:54:40 +0100181 bool any(Flags<F> f) { return (mFlags & f.mFlags) != 0; }
Michael Wright44753b12020-07-08 13:48:11 +0100182
183 /* Tests whether all of the given flags are set */
Michael Wright75d9e662020-07-11 23:54:40 +0100184 bool all(Flags<F> f) { return (mFlags & f.mFlags) == f.mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100185
Michael Wright75d9e662020-07-11 23:54:40 +0100186 Flags<F> operator|(Flags<F> rhs) const { return static_cast<F>(mFlags | rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100187 Flags<F>& operator|=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100188 mFlags = mFlags | rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100189 return *this;
190 }
191
Michael Wright75d9e662020-07-11 23:54:40 +0100192 Flags<F> operator&(Flags<F> rhs) const { return static_cast<F>(mFlags & rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100193 Flags<F>& operator&=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100194 mFlags = mFlags & rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100195 return *this;
196 }
197
Michael Wright75d9e662020-07-11 23:54:40 +0100198 Flags<F> operator^(Flags<F> rhs) const { return static_cast<F>(mFlags ^ rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100199 Flags<F>& operator^=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100200 mFlags = mFlags ^ rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100201 return *this;
202 }
203
Michael Wright75d9e662020-07-11 23:54:40 +0100204 Flags<F> operator~() { return static_cast<F>(~mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100205
Michael Wright75d9e662020-07-11 23:54:40 +0100206 bool operator==(Flags<F> rhs) const { return mFlags == rhs.mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100207 bool operator!=(Flags<F> rhs) const { return !operator==(rhs); }
208
209 Flags<F>& operator=(const Flags<F>& rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100210 mFlags = rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100211 return *this;
212 }
213
Michael Wright75d9e662020-07-11 23:54:40 +0100214 Iterator begin() const { return Iterator(*this); }
215
216 Iterator end() const { return Iterator(); }
217
Michael Wright44753b12020-07-08 13:48:11 +0100218 /*
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 Wright75d9e662020-07-11 23:54:40 +0100225 U get() const { return mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100226
Michael Wright8759d672020-07-21 00:46:45 +0100227 std::string string() const {
Michael Wright44753b12020-07-08 13:48:11 +0100228 std::string result;
229 bool first = true;
230 U unstringified = 0;
Michael Wright75d9e662020-07-11 23:54:40 +0100231 for (const F f : *this) {
Michael Wright8759d672020-07-21 00:46:45 +0100232 std::optional<std::string_view> flagString = flag_name(f);
Michael Wright44753b12020-07-08 13:48:11 +0100233 if (flagString) {
234 appendFlag(result, flagString.value(), first);
235 } else {
Michael Wright75d9e662020-07-11 23:54:40 +0100236 unstringified |= static_cast<U>(f);
Michael Wright44753b12020-07-08 13:48:11 +0100237 }
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
251private:
Michael Wright75d9e662020-07-11 23:54:40 +0100252 U mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100253
Michael Wright8759d672020-07-21 00:46:45 +0100254 static void appendFlag(std::string& str, const std::string_view& flag, bool& first) {
Michael Wright44753b12020-07-08 13:48:11 +0100255 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.
266namespace flag_operators {
267
Michael Wright8759d672020-07-21 00:46:45 +0100268template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>>
Michael Wright44753b12020-07-08 13:48:11 +0100269inline 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 Wright8759d672020-07-21 00:46:45 +0100273template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>>
Michael Wright44753b12020-07-08 13:48:11 +0100274Flags<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