1 // Copyright 2014 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_UTIL_FLAGS_H_ 6 #define V8_UTIL_FLAGS_H_ 7 8 // Origin: 9 // https://github.com/v8/v8/blob/1bafcc6b999b23ea1d394f5d267a08183e3c4e19/src/base/flags.h#L15-L90 10 11 namespace v8 { 12 namespace base { 13 14 // The Flags class provides a type-safe way of storing OR-combinations of enum 15 // values. The Flags<T, S> class is a template class, where T is an enum type, 16 // and S is the underlying storage type (usually int). 17 // 18 // The traditional C++ approach for storing OR-combinations of enum values is to 19 // use an int or unsigned int variable. The inconvenience with this approach is 20 // that there's no type checking at all; any enum value can be OR'd with any 21 // other enum value and passed on to a function that takes an int or unsigned 22 // int. 23 template <typename T, typename S = int> 24 class Flags final { 25 public: 26 using flag_type = T; 27 using mask_type = S; 28 Flags()29 constexpr Flags() : mask_(0) {} Flags(flag_type flag)30 constexpr Flags(flag_type flag) : mask_(static_cast<S>(flag)) {} Flags(mask_type mask)31 constexpr explicit Flags(mask_type mask) : mask_(static_cast<S>(mask)) {} 32 33 constexpr bool operator==(flag_type flag) const { 34 return mask_ == static_cast<S>(flag); 35 } 36 constexpr bool operator!=(flag_type flag) const { 37 return mask_ != static_cast<S>(flag); 38 } 39 40 Flags& operator&=(const Flags& flags) { 41 mask_ &= flags.mask_; 42 return *this; 43 } 44 Flags& operator|=(const Flags& flags) { 45 mask_ |= flags.mask_; 46 return *this; 47 } 48 Flags& operator^=(const Flags& flags) { 49 mask_ ^= flags.mask_; 50 return *this; 51 } 52 53 constexpr Flags operator&(const Flags& flags) const { 54 return Flags(mask_ & flags.mask_); 55 } 56 constexpr Flags operator|(const Flags& flags) const { 57 return Flags(mask_ | flags.mask_); 58 } 59 constexpr Flags operator^(const Flags& flags) const { 60 return Flags(mask_ ^ flags.mask_); 61 } 62 63 Flags& operator&=(flag_type flag) { return operator&=(Flags(flag)); } 64 Flags& operator|=(flag_type flag) { return operator|=(Flags(flag)); } 65 Flags& operator^=(flag_type flag) { return operator^=(Flags(flag)); } 66 67 constexpr Flags operator&(flag_type flag) const { 68 return operator&(Flags(flag)); 69 } 70 constexpr Flags operator|(flag_type flag) const { 71 return operator|(Flags(flag)); 72 } 73 constexpr Flags operator^(flag_type flag) const { 74 return operator^(Flags(flag)); 75 } 76 77 constexpr Flags operator~() const { return Flags(~mask_); } 78 mask_type()79 constexpr operator mask_type() const { return mask_; } 80 constexpr bool operator!() const { return !mask_; } 81 without(flag_type flag)82 Flags without(flag_type flag) { return *this & (~Flags(flag)); } 83 hash_value(const Flags & flags)84 friend size_t hash_value(const Flags& flags) { return flags.mask_; } 85 86 private: 87 mask_type mask_; 88 }; 89 90 } // namespace base 91 } // namespace v8 92 93 #endif // V8_UTIL_FLAG_H_ 94