blob: 0f52e18719829037659f5b3b4c1f6919c3dc4aef [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
19#include <cstdint>
20#include <optional>
21#include <string>
22#include <type_traits>
23
24#include "utils/BitSet.h"
25
26#ifndef __UI_INPUT_FLAGS_H
27#define __UI_INPUT_FLAGS_H
28
29namespace android {
30
31// A trait for determining whether a type is specifically an enum class or not.
32template <typename T, bool = std::is_enum_v<T>>
33struct is_enum_class : std::false_type {};
34
35// By definition, an enum class is an enum that is not implicitly convertible to its underlying
36// type.
37template <typename T>
38struct is_enum_class<T, true>
39 : std::bool_constant<!std::is_convertible_v<T, std::underlying_type_t<T>>> {};
40
41template <typename T>
42inline constexpr bool is_enum_class_v = is_enum_class<T>::value;
43
44/* A class for handling flags defined by an enum or enum class in a type-safe way. */
45template <class F, typename = std::enable_if_t<std::is_enum_v<F>>>
46class Flags {
47 // F must be an enum or its underlying type is undefined. Theoretically we could specialize this
48 // further to avoid this restriction but in general we want to encourage the use of enums
49 // anyways.
50 using U = typename std::underlying_type_t<F>;
51
52public:
Michael Wright75d9e662020-07-11 23:54:40 +010053 constexpr Flags(F f) : mFlags(static_cast<U>(f)) {}
54 constexpr Flags() : mFlags(0) {}
55 constexpr Flags(const Flags<F>& f) : mFlags(f.mFlags) {}
Michael Wright44753b12020-07-08 13:48:11 +010056
57 // Provide a non-explicit construct for non-enum classes since they easily convert to their
58 // underlying types (e.g. when used with bitwise operators). For enum classes, however, we
59 // should force them to be explicitly constructed from their underlying types to make full use
60 // of the type checker.
61 template <typename T = U>
Michael Wright75d9e662020-07-11 23:54:40 +010062 constexpr Flags(T t, typename std::enable_if_t<!is_enum_class_v<F>, T>* = nullptr)
63 : mFlags(t) {}
Michael Wright44753b12020-07-08 13:48:11 +010064 template <typename T = U>
65 explicit constexpr Flags(T t, typename std::enable_if_t<is_enum_class_v<F>, T>* = nullptr)
Michael Wright75d9e662020-07-11 23:54:40 +010066 : mFlags(t) {}
67
68 class Iterator {
69 // The type can't be larger than 64-bits otherwise it won't fit in BitSet64.
70 static_assert(sizeof(U) <= sizeof(uint64_t));
71
72 public:
73 Iterator(Flags<F> flags) : mRemainingFlags(flags.mFlags) { (*this)++; }
74 Iterator() : mRemainingFlags(0), mCurrFlag(static_cast<F>(0)) {}
75
76 // Pre-fix ++
77 Iterator& operator++() {
78 if (mRemainingFlags.isEmpty()) {
79 mCurrFlag = static_cast<F>(0);
80 } else {
81 uint64_t bit = mRemainingFlags.clearLastMarkedBit(); // counts from left
82 const U flag = 1 << (64 - bit - 1);
83 mCurrFlag = static_cast<F>(flag);
84 }
85 return *this;
86 }
87
88 // Post-fix ++
89 Iterator operator++(int) {
90 Iterator iter = *this;
91 ++*this;
92 return iter;
93 }
94
95 bool operator==(Iterator other) const {
96 return mCurrFlag == other.mCurrFlag && mRemainingFlags == other.mRemainingFlags;
97 }
98
99 bool operator!=(Iterator other) const { return !(*this == other); }
100
101 F operator*() { return mCurrFlag; }
102
103 // iterator traits
104
105 // In the future we could make this a bidirectional const iterator instead of a forward
106 // iterator but it doesn't seem worth the added complexity at this point. This could not,
107 // however, be made a non-const iterator as assigning one flag to another is a non-sensical
108 // operation.
109 using iterator_category = std::input_iterator_tag;
110 using value_type = F;
111 // Per the C++ spec, because input iterators are not assignable the iterator's reference
112 // type does not actually need to be a reference. In fact, making it a reference would imply
113 // that modifying it would change the underlying Flags object, which is obviously wrong for
114 // the same reason this can't be a non-const iterator.
115 using reference = F;
116 using difference_type = void;
117 using pointer = void;
118
119 private:
120 BitSet64 mRemainingFlags;
121 F mCurrFlag;
122 };
123
Michael Wright44753b12020-07-08 13:48:11 +0100124 /*
125 * Tests whether the given flag is set.
126 */
127 bool test(F flag) const {
128 U f = static_cast<U>(flag);
Michael Wright75d9e662020-07-11 23:54:40 +0100129 return (f & mFlags) == f;
Michael Wright44753b12020-07-08 13:48:11 +0100130 }
131
132 /* Tests whether any of the given flags are set */
Michael Wright75d9e662020-07-11 23:54:40 +0100133 bool any(Flags<F> f) { return (mFlags & f.mFlags) != 0; }
Michael Wright44753b12020-07-08 13:48:11 +0100134
135 /* Tests whether all of the given flags are set */
Michael Wright75d9e662020-07-11 23:54:40 +0100136 bool all(Flags<F> f) { return (mFlags & f.mFlags) == f.mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100137
Michael Wright75d9e662020-07-11 23:54:40 +0100138 Flags<F> operator|(Flags<F> rhs) const { return static_cast<F>(mFlags | rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100139 Flags<F>& operator|=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100140 mFlags = mFlags | rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100141 return *this;
142 }
143
Michael Wright75d9e662020-07-11 23:54:40 +0100144 Flags<F> operator&(Flags<F> rhs) const { return static_cast<F>(mFlags & rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100145 Flags<F>& operator&=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100146 mFlags = mFlags & rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100147 return *this;
148 }
149
Michael Wright75d9e662020-07-11 23:54:40 +0100150 Flags<F> operator^(Flags<F> rhs) const { return static_cast<F>(mFlags ^ rhs.mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100151 Flags<F>& operator^=(Flags<F> rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100152 mFlags = mFlags ^ rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100153 return *this;
154 }
155
Michael Wright75d9e662020-07-11 23:54:40 +0100156 Flags<F> operator~() { return static_cast<F>(~mFlags); }
Michael Wright44753b12020-07-08 13:48:11 +0100157
Michael Wright75d9e662020-07-11 23:54:40 +0100158 bool operator==(Flags<F> rhs) const { return mFlags == rhs.mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100159 bool operator!=(Flags<F> rhs) const { return !operator==(rhs); }
160
161 Flags<F>& operator=(const Flags<F>& rhs) {
Michael Wright75d9e662020-07-11 23:54:40 +0100162 mFlags = rhs.mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100163 return *this;
164 }
165
Michael Wright75d9e662020-07-11 23:54:40 +0100166 Iterator begin() const { return Iterator(*this); }
167
168 Iterator end() const { return Iterator(); }
169
Michael Wright44753b12020-07-08 13:48:11 +0100170 /*
171 * Returns the stored set of flags.
172 *
173 * Note that this returns the underlying type rather than the base enum class. This is because
174 * the value is no longer necessarily a strict member of the enum since the returned value could
175 * be multiple enum variants OR'd together.
176 */
Michael Wright75d9e662020-07-11 23:54:40 +0100177 U get() const { return mFlags; }
Michael Wright44753b12020-07-08 13:48:11 +0100178
179 std::string string() const { return string(defaultStringify); }
180
181 std::string string(std::function<std::optional<std::string>(F)> stringify) const {
Michael Wright44753b12020-07-08 13:48:11 +0100182 std::string result;
183 bool first = true;
184 U unstringified = 0;
Michael Wright75d9e662020-07-11 23:54:40 +0100185 for (const F f : *this) {
186 std::optional<std::string> flagString = stringify(f);
Michael Wright44753b12020-07-08 13:48:11 +0100187 if (flagString) {
188 appendFlag(result, flagString.value(), first);
189 } else {
Michael Wright75d9e662020-07-11 23:54:40 +0100190 unstringified |= static_cast<U>(f);
Michael Wright44753b12020-07-08 13:48:11 +0100191 }
192 }
193
194 if (unstringified != 0) {
195 appendFlag(result, base::StringPrintf("0x%08x", unstringified), first);
196 }
197
198 if (first) {
199 result += "0x0";
200 }
201
202 return result;
203 }
204
205private:
Michael Wright75d9e662020-07-11 23:54:40 +0100206 U mFlags;
Michael Wright44753b12020-07-08 13:48:11 +0100207
208 static std::optional<std::string> defaultStringify(F) { return std::nullopt; }
209 static void appendFlag(std::string& str, const std::string& flag, bool& first) {
210 if (first) {
211 first = false;
212 } else {
213 str += " | ";
214 }
215 str += flag;
216 }
217};
218
219// This namespace provides operator overloads for enum classes to make it easier to work with them
220// as flags. In order to use these, add them via a `using namespace` declaration.
221namespace flag_operators {
222
223template <typename F, typename = std::enable_if_t<is_enum_class_v<F>>>
224inline Flags<F> operator~(F f) {
225 using U = typename std::underlying_type_t<F>;
226 return static_cast<F>(~static_cast<U>(f));
227}
228template <typename F, typename = std::enable_if_t<is_enum_class_v<F>>>
229Flags<F> operator|(F lhs, F rhs) {
230 using U = typename std::underlying_type_t<F>;
231 return static_cast<F>(static_cast<U>(lhs) | static_cast<U>(rhs));
232}
233
234} // namespace flag_operators
235} // namespace android
236
237#endif // __UI_INPUT_FLAGS_H