OLD | NEW |
(Empty) | |
| 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_BASE_FLAGS_H_ |
| 6 #define V8_BASE_FLAGS_H_ |
| 7 |
| 8 #include "include/v8config.h" |
| 9 |
| 10 namespace v8 { |
| 11 namespace base { |
| 12 |
| 13 // The Flags class provides a type-safe way of storing OR-combinations of enum |
| 14 // values. The Flags<T> class is a template class, where T is an enum type. |
| 15 // |
| 16 // The traditional C++ approach for storing OR-combinations of enum values is to |
| 17 // use an int or unsigned int variable. The inconvenience with this approach is |
| 18 // that there's no type checking at all; any enum value can be OR'd with any |
| 19 // other enum value and passed on to a function that takes an int or unsigned |
| 20 // int. |
| 21 template <typename T> |
| 22 class Flags V8_FINAL { |
| 23 public: |
| 24 typedef T flag_type; |
| 25 typedef int mask_type; |
| 26 |
| 27 Flags() : mask_(0) {} |
| 28 Flags(flag_type flag) : mask_(flag) {} // NOLINT(runtime/explicit) |
| 29 explicit Flags(mask_type mask) : mask_(mask) {} |
| 30 |
| 31 Flags& operator&=(const Flags& flags) { |
| 32 mask_ &= flags.mask_; |
| 33 return *this; |
| 34 } |
| 35 Flags& operator|=(const Flags& flags) { |
| 36 mask_ |= flags.mask_; |
| 37 return *this; |
| 38 } |
| 39 Flags& operator^=(const Flags& flags) { |
| 40 mask_ ^= flags.mask_; |
| 41 return *this; |
| 42 } |
| 43 |
| 44 Flags operator&(const Flags& flags) const { return Flags(*this) &= flags; } |
| 45 Flags operator|(const Flags& flags) const { return Flags(*this) |= flags; } |
| 46 Flags operator^(const Flags& flags) const { return Flags(*this) ^= flags; } |
| 47 |
| 48 Flags& operator&=(flag_type flag) { return operator&=(Flags(flag)); } |
| 49 Flags& operator|=(flag_type flag) { return operator|=(Flags(flag)); } |
| 50 Flags& operator^=(flag_type flag) { return operator^=(Flags(flag)); } |
| 51 |
| 52 Flags operator&(flag_type flag) const { return operator&(Flags(flag)); } |
| 53 Flags operator|(flag_type flag) const { return operator|(Flags(flag)); } |
| 54 Flags operator^(flag_type flag) const { return operator^(Flags(flag)); } |
| 55 |
| 56 Flags operator~() const { return Flags(~mask_); } |
| 57 |
| 58 operator mask_type() const { return mask_; } |
| 59 bool operator!() const { return !mask_; } |
| 60 |
| 61 private: |
| 62 mask_type mask_; |
| 63 }; |
| 64 |
| 65 |
| 66 #define DEFINE_FLAGS(Type, Enum) typedef ::v8::base::Flags<Enum> Type |
| 67 |
| 68 #define DEFINE_OPERATORS_FOR_FLAGS(Type) \ |
| 69 inline ::v8::base::Flags<Type::flag_type> operator&( \ |
| 70 Type::flag_type lhs, \ |
| 71 Type::flag_type rhs)V8_UNUSED V8_WARN_UNUSED_RESULT; \ |
| 72 inline ::v8::base::Flags<Type::flag_type> operator&(Type::flag_type lhs, \ |
| 73 Type::flag_type rhs) { \ |
| 74 return ::v8::base::Flags<Type::flag_type>(lhs) & rhs; \ |
| 75 } \ |
| 76 inline ::v8::base::Flags<Type::flag_type> operator&( \ |
| 77 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) \ |
| 78 V8_UNUSED V8_WARN_UNUSED_RESULT; \ |
| 79 inline ::v8::base::Flags<Type::flag_type> operator&( \ |
| 80 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) { \ |
| 81 return rhs & lhs; \ |
| 82 } \ |
| 83 inline void operator&(Type::flag_type lhs, Type::mask_type rhs)V8_UNUSED; \ |
| 84 inline void operator&(Type::flag_type lhs, Type::mask_type rhs) {} \ |
| 85 inline ::v8::base::Flags<Type::flag_type> operator|(Type::flag_type lhs, \ |
| 86 Type::flag_type rhs) \ |
| 87 V8_UNUSED V8_WARN_UNUSED_RESULT; \ |
| 88 inline ::v8::base::Flags<Type::flag_type> operator|(Type::flag_type lhs, \ |
| 89 Type::flag_type rhs) { \ |
| 90 return ::v8::base::Flags<Type::flag_type>(lhs) | rhs; \ |
| 91 } \ |
| 92 inline ::v8::base::Flags<Type::flag_type> operator|( \ |
| 93 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) \ |
| 94 V8_UNUSED V8_WARN_UNUSED_RESULT; \ |
| 95 inline ::v8::base::Flags<Type::flag_type> operator|( \ |
| 96 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) { \ |
| 97 return rhs | lhs; \ |
| 98 } \ |
| 99 inline void operator|(Type::flag_type lhs, Type::mask_type rhs) V8_UNUSED; \ |
| 100 inline void operator|(Type::flag_type lhs, Type::mask_type rhs) {} \ |
| 101 inline ::v8::base::Flags<Type::flag_type> operator^(Type::flag_type lhs, \ |
| 102 Type::flag_type rhs) \ |
| 103 V8_UNUSED V8_WARN_UNUSED_RESULT; \ |
| 104 inline ::v8::base::Flags<Type::flag_type> operator^(Type::flag_type lhs, \ |
| 105 Type::flag_type rhs) { \ |
| 106 return ::v8::base::Flags<Type::flag_type>(lhs) ^ rhs; \ |
| 107 } inline ::v8::base::Flags<Type::flag_type> \ |
| 108 operator^(Type::flag_type lhs, \ |
| 109 const ::v8::base::Flags<Type::flag_type>& rhs) \ |
| 110 V8_UNUSED V8_WARN_UNUSED_RESULT; \ |
| 111 inline ::v8::base::Flags<Type::flag_type> operator^( \ |
| 112 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) { \ |
| 113 return rhs ^ lhs; \ |
| 114 } inline void operator^(Type::flag_type lhs, Type::mask_type rhs) V8_UNUSED; \ |
| 115 inline void operator^(Type::flag_type lhs, Type::mask_type rhs) {} |
| 116 |
| 117 } // namespace base |
| 118 } // namespace v8 |
| 119 |
| 120 #endif // V8_BASE_FLAGS_H_ |
OLD | NEW |