blob: dbe3148fc57c1cfb753187b7f15089dc6a87c299 [file] [log] [blame]
Michael Wright44753b12020-07-08 13:48:11 +01001/*
Dominik Laskowski75788452021-02-09 18:51:25 -08002 * Copyright 2020 The Android Open Source Project
Michael Wright44753b12020-07-08 13:48:11 +01003 *
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
Dominik Laskowski75788452021-02-09 18:51:25 -080017#pragma once
Michael Wright44753b12020-07-08 13:48:11 +010018
Dominik Laskowski75788452021-02-09 18:51:25 -080019#include <ftl/enum.h>
20#include <ftl/string.h>
21
Dominik Laskowskia7fe7eb2022-02-16 10:44:28 -080022#include <bitset>
Michael Wright44753b12020-07-08 13:48:11 +010023#include <cstdint>
Dominik Laskowski75788452021-02-09 18:51:25 -080024#include <iterator>
Michael Wright44753b12020-07-08 13:48:11 +010025#include <string>
26#include <type_traits>
27
Dominik Laskowski2f01d772022-03-23 16:01:29 -070028// TODO(b/185536303): Align with FTL style.
Michael Wright44753b12020-07-08 13:48:11 +010029
Dominik Laskowski2f01d772022-03-23 16:01:29 -070030namespace android::ftl {
Michael Wright44753b12020-07-08 13:48:11 +010031
Michael Wright44753b12020-07-08 13:48:11 +010032/* A class for handling flags defined by an enum or enum class in a type-safe way. */
Michael Wright8759d672020-07-21 00:46:45 +010033template <typename F>
Michael Wright44753b12020-07-08 13:48:11 +010034class Flags {
35 // F must be an enum or its underlying type is undefined. Theoretically we could specialize this
36 // further to avoid this restriction but in general we want to encourage the use of enums
37 // anyways.
Michael Wright8759d672020-07-21 00:46:45 +010038 static_assert(std::is_enum_v<F>, "Flags type must be an enum");
Dominik Laskowski75788452021-02-09 18:51:25 -080039 using U = std::underlying_type_t<F>;
Michael Wright44753b12020-07-08 13:48:11 +010040
41public:
Michael Wright75d9e662020-07-11 23:54:40 +010042 constexpr Flags(F f) : mFlags(static_cast<U>(f)) {}
43 constexpr Flags() : mFlags(0) {}
44 constexpr Flags(const Flags<F>& f) : mFlags(f.mFlags) {}
Michael Wright44753b12020-07-08 13:48:11 +010045
46 // Provide a non-explicit construct for non-enum classes since they easily convert to their
47 // underlying types (e.g. when used with bitwise operators). For enum classes, however, we
48 // should force them to be explicitly constructed from their underlying types to make full use
49 // of the type checker.
50 template <typename T = U>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070051 constexpr Flags(T t, std::enable_if_t<!is_scoped_enum_v<F>, T>* = nullptr) : mFlags(t) {}
Dominik Laskowski75788452021-02-09 18:51:25 -080052
Michael Wright44753b12020-07-08 13:48:11 +010053 template <typename T = U>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070054 explicit constexpr Flags(T t, std::enable_if_t<is_scoped_enum_v<F>, T>* = nullptr)
Michael Wright75d9e662020-07-11 23:54:40 +010055 : mFlags(t) {}
56
57 class Iterator {
Dominik Laskowskia7fe7eb2022-02-16 10:44:28 -080058 using Bits = std::uint64_t;
59 static_assert(sizeof(U) <= sizeof(Bits));
Michael Wright75d9e662020-07-11 23:54:40 +010060
61 public:
Dominik Laskowskia7fe7eb2022-02-16 10:44:28 -080062 constexpr Iterator() = default;
Michael Wright75d9e662020-07-11 23:54:40 +010063 Iterator(Flags<F> flags) : mRemainingFlags(flags.mFlags) { (*this)++; }
Michael Wright75d9e662020-07-11 23:54:40 +010064
65 // Pre-fix ++
66 Iterator& operator++() {
Dominik Laskowskia7fe7eb2022-02-16 10:44:28 -080067 if (mRemainingFlags.none()) {
68 mCurrFlag = 0;
Michael Wright75d9e662020-07-11 23:54:40 +010069 } else {
Dominik Laskowskia7fe7eb2022-02-16 10:44:28 -080070 // TODO: Replace with std::countr_zero in C++20.
71 const Bits bit = static_cast<Bits>(__builtin_ctzll(mRemainingFlags.to_ullong()));
72 mRemainingFlags.reset(static_cast<std::size_t>(bit));
73 mCurrFlag = static_cast<U>(static_cast<Bits>(1) << bit);
Michael Wright75d9e662020-07-11 23:54:40 +010074 }
75 return *this;
76 }
77
78 // Post-fix ++
79 Iterator operator++(int) {
80 Iterator iter = *this;
81 ++*this;
82 return iter;
83 }
84
85 bool operator==(Iterator other) const {
86 return mCurrFlag == other.mCurrFlag && mRemainingFlags == other.mRemainingFlags;
87 }
88
89 bool operator!=(Iterator other) const { return !(*this == other); }
90
Dominik Laskowskia7fe7eb2022-02-16 10:44:28 -080091 F operator*() const { return F{mCurrFlag}; }
Michael Wright75d9e662020-07-11 23:54:40 +010092
93 // iterator traits
94
95 // In the future we could make this a bidirectional const iterator instead of a forward
96 // iterator but it doesn't seem worth the added complexity at this point. This could not,
97 // however, be made a non-const iterator as assigning one flag to another is a non-sensical
98 // operation.
99 using iterator_category = std::input_iterator_tag;
100 using value_type = F;
101 // Per the C++ spec, because input iterators are not assignable the iterator's reference
102 // type does not actually need to be a reference. In fact, making it a reference would imply
103 // that modifying it would change the underlying Flags object, which is obviously wrong for
104 // the same reason this can't be a non-const iterator.
105 using reference = F;
106 using difference_type = void;
107 using pointer = void;
108
109 private:
Dominik Laskowskia7fe7eb2022-02-16 10:44:28 -0800110 std::bitset<sizeof(Bits) * 8> mRemainingFlags;
111 U mCurrFlag = 0;
Michael Wright75d9e662020-07-11 23:54:40 +0100112 };
113
Michael Wright44753b12020-07-08 13:48:11 +0100114 /*
115 * Tests whether the given flag is set.
116 */
117 bool test(F flag) const {
118 U f = static_cast<U>(flag);
Michael Wright75d9e662020-07-11 23:54:40 +0100119 return (f & mFlags) == f;
Michael Wright44753b12020-07-08 13:48:11 +0100120 }
121
122 /* Tests whether any of the given flags are set */
Prabir Pradhan12f89132023-04-18 20:47:03 +0000123 bool any(Flags<F> f = ~Flags<F>()) const { return (mFlags & f.mFlags) != 0; }
Michael Wright44753b12020-07-08 13:48:11 +0100124
125 /* Tests whether all of the given flags are set */
Prabir Pradhanf0a3d812022-02-07 02:54:39 -0800126 bool all(Flags<F> f) const { return (mFlags & f.mFlags) == f.mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100127
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800128 constexpr Flags<F> operator|(Flags<F> rhs) const { return static_cast<F>(mFlags | rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100129 Flags<F>& operator|=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100130 mFlags = mFlags | rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100131 return *this;
132 }
133
Michael Wright75d9e662020-07-11 23:54:40 +0100134 Flags<F> operator&(Flags<F> rhs) const { return static_cast<F>(mFlags & rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100135 Flags<F>& operator&=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100136 mFlags = mFlags & rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100137 return *this;
138 }
139
Michael Wright75d9e662020-07-11 23:54:40 +0100140 Flags<F> operator^(Flags<F> rhs) const { return static_cast<F>(mFlags ^ rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100141 Flags<F>& operator^=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100142 mFlags = mFlags ^ rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100143 return *this;
144 }
145
Michael Wright75d9e662020-07-11 23:54:40 +0100146 Flags<F> operator~() { return static_cast<F>(~mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100147
Michael Wright75d9e662020-07-11 23:54:40 +0100148 bool operator==(Flags<F> rhs) const { return mFlags == rhs.mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100149 bool operator!=(Flags<F> rhs) const { return !operator==(rhs); }
150
151 Flags<F>& operator=(const Flags<F>& rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100152 mFlags = rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100153 return *this;
154 }
155
Prabir Pradhanf0a3d812022-02-07 02:54:39 -0800156 inline Flags<F>& clear(Flags<F> f = static_cast<F>(~static_cast<U>(0))) {
157 return *this &= ~f;
158 }
159
Michael Wright75d9e662020-07-11 23:54:40 +0100160 Iterator begin() const { return Iterator(*this); }
161
162 Iterator end() const { return Iterator(); }
163
Michael Wright44753b12020-07-08 13:48:11 +0100164 /*
165 * Returns the stored set of flags.
166 *
167 * Note that this returns the underlying type rather than the base enum class. This is because
168 * the value is no longer necessarily a strict member of the enum since the returned value could
169 * be multiple enum variants OR'd together.
170 */
Michael Wright75d9e662020-07-11 23:54:40 +0100171 U get() const { return mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100172
Michael Wright8759d672020-07-21 00:46:45 +0100173 std::string string() const {
Michael Wright44753b12020-07-08 13:48:11 +0100174 std::string result;
175 bool first = true;
176 U unstringified = 0;
Michael Wright75d9e662020-07-11 23:54:40 +0100177 for (const F f : *this) {
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700178 if (const auto flagName = flag_name(f)) {
Dominik Laskowski75788452021-02-09 18:51:25 -0800179 appendFlag(result, flagName.value(), first);
Michael Wright44753b12020-07-08 13:48:11 +0100180 } else {
Michael Wright75d9e662020-07-11 23:54:40 +0100181 unstringified |= static_cast<U>(f);
Michael Wright44753b12020-07-08 13:48:11 +0100182 }
183 }
184
185 if (unstringified != 0) {
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700186 constexpr auto radix = sizeof(U) == 1 ? Radix::kBin : Radix::kHex;
187 appendFlag(result, to_string(unstringified, radix), first);
Michael Wright44753b12020-07-08 13:48:11 +0100188 }
189
190 if (first) {
191 result += "0x0";
192 }
193
194 return result;
195 }
196
197private:
Michael Wright75d9e662020-07-11 23:54:40 +0100198 U mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100199
Michael Wright8759d672020-07-21 00:46:45 +0100200 static void appendFlag(std::string& str, const std::string_view& flag, bool& first) {
Michael Wright44753b12020-07-08 13:48:11 +0100201 if (first) {
202 first = false;
203 } else {
204 str += " | ";
205 }
206 str += flag;
207 }
208};
209
210// This namespace provides operator overloads for enum classes to make it easier to work with them
211// as flags. In order to use these, add them via a `using namespace` declaration.
212namespace flag_operators {
213
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700214template <typename F, typename = std::enable_if_t<is_scoped_enum_v<F>>>
Michael Wright44753b12020-07-08 13:48:11 +0100215inline Flags<F> operator~(F f) {
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700216 return static_cast<F>(~to_underlying(f));
Michael Wright44753b12020-07-08 13:48:11 +0100217}
Dominik Laskowski75788452021-02-09 18:51:25 -0800218
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700219template <typename F, typename = std::enable_if_t<is_scoped_enum_v<F>>>
Siarhei Vishniakou253f4642022-11-09 13:42:06 -0800220constexpr Flags<F> operator|(F lhs, F rhs) {
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700221 return static_cast<F>(to_underlying(lhs) | to_underlying(rhs));
Michael Wright44753b12020-07-08 13:48:11 +0100222}
223
224} // namespace flag_operators
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700225} // namespace android::ftl