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 "include/v8config.h" | 8 #include "include/v8config.h" |
9 | 9 |
10 namespace v8 { | 10 namespace v8 { |
11 namespace base { | 11 namespace base { |
12 | 12 |
13 // The Flags class provides a type-safe way of storing OR-combinations of enum | 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. | 14 // 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). |
15 // | 16 // |
16 // The traditional C++ approach for storing OR-combinations of enum values is to | 17 // 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 // 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 // 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 // other enum value and passed on to a function that takes an int or unsigned |
20 // int. | 21 // int. |
21 template <typename T> | 22 template <typename T, typename S = int> |
22 class Flags V8_FINAL { | 23 class Flags V8_FINAL { |
23 public: | 24 public: |
24 typedef T flag_type; | 25 typedef T flag_type; |
25 typedef int mask_type; | 26 typedef S mask_type; |
26 | 27 |
27 Flags() : mask_(0) {} | 28 Flags() : mask_(0) {} |
28 Flags(flag_type flag) : mask_(flag) {} // NOLINT(runtime/explicit) | 29 Flags(flag_type flag) : mask_(flag) {} // NOLINT(runtime/explicit) |
29 explicit Flags(mask_type mask) : mask_(mask) {} | 30 explicit Flags(mask_type mask) : mask_(mask) {} |
30 | 31 |
31 Flags& operator&=(const Flags& flags) { | 32 Flags& operator&=(const Flags& flags) { |
32 mask_ &= flags.mask_; | 33 mask_ &= flags.mask_; |
33 return *this; | 34 return *this; |
34 } | 35 } |
35 Flags& operator|=(const Flags& flags) { | 36 Flags& operator|=(const Flags& flags) { |
(...skipping 20 matching lines...) Expand all Loading... |
56 Flags operator~() const { return Flags(~mask_); } | 57 Flags operator~() const { return Flags(~mask_); } |
57 | 58 |
58 operator mask_type() const { return mask_; } | 59 operator mask_type() const { return mask_; } |
59 bool operator!() const { return !mask_; } | 60 bool operator!() const { return !mask_; } |
60 | 61 |
61 private: | 62 private: |
62 mask_type mask_; | 63 mask_type mask_; |
63 }; | 64 }; |
64 | 65 |
65 | 66 |
66 #define DEFINE_FLAGS(Type, Enum) typedef ::v8::base::Flags<Enum> Type | |
67 | |
68 #define DEFINE_OPERATORS_FOR_FLAGS(Type) \ | 67 #define DEFINE_OPERATORS_FOR_FLAGS(Type) \ |
69 inline ::v8::base::Flags<Type::flag_type> operator&( \ | 68 inline ::v8::base::Flags<Type::flag_type> operator&( \ |
70 Type::flag_type lhs, \ | 69 Type::flag_type lhs, \ |
71 Type::flag_type rhs)V8_UNUSED V8_WARN_UNUSED_RESULT; \ | 70 Type::flag_type rhs)V8_UNUSED V8_WARN_UNUSED_RESULT; \ |
72 inline ::v8::base::Flags<Type::flag_type> operator&(Type::flag_type lhs, \ | 71 inline ::v8::base::Flags<Type::flag_type> operator&(Type::flag_type lhs, \ |
73 Type::flag_type rhs) { \ | 72 Type::flag_type rhs) { \ |
74 return ::v8::base::Flags<Type::flag_type>(lhs) & rhs; \ | 73 return ::v8::base::Flags<Type::flag_type>(lhs) & rhs; \ |
75 } \ | 74 } \ |
76 inline ::v8::base::Flags<Type::flag_type> operator&( \ | 75 inline ::v8::base::Flags<Type::flag_type> operator&( \ |
77 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) \ | 76 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) \ |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 inline ::v8::base::Flags<Type::flag_type> operator^( \ | 110 inline ::v8::base::Flags<Type::flag_type> operator^( \ |
112 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) { \ | 111 Type::flag_type lhs, const ::v8::base::Flags<Type::flag_type>& rhs) { \ |
113 return rhs ^ lhs; \ | 112 return rhs ^ lhs; \ |
114 } inline void operator^(Type::flag_type lhs, Type::mask_type rhs) V8_UNUSED; \ | 113 } 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) {} | 114 inline void operator^(Type::flag_type lhs, Type::mask_type rhs) {} |
116 | 115 |
117 } // namespace base | 116 } // namespace base |
118 } // namespace v8 | 117 } // namespace v8 |
119 | 118 |
120 #endif // V8_BASE_FLAGS_H_ | 119 #endif // V8_BASE_FLAGS_H_ |
OLD | NEW |