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