enum-set.h 2.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// Copyright 2019 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_ENUM_SET_H_
#define V8_BASE_ENUM_SET_H_

#include <type_traits>

#include "src/base/logging.h"

namespace v8 {
namespace base {

// A poor man's version of STL's bitset: A bit set of enums E (without explicit
// values), fitting into an integral type T.
template <class E, class T = int>
class EnumSet {
  static_assert(std::is_enum<E>::value, "EnumSet can only be used with enums");

 public:
22 23
  constexpr EnumSet() = default;

24 25 26 27
  explicit constexpr EnumSet(std::initializer_list<E> init) {
    T bits = 0;
    for (E e : init) bits |= Mask(e);
    bits_ = bits;
28 29
  }

30 31 32 33 34 35 36
  constexpr bool empty() const { return bits_ == 0; }
  constexpr bool contains(E element) const {
    return (bits_ & Mask(element)) != 0;
  }
  constexpr bool contains_any(EnumSet set) const {
    return (bits_ & set.bits_) != 0;
  }
37
  void Add(E element) { bits_ |= Mask(element); }
38
  void Add(EnumSet set) { bits_ |= set.bits_; }
39
  void Remove(E element) { bits_ &= ~Mask(element); }
40
  void Remove(EnumSet set) { bits_ &= ~set.bits_; }
41
  void RemoveAll() { bits_ = 0; }
42
  void Intersect(EnumSet set) { bits_ &= set.bits_; }
43
  constexpr T ToIntegral() const { return bits_; }
Clemens Backes's avatar
Clemens Backes committed
44

45 46
  constexpr bool operator==(EnumSet set) const { return bits_ == set.bits_; }
  constexpr bool operator!=(EnumSet set) const { return bits_ != set.bits_; }
Clemens Backes's avatar
Clemens Backes committed
47

48 49 50 51 52 53 54 55 56
  constexpr EnumSet operator|(EnumSet set) const {
    return EnumSet(bits_ | set.bits_);
  }
  constexpr EnumSet operator&(EnumSet set) const {
    return EnumSet(bits_ & set.bits_);
  }
  constexpr EnumSet operator-(EnumSet set) const {
    return EnumSet(bits_ & ~set.bits_);
  }
Clemens Backes's avatar
Clemens Backes committed
57 58 59 60 61

  EnumSet& operator|=(EnumSet set) { return *this = *this | set; }
  EnumSet& operator&=(EnumSet set) { return *this = *this & set; }
  EnumSet& operator-=(EnumSet set) { return *this = *this - set; }

62 63 64 65 66 67 68 69 70
  constexpr EnumSet operator|(E element) const {
    return EnumSet(bits_ | Mask(element));
  }
  constexpr EnumSet operator&(E element) const {
    return EnumSet(bits_ & Mask(element));
  }
  constexpr EnumSet operator-(E element) const {
    return EnumSet(bits_ & ~Mask(element));
  }
Clemens Backes's avatar
Clemens Backes committed
71 72 73 74

  EnumSet& operator|=(E element) { return *this = *this | element; }
  EnumSet& operator&=(E element) { return *this = *this & element; }
  EnumSet& operator-=(E element) { return *this = *this - element; }
75

76 77
  static constexpr EnumSet FromIntegral(T bits) { return EnumSet{bits}; }

78
 private:
79 80
  explicit constexpr EnumSet(T bits) : bits_(bits) {}

81
  static constexpr T Mask(E element) {
82
    CONSTEXPR_DCHECK(sizeof(T) * 8 > static_cast<size_t>(element));
83 84 85 86 87 88 89 90 91 92
    return T{1} << static_cast<typename std::underlying_type<E>::type>(element);
  }

  T bits_ = 0;
};

}  // namespace base
}  // namespace v8

#endif  // V8_BASE_ENUM_SET_H_