| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef V8_BASE_FLAGS_H_ | 5 #ifndef V8_BASE_FLAGS_H_ |
| 6 #define V8_BASE_FLAGS_H_ | 6 #define V8_BASE_FLAGS_H_ |
| 7 | 7 |
| 8 #include <cstddef> |
| 9 |
| 8 #include "src/base/compiler-specific.h" | 10 #include "src/base/compiler-specific.h" |
| 9 | 11 |
| 10 namespace v8 { | 12 namespace v8 { |
| 11 namespace base { | 13 namespace base { |
| 12 | 14 |
| 13 // The Flags class provides a type-safe way of storing OR-combinations of enum | 15 // The Flags class provides a type-safe way of storing OR-combinations of enum |
| 14 // values. The Flags<T, S> class is a template class, where T is an enum type, | 16 // values. The Flags<T, S> class is a template class, where T is an enum type, |
| 15 // and S is the underlying storage type (usually int). | 17 // and S is the underlying storage type (usually int). |
| 16 // | 18 // |
| 17 // The traditional C++ approach for storing OR-combinations of enum values is to | 19 // The traditional C++ approach for storing OR-combinations of enum values is to |
| 18 // use an int or unsigned int variable. The inconvenience with this approach is | 20 // use an int or unsigned int variable. The inconvenience with this approach is |
| 19 // that there's no type checking at all; any enum value can be OR'd with any | 21 // that there's no type checking at all; any enum value can be OR'd with any |
| 20 // other enum value and passed on to a function that takes an int or unsigned | 22 // other enum value and passed on to a function that takes an int or unsigned |
| 21 // int. | 23 // int. |
| 22 template <typename T, typename S = int> | 24 template <typename T, typename S = int> |
| 23 class Flags final { | 25 class Flags final { |
| 24 public: | 26 public: |
| 25 typedef T flag_type; | 27 typedef T flag_type; |
| 26 typedef S mask_type; | 28 typedef S mask_type; |
| 27 | 29 |
| 28 Flags() : mask_(0) {} | 30 Flags() : mask_(0) {} |
| 29 Flags(flag_type flag) // NOLINT(runtime/explicit) | 31 Flags(flag_type flag) // NOLINT(runtime/explicit) |
| 30 : mask_(static_cast<S>(flag)) {} | 32 : mask_(static_cast<S>(flag)) {} |
| 31 explicit Flags(mask_type mask) : mask_(static_cast<S>(mask)) {} | 33 explicit Flags(mask_type mask) : mask_(static_cast<S>(mask)) {} |
| 32 | 34 |
| 35 bool operator==(flag_type flag) const { |
| 36 return mask_ == static_cast<S>(flag); |
| 37 } |
| 38 bool operator!=(flag_type flag) const { |
| 39 return mask_ != static_cast<S>(flag); |
| 40 } |
| 41 |
| 33 Flags& operator&=(const Flags& flags) { | 42 Flags& operator&=(const Flags& flags) { |
| 34 mask_ &= flags.mask_; | 43 mask_ &= flags.mask_; |
| 35 return *this; | 44 return *this; |
| 36 } | 45 } |
| 37 Flags& operator|=(const Flags& flags) { | 46 Flags& operator|=(const Flags& flags) { |
| 38 mask_ |= flags.mask_; | 47 mask_ |= flags.mask_; |
| 39 return *this; | 48 return *this; |
| 40 } | 49 } |
| 41 Flags& operator^=(const Flags& flags) { | 50 Flags& operator^=(const Flags& flags) { |
| 42 mask_ ^= flags.mask_; | 51 mask_ ^= flags.mask_; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 53 | 62 |
| 54 Flags operator&(flag_type flag) const { return operator&(Flags(flag)); } | 63 Flags operator&(flag_type flag) const { return operator&(Flags(flag)); } |
| 55 Flags operator|(flag_type flag) const { return operator|(Flags(flag)); } | 64 Flags operator|(flag_type flag) const { return operator|(Flags(flag)); } |
| 56 Flags operator^(flag_type flag) const { return operator^(Flags(flag)); } | 65 Flags operator^(flag_type flag) const { return operator^(Flags(flag)); } |
| 57 | 66 |
| 58 Flags operator~() const { return Flags(~mask_); } | 67 Flags operator~() const { return Flags(~mask_); } |
| 59 | 68 |
| 60 operator mask_type() const { return mask_; } | 69 operator mask_type() const { return mask_; } |
| 61 bool operator!() const { return !mask_; } | 70 bool operator!() const { return !mask_; } |
| 62 | 71 |
| 72 friend size_t hash_value(const Flags& flags) { return flags.mask_; } |
| 73 |
| 63 private: | 74 private: |
| 64 mask_type mask_; | 75 mask_type mask_; |
| 65 }; | 76 }; |
| 66 | 77 |
| 67 | 78 |
| 68 #define DEFINE_OPERATORS_FOR_FLAGS(Type) \ | 79 #define DEFINE_OPERATORS_FOR_FLAGS(Type) \ |
| 69 inline Type operator&( \ | 80 inline Type operator&( \ |
| 70 Type::flag_type lhs, \ | 81 Type::flag_type lhs, \ |
| 71 Type::flag_type rhs)ALLOW_UNUSED_TYPE WARN_UNUSED_RESULT; \ | 82 Type::flag_type rhs)ALLOW_UNUSED_TYPE WARN_UNUSED_RESULT; \ |
| 72 inline Type operator&(Type::flag_type lhs, Type::flag_type rhs) { \ | 83 inline Type operator&(Type::flag_type lhs, Type::flag_type rhs) { \ |
| (...skipping 29 matching lines...) Expand all Loading... |
| 102 inline Type operator^(Type::flag_type lhs, const Type& rhs) { \ | 113 inline Type operator^(Type::flag_type lhs, const Type& rhs) { \ |
| 103 return rhs ^ lhs; \ | 114 return rhs ^ lhs; \ |
| 104 } inline void operator^(Type::flag_type lhs, Type::mask_type rhs) \ | 115 } inline void operator^(Type::flag_type lhs, Type::mask_type rhs) \ |
| 105 ALLOW_UNUSED_TYPE; \ | 116 ALLOW_UNUSED_TYPE; \ |
| 106 inline void operator^(Type::flag_type lhs, Type::mask_type rhs) {} | 117 inline void operator^(Type::flag_type lhs, Type::mask_type rhs) {} |
| 107 | 118 |
| 108 } // namespace base | 119 } // namespace base |
| 109 } // namespace v8 | 120 } // namespace v8 |
| 110 | 121 |
| 111 #endif // V8_BASE_FLAGS_H_ | 122 #endif // V8_BASE_FLAGS_H_ |
| OLD | NEW |