flags.h 5.22 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 9
#include <cstddef>

10
#include "src/base/compiler-specific.h"
11 12 13 14 15

namespace v8 {
namespace base {

// The Flags class provides a type-safe way of storing OR-combinations of enum
16 17
// 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).
18 19 20 21 22 23
//
// 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.
24
template <typename T, typename S = int>
25
class Flags final {
26
 public:
27 28
  using flag_type = T;
  using mask_type = S;
29

30 31
  constexpr Flags() : mask_(0) {}
  constexpr Flags(flag_type flag)  // NOLINT(runtime/explicit)
32
      : mask_(static_cast<S>(flag)) {}
33
  constexpr explicit Flags(mask_type mask) : mask_(static_cast<S>(mask)) {}
34

35
  constexpr bool operator==(flag_type flag) const {
36 37
    return mask_ == static_cast<S>(flag);
  }
38
  constexpr bool operator!=(flag_type flag) const {
39 40 41
    return mask_ != static_cast<S>(flag);
  }

42 43 44 45 46 47 48 49 50 51 52 53 54
  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;
  }

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 66 67 68

  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)); }

69 70 71 72 73 74 75 76 77
  constexpr Flags operator&(flag_type flag) const {
    return operator&(Flags(flag));
  }
  constexpr Flags operator|(flag_type flag) const {
    return operator|(Flags(flag));
  }
  constexpr Flags operator^(flag_type flag) const {
    return operator^(Flags(flag));
  }
78

79
  constexpr Flags operator~() const { return Flags(~mask_); }
80

81 82
  constexpr operator mask_type() const { return mask_; }
  constexpr bool operator!() const { return !mask_; }
83

84
  Flags without(flag_type flag) const { return *this & (~Flags(flag)); }
85

86 87
  friend size_t hash_value(const Flags& flags) { return flags.mask_; }

88 89 90 91
 private:
  mask_type mask_;
};

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#define DEFINE_OPERATORS_FOR_FLAGS(Type)                                 \
  V8_ALLOW_UNUSED V8_WARN_UNUSED_RESULT inline constexpr Type operator&( \
      Type::flag_type lhs, Type::flag_type rhs) {                        \
    return Type(lhs) & rhs;                                              \
  }                                                                      \
  V8_ALLOW_UNUSED V8_WARN_UNUSED_RESULT inline constexpr Type operator&( \
      Type::flag_type lhs, const Type& rhs) {                            \
    return rhs & lhs;                                                    \
  }                                                                      \
  V8_ALLOW_UNUSED inline void operator&(Type::flag_type lhs,             \
                                        Type::mask_type rhs) {}          \
  V8_ALLOW_UNUSED V8_WARN_UNUSED_RESULT inline constexpr Type operator|( \
      Type::flag_type lhs, Type::flag_type rhs) {                        \
    return Type(lhs) | rhs;                                              \
  }                                                                      \
  V8_ALLOW_UNUSED V8_WARN_UNUSED_RESULT inline constexpr Type operator|( \
      Type::flag_type lhs, const Type& rhs) {                            \
    return rhs | lhs;                                                    \
  }                                                                      \
  V8_ALLOW_UNUSED inline void operator|(Type::flag_type lhs,             \
                                        Type::mask_type rhs) {}          \
  V8_ALLOW_UNUSED V8_WARN_UNUSED_RESULT inline constexpr Type operator^( \
      Type::flag_type lhs, Type::flag_type rhs) {                        \
    return Type(lhs) ^ rhs;                                              \
  }                                                                      \
  V8_ALLOW_UNUSED V8_WARN_UNUSED_RESULT inline constexpr Type operator^( \
      Type::flag_type lhs, const Type& rhs) {                            \
    return rhs ^ lhs;                                                    \
  }                                                                      \
  V8_ALLOW_UNUSED inline void operator^(Type::flag_type lhs,             \
                                        Type::mask_type rhs) {}          \
  V8_ALLOW_UNUSED inline constexpr Type operator~(Type::flag_type val) { \
    return ~Type(val);                                                   \
125
  }
126 127 128 129 130

}  // namespace base
}  // namespace v8

#endif  // V8_BASE_FLAGS_H_