flags.h 5.15 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_BASE_FLAGS_H_
#define V8_BASE_FLAGS_H_

8
#include "src/base/compiler-specific.h"
9 10 11 12 13

namespace v8 {
namespace base {

// The Flags class provides a type-safe way of storing OR-combinations of enum
14 15
// values. The Flags<T, S> class is a template class, where T is an enum type,
// and S is the underlying storage type (usually int).
16 17 18 19 20 21
//
// The traditional C++ approach for storing OR-combinations of enum values is to
// use an int or unsigned int variable. The inconvenience with this approach is
// that there's no type checking at all; any enum value can be OR'd with any
// other enum value and passed on to a function that takes an int or unsigned
// int.
22
template <typename T, typename S = int>
23
class Flags FINAL {
24 25
 public:
  typedef T flag_type;
26
  typedef S mask_type;
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

  Flags() : mask_(0) {}
  Flags(flag_type flag) : mask_(flag) {}  // NOLINT(runtime/explicit)
  explicit Flags(mask_type mask) : mask_(mask) {}

  Flags& operator&=(const Flags& flags) {
    mask_ &= flags.mask_;
    return *this;
  }
  Flags& operator|=(const Flags& flags) {
    mask_ |= flags.mask_;
    return *this;
  }
  Flags& operator^=(const Flags& flags) {
    mask_ ^= flags.mask_;
    return *this;
  }

  Flags operator&(const Flags& flags) const { return Flags(*this) &= flags; }
  Flags operator|(const Flags& flags) const { return Flags(*this) |= flags; }
  Flags operator^(const Flags& flags) const { return Flags(*this) ^= flags; }

  Flags& operator&=(flag_type flag) { return operator&=(Flags(flag)); }
  Flags& operator|=(flag_type flag) { return operator|=(Flags(flag)); }
  Flags& operator^=(flag_type flag) { return operator^=(Flags(flag)); }

  Flags operator&(flag_type flag) const { return operator&(Flags(flag)); }
  Flags operator|(flag_type flag) const { return operator|(Flags(flag)); }
  Flags operator^(flag_type flag) const { return operator^(Flags(flag)); }

  Flags operator~() const { return Flags(~mask_); }

  operator mask_type() const { return mask_; }
  bool operator!() const { return !mask_; }

 private:
  mask_type mask_;
};


#define DEFINE_OPERATORS_FOR_FLAGS(Type)                                       \
68 69 70 71
  inline Type operator&(Type::flag_type lhs,                                   \
                        Type::flag_type rhs)ALLOW_UNUSED WARN_UNUSED_RESULT;   \
  inline Type operator&(Type::flag_type lhs, Type::flag_type rhs) {            \
    return Type(lhs) & rhs;                                                    \
72
  }                                                                            \
73 74 75
  inline Type operator&(Type::flag_type lhs,                                   \
                        const Type& rhs)ALLOW_UNUSED WARN_UNUSED_RESULT;       \
  inline Type operator&(Type::flag_type lhs, const Type& rhs) {                \
76 77
    return rhs & lhs;                                                          \
  }                                                                            \
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 83
  inline Type operator|(Type::flag_type lhs, Type::flag_type rhs) {            \
    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 89
    return rhs | lhs;                                                          \
  }                                                                            \
90 91
  inline void operator|(Type::flag_type lhs, Type::mask_type rhs)              \
      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 96 97
  inline Type operator^(Type::flag_type lhs, Type::flag_type rhs) {            \
    return Type(lhs) ^ rhs;                                                    \
  } 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 102
  } inline void operator^(Type::flag_type lhs, Type::mask_type rhs)            \
      ALLOW_UNUSED;                                                            \
103 104 105 106 107 108
  inline void operator^(Type::flag_type lhs, Type::mask_type rhs) {}

}  // namespace base
}  // namespace v8

#endif  // V8_BASE_FLAGS_H_