1 // Copyright 2019 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_BASE_ENUM_SET_H_ 6 #define V8_BASE_ENUM_SET_H_ 7 8 #include <type_traits> 9 10 #include "src/base/logging.h" 11 12 namespace v8 { 13 namespace base { 14 15 // A poor man's version of STL's bitset: A bit set of enums E (without explicit 16 // values), fitting into an integral type T. 17 template <class E, class T = int> 18 class EnumSet { 19 static_assert(std::is_enum<E>::value, "EnumSet can only be used with enums"); 20 21 public: 22 constexpr EnumSet() = default; 23 EnumSet(std::initializer_list<E> init)24 explicit constexpr EnumSet(std::initializer_list<E> init) { 25 T bits = 0; 26 for (E e : init) bits |= Mask(e); 27 bits_ = bits; 28 } 29 empty()30 bool empty() const { return bits_ == 0; } contains(E element)31 bool contains(E element) const { return (bits_ & Mask(element)) != 0; } contains_any(EnumSet set)32 bool contains_any(EnumSet set) const { return (bits_ & set.bits_) != 0; } Add(E element)33 void Add(E element) { bits_ |= Mask(element); } Add(EnumSet set)34 void Add(EnumSet set) { bits_ |= set.bits_; } Remove(E element)35 void Remove(E element) { bits_ &= ~Mask(element); } Remove(EnumSet set)36 void Remove(EnumSet set) { bits_ &= ~set.bits_; } RemoveAll()37 void RemoveAll() { bits_ = 0; } Intersect(EnumSet set)38 void Intersect(EnumSet set) { bits_ &= set.bits_; } ToIntegral()39 T ToIntegral() const { return bits_; } 40 41 bool operator==(EnumSet set) const { return bits_ == set.bits_; } 42 bool operator!=(EnumSet set) const { return bits_ != set.bits_; } 43 44 EnumSet operator|(EnumSet set) const { return EnumSet(bits_ | set.bits_); } 45 EnumSet operator&(EnumSet set) const { return EnumSet(bits_ & set.bits_); } 46 EnumSet operator-(EnumSet set) const { return EnumSet(bits_ & ~set.bits_); } 47 48 EnumSet& operator|=(EnumSet set) { return *this = *this | set; } 49 EnumSet& operator&=(EnumSet set) { return *this = *this & set; } 50 EnumSet& operator-=(EnumSet set) { return *this = *this - set; } 51 52 EnumSet operator|(E element) const { return EnumSet(bits_ | Mask(element)); } 53 EnumSet operator&(E element) const { return EnumSet(bits_ & Mask(element)); } 54 EnumSet operator-(E element) const { return EnumSet(bits_ & ~Mask(element)); } 55 56 EnumSet& operator|=(E element) { return *this = *this | element; } 57 EnumSet& operator&=(E element) { return *this = *this & element; } 58 EnumSet& operator-=(E element) { return *this = *this - element; } 59 FromIntegral(T bits)60 static constexpr EnumSet FromIntegral(T bits) { return EnumSet{bits}; } 61 62 private: EnumSet(T bits)63 explicit constexpr EnumSet(T bits) : bits_(bits) {} 64 Mask(E element)65 static constexpr T Mask(E element) { 66 #if V8_HAS_CXX14_CONSTEXPR 67 DCHECK_GT(sizeof(T) * 8, static_cast<int>(element)); 68 #endif 69 return T{1} << static_cast<typename std::underlying_type<E>::type>(element); 70 } 71 72 T bits_ = 0; 73 }; 74 75 } // namespace base 76 } // namespace v8 77 78 #endif // V8_BASE_ENUM_SET_H_ 79