Commit a07c2de2 authored by Clemens Hammacher's avatar Clemens Hammacher Committed by Commit Bot

[utils] Prepare EnumSet for scoped enums

Currently, EnumSet cannot be used with scoped enums (declared as "enum
class" or "enum struct"), as these cannot be implicitly casted to their
underlying integral type. This CL changes this by adding a static cast.
Plus drive-by fixes.

R=mstarzinger@chromium.org

Change-Id: I3aa2ef78e896b2734f1e5695237efc8a1130703c
Reviewed-on: https://chromium-review.googlesource.com/598789
Commit-Queue: Clemens Hammacher <clemensh@chromium.org>
Reviewed-by: 's avatarMichael Starzinger <mstarzinger@chromium.org>
Cr-Commit-Position: refs/heads/master@{#47120}
parent b4e55e94
......@@ -9,6 +9,7 @@
#include <stdlib.h>
#include <string.h>
#include <cmath>
#include <type_traits>
#include "include/v8.h"
#include "src/allocation.h"
......@@ -797,16 +798,16 @@ class EnumSet {
T ToIntegral() const { return bits_; }
bool operator==(const EnumSet& set) { return bits_ == set.bits_; }
bool operator!=(const EnumSet& set) { return bits_ != set.bits_; }
EnumSet<E, T> operator|(const EnumSet& set) const {
return EnumSet<E, T>(bits_ | set.bits_);
EnumSet operator|(const EnumSet& set) const {
return EnumSet(bits_ | set.bits_);
}
private:
static_assert(std::is_enum<E>::value, "EnumSet can only be used with enums");
T Mask(E element) const {
// The strange typing in DCHECK is necessary to avoid stupid warnings, see:
// http://gcc.gnu.org/bugzilla/show_bug.cgi?id=43680
DCHECK(static_cast<int>(element) < static_cast<int>(sizeof(T) * CHAR_BIT));
return static_cast<T>(1) << element;
DCHECK_GT(sizeof(T) * CHAR_BIT, static_cast<int>(element));
return T{1} << static_cast<typename std::underlying_type<E>::type>(element);
}
T bits_;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment