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 <array>
20 #include <cstdint>
21 #include <optional>
22 #include <string>
23 #include <type_traits>
24
25 #include <ftl/NamedEnum.h>
26 #include "utils/BitSet.h"
27
28 #pragma once
29
30 namespace android {
31
32 namespace details {
33
34 template <typename F>
35 inline constexpr auto flag_count = sizeof(F) * __CHAR_BIT__;
36
37 template <typename F, typename T, T... I>
generate_flag_values(std::integer_sequence<T,I...> seq)38 constexpr auto generate_flag_values(std::integer_sequence<T, I...> seq) {
39 constexpr size_t count = seq.size();
40
41 std::array<F, count> values{};
42 for (size_t i = 0, v = 0; v < count; ++i) {
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>
generate_flag_names(std::index_sequence<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
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;
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>;
86 auto idx = static_cast<size_t>(__builtin_ctzl(static_cast<U>(flag)));
87 return details::flag_names<F>[idx];
88 }
89
90 /* A class for handling flags defined by an enum or enum class in a type-safe way. */
91 template <typename F>
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.
96 static_assert(std::is_enum_v<F>, "Flags type must be an enum");
97 using U = typename std::underlying_type_t<F>;
98
99 public:
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) {}
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>
109 constexpr Flags(T t, typename std::enable_if_t<!details::is_enum_class_v<F>, T>* = nullptr)
110 : mFlags(t) {}
111 template <typename T = U>
112 explicit constexpr Flags(T t,
113 typename std::enable_if_t<details::is_enum_class_v<F>, T>* = nullptr)
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
172 /*
173 * Tests whether the given flag is set.
174 */
175 bool test(F flag) const {
176 U f = static_cast<U>(flag);
177 return (f & mFlags) == f;
178 }
179
180 /* Tests whether any of the given flags are set */
181 bool any(Flags<F> f) { return (mFlags & f.mFlags) != 0; }
182
183 /* Tests whether all of the given flags are set */
184 bool all(Flags<F> f) { return (mFlags & f.mFlags) == f.mFlags; }
185
186 Flags<F> operator|(Flags<F> rhs) const { return static_cast<F>(mFlags | rhs.mFlags); }
187 Flags<F>& operator|=(Flags<F> rhs) {
188 mFlags = mFlags | rhs.mFlags;
189 return *this;
190 }
191
192 Flags<F> operator&(Flags<F> rhs) const { return static_cast<F>(mFlags & rhs.mFlags); }
193 Flags<F>& operator&=(Flags<F> rhs) {
194 mFlags = mFlags & rhs.mFlags;
195 return *this;
196 }
197
198 Flags<F> operator^(Flags<F> rhs) const { return static_cast<F>(mFlags ^ rhs.mFlags); }
199 Flags<F>& operator^=(Flags<F> rhs) {
200 mFlags = mFlags ^ rhs.mFlags;
201 return *this;
202 }
203
204 Flags<F> operator~() { return static_cast<F>(~mFlags); }
205
206 bool operator==(Flags<F> rhs) const { return mFlags == rhs.mFlags; }
207 bool operator!=(Flags<F> rhs) const { return !operator==(rhs); }
208
209 Flags<F>& operator=(const Flags<F>& rhs) {
210 mFlags = rhs.mFlags;
211 return *this;
212 }
213
214 Iterator begin() const { return Iterator(*this); }
215
216 Iterator end() const { return Iterator(); }
217
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 */
225 U get() const { return mFlags; }
226
227 std::string string() const {
228 std::string result;
229 bool first = true;
230 U unstringified = 0;
231 for (const F f : *this) {
232 std::optional<std::string_view> flagString = flag_name(f);
233 if (flagString) {
234 appendFlag(result, flagString.value(), first);
235 } else {
236 unstringified |= static_cast<U>(f);
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:
252 U mFlags;
253
254 static void appendFlag(std::string& str, const std::string_view& flag, bool& first) {
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
268 template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>>
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 }
273 template <typename F, typename = std::enable_if_t<details::is_enum_class_v<F>>>
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
281