Home | History | Annotate | Download | only in base
      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 "src/base/compiler-specific.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, S> class is a template class, where T is an enum type,
     15 // and S is the underlying storage type (usually int).
     16 //
     17 // 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
     19 // 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
     21 // int.
     22 template <typename T, typename S = int>
     23 class Flags FINAL {
     24  public:
     25   typedef T flag_type;
     26   typedef S mask_type;
     27 
     28   Flags() : mask_(0) {}
     29   Flags(flag_type flag) : mask_(flag) {}  // NOLINT(runtime/explicit)
     30   explicit Flags(mask_type mask) : mask_(mask) {}
     31 
     32   Flags& operator&=(const Flags& flags) {
     33     mask_ &= flags.mask_;
     34     return *this;
     35   }
     36   Flags& operator|=(const Flags& flags) {
     37     mask_ |= flags.mask_;
     38     return *this;
     39   }
     40   Flags& operator^=(const Flags& flags) {
     41     mask_ ^= flags.mask_;
     42     return *this;
     43   }
     44 
     45   Flags operator&(const Flags& flags) const { return Flags(*this) &= flags; }
     46   Flags operator|(const Flags& flags) const { return Flags(*this) |= flags; }
     47   Flags operator^(const Flags& flags) const { return Flags(*this) ^= flags; }
     48 
     49   Flags& operator&=(flag_type flag) { return operator&=(Flags(flag)); }
     50   Flags& operator|=(flag_type flag) { return operator|=(Flags(flag)); }
     51   Flags& operator^=(flag_type flag) { return operator^=(Flags(flag)); }
     52 
     53   Flags operator&(flag_type flag) const { return operator&(Flags(flag)); }
     54   Flags operator|(flag_type flag) const { return operator|(Flags(flag)); }
     55   Flags operator^(flag_type flag) const { return operator^(Flags(flag)); }
     56 
     57   Flags operator~() const { return Flags(~mask_); }
     58 
     59   operator mask_type() const { return mask_; }
     60   bool operator!() const { return !mask_; }
     61 
     62  private:
     63   mask_type mask_;
     64 };
     65 
     66 
     67 #define DEFINE_OPERATORS_FOR_FLAGS(Type)                                       \
     68   inline Type operator&(Type::flag_type lhs,                                   \
     69                         Type::flag_type rhs)ALLOW_UNUSED WARN_UNUSED_RESULT;   \
     70   inline Type operator&(Type::flag_type lhs, Type::flag_type rhs) {            \
     71     return Type(lhs) & rhs;                                                    \
     72   }                                                                            \
     73   inline Type operator&(Type::flag_type lhs,                                   \
     74                         const Type& rhs)ALLOW_UNUSED WARN_UNUSED_RESULT;       \
     75   inline Type operator&(Type::flag_type lhs, const Type& rhs) {                \
     76     return rhs & lhs;                                                          \
     77   }                                                                            \
     78   inline void operator&(Type::flag_type lhs, Type::mask_type rhs)ALLOW_UNUSED; \
     79   inline void operator&(Type::flag_type lhs, Type::mask_type rhs) {}           \
     80   inline Type operator|(Type::flag_type lhs, Type::flag_type rhs)              \
     81       ALLOW_UNUSED WARN_UNUSED_RESULT;                                         \
     82   inline Type operator|(Type::flag_type lhs, Type::flag_type rhs) {            \
     83     return Type(lhs) | rhs;                                                    \
     84   }                                                                            \
     85   inline Type operator|(Type::flag_type lhs, const Type& rhs)                  \
     86       ALLOW_UNUSED WARN_UNUSED_RESULT;                                         \
     87   inline Type operator|(Type::flag_type lhs, const Type& rhs) {                \
     88     return rhs | lhs;                                                          \
     89   }                                                                            \
     90   inline void operator|(Type::flag_type lhs, Type::mask_type rhs)              \
     91       ALLOW_UNUSED;                                                            \
     92   inline void operator|(Type::flag_type lhs, Type::mask_type rhs) {}           \
     93   inline Type operator^(Type::flag_type lhs, Type::flag_type rhs)              \
     94       ALLOW_UNUSED WARN_UNUSED_RESULT;                                         \
     95   inline Type operator^(Type::flag_type lhs, Type::flag_type rhs) {            \
     96     return Type(lhs) ^ rhs;                                                    \
     97   } inline Type operator^(Type::flag_type lhs, const Type& rhs)                \
     98       ALLOW_UNUSED WARN_UNUSED_RESULT;                                         \
     99   inline Type operator^(Type::flag_type lhs, const Type& rhs) {                \
    100     return rhs ^ lhs;                                                          \
    101   } inline void operator^(Type::flag_type lhs, Type::mask_type rhs)            \
    102       ALLOW_UNUSED;                                                            \
    103   inline void operator^(Type::flag_type lhs, Type::mask_type rhs) {}
    104 
    105 }  // namespace base
    106 }  // namespace v8
    107 
    108 #endif  // V8_BASE_FLAGS_H_
    109