Index: components/sync/base/enum_set.h |
diff --git a/components/sync/base/enum_set.h b/components/sync/base/enum_set.h |
index c6e7b77688a3e1102a52c509fcab15a359d64757..9967b4c030196554b4e258618fc46a411ed9afa5 100644 |
--- a/components/sync/base/enum_set.h |
+++ b/components/sync/base/enum_set.h |
@@ -101,7 +101,7 @@ class EnumSet { |
} |
private: |
- friend Iterator EnumSet::First() const; |
+ friend constexpr Iterator EnumSet::First() const; |
explicit Iterator(const EnumBitSet& enums) |
: enums_(&enums), i_(FindNext(0)) {} |
@@ -117,7 +117,9 @@ class EnumSet { |
size_t i_; |
}; |
- EnumSet() {} |
+ EnumSet() = default; |
+ |
+ ~EnumSet() = default; |
// Recursively chain constructors. Base case is the empty pack. |
template <class... T> |
@@ -125,22 +127,35 @@ class EnumSet { |
Put(head); |
} |
- // Returns an EnumSet with all possible values. |
- static EnumSet All() { |
- EnumBitSet enums; |
- enums.set(); |
- return EnumSet(enums); |
+ // For a given list of enum values, calculates the equivalent 64 bit bitstring |
+ // which is represented as a uint64_t. |
+ constexpr uint64_t bitstring( |
+ typename std::initializer_list<E>::const_iterator const& first, |
+ typename std::initializer_list<E>::const_iterator const& last) const { |
+ static_assert( |
+ kValueCount < 64, |
+ "Max number of enum values is 64 for constexpr initialization"); |
+ return first != last ? ((1ULL << unsigned(ToIndex(*first))) | |
+ bitstring(first + 1, last)) |
+ : 0; |
} |
+ constexpr EnumSet(const std::initializer_list<E>& enum_list) |
+ : EnumSet(EnumBitSet(bitstring(begin(enum_list), end(enum_list)))) {} |
+ |
+ // Returns an EnumSet with all possible values. |
+ static constexpr EnumSet All() { return EnumSet((1ULL << kValueCount) - 1); } |
+ |
// Returns an EnumSet with all the values from start to end, inclusive. |
- static EnumSet FromRange(E start, E end) { |
- EnumSet set; |
- set.PutRange(start, end); |
- return set; |
+ static constexpr EnumSet FromRange(E start, E end) { |
+ static_assert( |
+ kValueCount < 64, |
+ "Max number of enum values is 64 for constexpr initialization"); |
+ return EnumSet( |
+ EnumBitSet(((1ULL << ToIndex(end)) - (1ULL << ToIndex(start))) | |
+ (1ULL << ToIndex(end)))); |
} |
- ~EnumSet() {} |
- |
// Copy constructor and assignment welcome. |
// Set operations. Put, Retain, and Remove are basically |
@@ -182,8 +197,8 @@ class EnumSet { |
void Clear() { enums_.reset(); } |
// Returns true iff the given value is in range and a member of our set. |
- bool Has(E value) const { |
- return InRange(value) && enums_.test(ToIndex(value)); |
+ constexpr bool Has(E value) const { |
+ return InRange(value) && enums_[ToIndex(value)]; |
} |
// Returns true iff the given set is a subset of our set. |
@@ -198,7 +213,7 @@ class EnumSet { |
size_t Size() const { return enums_.count(); } |
// Returns an iterator pointing to the first element (if any). |
- Iterator First() const { return Iterator(enums_); } |
+ constexpr Iterator First() const { return Iterator(enums_); } |
// Returns true iff our set and the given set contain exactly the same values. |
bool operator==(const EnumSet& other) const { return enums_ == other.enums_; } |
@@ -215,19 +230,15 @@ class EnumSet { |
friend EnumSet Difference<E, MinEnumValue, MaxEnumValue>(EnumSet set1, |
EnumSet set2); |
- explicit EnumSet(EnumBitSet enums) : enums_(enums) {} |
+ explicit constexpr EnumSet(EnumBitSet enums) : enums_(enums) {} |
- static bool InRange(E value) { |
+ static constexpr bool InRange(E value) { |
return (value >= MinEnumValue) && (value <= MaxEnumValue); |
} |
// Converts a value to/from an index into |enums_|. |
- static size_t ToIndex(E value) { |
- DCHECK_GE(value, MinEnumValue); |
- DCHECK_LE(value, MaxEnumValue); |
- return value - MinEnumValue; |
- } |
+ static constexpr size_t ToIndex(E value) { return value - MinEnumValue; } |
static E FromIndex(size_t i) { |
DCHECK_LT(i, kValueCount); |