blob: 932af2d9f9c397927b151149005ed06b0f3ab555 [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
Michael Wright44753b12020-07-08 13:48:11 +010022#include <cstdint>
Dominik Laskowski75788452021-02-09 18:51:25 -080023#include <iterator>
Michael Wright44753b12020-07-08 13:48:11 +010024#include <string>
25#include <type_traits>
26
27#include "utils/BitSet.h"
28
Dominik Laskowski75788452021-02-09 18:51:25 -080029// TODO(b/185536303): Align with FTL style and namespace.
Michael Wright44753b12020-07-08 13:48:11 +010030
31namespace android {
32
Michael Wright44753b12020-07-08 13:48:11 +010033/* A class for handling flags defined by an enum or enum class in a type-safe way. */
Michael Wright8759d672020-07-21 00:46:45 +010034template <typename F>
Michael Wright44753b12020-07-08 13:48:11 +010035class Flags {
36 // F must be an enum or its underlying type is undefined. Theoretically we could specialize this
37 // further to avoid this restriction but in general we want to encourage the use of enums
38 // anyways.
Michael Wright8759d672020-07-21 00:46:45 +010039 static_assert(std::is_enum_v<F>, "Flags type must be an enum");
Dominik Laskowski75788452021-02-09 18:51:25 -080040 using U = std::underlying_type_t<F>;
Michael Wright44753b12020-07-08 13:48:11 +010041
42public:
Michael Wright75d9e662020-07-11 23:54:40 +010043 constexpr Flags(F f) : mFlags(static_cast<U>(f)) {}
44 constexpr Flags() : mFlags(0) {}
45 constexpr Flags(const Flags<F>& f) : mFlags(f.mFlags) {}
Michael Wright44753b12020-07-08 13:48:11 +010046
47 // Provide a non-explicit construct for non-enum classes since they easily convert to their
48 // underlying types (e.g. when used with bitwise operators). For enum classes, however, we
49 // should force them to be explicitly constructed from their underlying types to make full use
50 // of the type checker.
51 template <typename T = U>
Dominik Laskowski75788452021-02-09 18:51:25 -080052 constexpr Flags(T t, std::enable_if_t<!ftl::is_scoped_enum_v<F>, T>* = nullptr) : mFlags(t) {}
53
Michael Wright44753b12020-07-08 13:48:11 +010054 template <typename T = U>
Dominik Laskowski75788452021-02-09 18:51:25 -080055 explicit constexpr Flags(T t, std::enable_if_t<ftl::is_scoped_enum_v<F>, T>* = nullptr)
Michael Wright75d9e662020-07-11 23:54:40 +010056 : mFlags(t) {}
57
58 class Iterator {
59 // The type can't be larger than 64-bits otherwise it won't fit in BitSet64.
60 static_assert(sizeof(U) <= sizeof(uint64_t));
61
62 public:
63 Iterator(Flags<F> flags) : mRemainingFlags(flags.mFlags) { (*this)++; }
64 Iterator() : mRemainingFlags(0), mCurrFlag(static_cast<F>(0)) {}
65
66 // Pre-fix ++
67 Iterator& operator++() {
68 if (mRemainingFlags.isEmpty()) {
69 mCurrFlag = static_cast<F>(0);
70 } else {
71 uint64_t bit = mRemainingFlags.clearLastMarkedBit(); // counts from left
72 const U flag = 1 << (64 - bit - 1);
73 mCurrFlag = static_cast<F>(flag);
74 }
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
91 F operator*() { return mCurrFlag; }
92
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:
110 BitSet64 mRemainingFlags;
111 F mCurrFlag;
112 };
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 */
Michael Wright75d9e662020-07-11 23:54:40 +0100123 bool any(Flags<F> f) { return (mFlags & f.mFlags) != 0; }
Michael Wright44753b12020-07-08 13:48:11 +0100124
125 /* Tests whether all of the given flags are set */
Michael Wright75d9e662020-07-11 23:54:40 +0100126 bool all(Flags<F> f) { return (mFlags & f.mFlags) == f.mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100127
Michael Wright75d9e662020-07-11 23:54:40 +0100128 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
Michael Wright75d9e662020-07-11 23:54:40 +0100156 Iterator begin() const { return Iterator(*this); }
157
158 Iterator end() const { return Iterator(); }
159
Michael Wright44753b12020-07-08 13:48:11 +0100160 /*
161 * Returns the stored set of flags.
162 *
163 * Note that this returns the underlying type rather than the base enum class. This is because
164 * the value is no longer necessarily a strict member of the enum since the returned value could
165 * be multiple enum variants OR'd together.
166 */
Michael Wright75d9e662020-07-11 23:54:40 +0100167 U get() const { return mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100168
Michael Wright8759d672020-07-21 00:46:45 +0100169 std::string string() const {
Michael Wright44753b12020-07-08 13:48:11 +0100170 std::string result;
171 bool first = true;
172 U unstringified = 0;
Michael Wright75d9e662020-07-11 23:54:40 +0100173 for (const F f : *this) {
Dominik Laskowski75788452021-02-09 18:51:25 -0800174 if (const auto flagName = ftl::flag_name(f)) {
175 appendFlag(result, flagName.value(), first);
Michael Wright44753b12020-07-08 13:48:11 +0100176 } else {
Michael Wright75d9e662020-07-11 23:54:40 +0100177 unstringified |= static_cast<U>(f);
Michael Wright44753b12020-07-08 13:48:11 +0100178 }
179 }
180
181 if (unstringified != 0) {
Dominik Laskowski75788452021-02-09 18:51:25 -0800182 constexpr auto radix = sizeof(U) == 1 ? ftl::Radix::kBin : ftl::Radix::kHex;
183 appendFlag(result, ftl::to_string(unstringified, radix), first);
Michael Wright44753b12020-07-08 13:48:11 +0100184 }
185
186 if (first) {
187 result += "0x0";
188 }
189
190 return result;
191 }
192
193private:
Michael Wright75d9e662020-07-11 23:54:40 +0100194 U mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100195
Michael Wright8759d672020-07-21 00:46:45 +0100196 static void appendFlag(std::string& str, const std::string_view& flag, bool& first) {
Michael Wright44753b12020-07-08 13:48:11 +0100197 if (first) {
198 first = false;
199 } else {
200 str += " | ";
201 }
202 str += flag;
203 }
204};
205
206// This namespace provides operator overloads for enum classes to make it easier to work with them
207// as flags. In order to use these, add them via a `using namespace` declaration.
208namespace flag_operators {
209
Dominik Laskowski75788452021-02-09 18:51:25 -0800210template <typename F, typename = std::enable_if_t<ftl::is_scoped_enum_v<F>>>
Michael Wright44753b12020-07-08 13:48:11 +0100211inline Flags<F> operator~(F f) {
Dominik Laskowski04667b72021-12-15 13:14:54 -0800212 return static_cast<F>(~ftl::to_underlying(f));
Michael Wright44753b12020-07-08 13:48:11 +0100213}
Dominik Laskowski75788452021-02-09 18:51:25 -0800214
215template <typename F, typename = std::enable_if_t<ftl::is_scoped_enum_v<F>>>
Michael Wright44753b12020-07-08 13:48:11 +0100216Flags<F> operator|(F lhs, F rhs) {
Dominik Laskowski04667b72021-12-15 13:14:54 -0800217 return static_cast<F>(ftl::to_underlying(lhs) | ftl::to_underlying(rhs));
Michael Wright44753b12020-07-08 13:48:11 +0100218}
219
220} // namespace flag_operators
221} // namespace android