2013-07-24 11:41:39 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-11-07 03:23:13 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* A set abstraction for enumeration values. */
|
|
|
|
|
|
|
|
#ifndef mozilla_EnumSet_h
|
|
|
|
#define mozilla_EnumSet_h
|
|
|
|
|
|
|
|
#include "mozilla/Assertions.h"
|
2014-06-07 07:17:06 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2016-10-19 07:33:14 +03:00
|
|
|
|
|
|
|
#include <initializer_list>
|
2013-07-30 18:25:31 +04:00
|
|
|
|
|
|
|
#include <stdint.h>
|
2012-11-07 03:23:13 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* EnumSet<T> is a set of values defined by an enumeration. It is implemented
|
|
|
|
* using a 32 bit mask for each value so it will only work for enums with an int
|
|
|
|
* representation less than 32. It works both for enum and enum class types.
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class EnumSet
|
|
|
|
{
|
2014-06-13 10:34:08 +04:00
|
|
|
public:
|
|
|
|
EnumSet()
|
|
|
|
: mBitField(0)
|
2016-04-28 16:25:05 +03:00
|
|
|
{
|
|
|
|
initVersion();
|
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
|
|
|
MOZ_IMPLICIT EnumSet(T aEnum)
|
|
|
|
: mBitField(bitFor(aEnum))
|
|
|
|
{ }
|
|
|
|
|
|
|
|
EnumSet(T aEnum1, T aEnum2)
|
|
|
|
: mBitField(bitFor(aEnum1) |
|
|
|
|
bitFor(aEnum2))
|
2016-04-28 16:25:05 +03:00
|
|
|
{
|
|
|
|
initVersion();
|
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
|
|
|
EnumSet(T aEnum1, T aEnum2, T aEnum3)
|
|
|
|
: mBitField(bitFor(aEnum1) |
|
|
|
|
bitFor(aEnum2) |
|
|
|
|
bitFor(aEnum3))
|
2016-04-28 16:25:05 +03:00
|
|
|
{
|
|
|
|
initVersion();
|
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
|
|
|
EnumSet(T aEnum1, T aEnum2, T aEnum3, T aEnum4)
|
2016-04-28 16:25:05 +03:00
|
|
|
: mBitField(bitFor(aEnum1) |
|
|
|
|
bitFor(aEnum2) |
|
|
|
|
bitFor(aEnum3) |
|
|
|
|
bitFor(aEnum4))
|
|
|
|
{
|
|
|
|
initVersion();
|
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
2016-04-28 16:25:05 +03:00
|
|
|
MOZ_IMPLICIT EnumSet(std::initializer_list<T> list)
|
|
|
|
: mBitField(0)
|
|
|
|
{
|
|
|
|
for (auto value : list) {
|
|
|
|
(*this) += value;
|
|
|
|
}
|
|
|
|
initVersion();
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
EnumSet(const EnumSet& aEnumSet)
|
2016-04-28 16:25:05 +03:00
|
|
|
: mBitField(aEnumSet.mBitField)
|
|
|
|
{
|
|
|
|
initVersion();
|
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an element
|
|
|
|
*/
|
|
|
|
void operator+=(T aEnum)
|
|
|
|
{
|
2016-04-28 16:25:05 +03:00
|
|
|
incVersion();
|
2014-06-13 10:34:08 +04:00
|
|
|
mBitField |= bitFor(aEnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add an element
|
|
|
|
*/
|
|
|
|
EnumSet<T> operator+(T aEnum) const
|
|
|
|
{
|
|
|
|
EnumSet<T> result(*this);
|
|
|
|
result += aEnum;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Union
|
|
|
|
*/
|
|
|
|
void operator+=(const EnumSet<T> aEnumSet)
|
|
|
|
{
|
2016-04-28 16:25:05 +03:00
|
|
|
incVersion();
|
2014-06-13 10:34:08 +04:00
|
|
|
mBitField |= aEnumSet.mBitField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Union
|
|
|
|
*/
|
|
|
|
EnumSet<T> operator+(const EnumSet<T> aEnumSet) const
|
|
|
|
{
|
|
|
|
EnumSet<T> result(*this);
|
|
|
|
result += aEnumSet;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an element
|
|
|
|
*/
|
|
|
|
void operator-=(T aEnum)
|
|
|
|
{
|
2016-04-28 16:25:05 +03:00
|
|
|
incVersion();
|
2014-06-13 10:34:08 +04:00
|
|
|
mBitField &= ~(bitFor(aEnum));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove an element
|
|
|
|
*/
|
|
|
|
EnumSet<T> operator-(T aEnum) const
|
|
|
|
{
|
|
|
|
EnumSet<T> result(*this);
|
|
|
|
result -= aEnum;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a set of elements
|
|
|
|
*/
|
|
|
|
void operator-=(const EnumSet<T> aEnumSet)
|
|
|
|
{
|
2016-04-28 16:25:05 +03:00
|
|
|
incVersion();
|
2014-06-13 10:34:08 +04:00
|
|
|
mBitField &= ~(aEnumSet.mBitField);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove a set of elements
|
|
|
|
*/
|
|
|
|
EnumSet<T> operator-(const EnumSet<T> aEnumSet) const
|
|
|
|
{
|
|
|
|
EnumSet<T> result(*this);
|
|
|
|
result -= aEnumSet;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-10-30 18:28:53 +03:00
|
|
|
/**
|
|
|
|
* Clear
|
|
|
|
*/
|
|
|
|
void clear()
|
|
|
|
{
|
2016-04-28 16:25:05 +03:00
|
|
|
incVersion();
|
2015-10-30 18:28:53 +03:00
|
|
|
mBitField = 0;
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
/**
|
|
|
|
* Intersection
|
|
|
|
*/
|
|
|
|
void operator&=(const EnumSet<T> aEnumSet)
|
|
|
|
{
|
2016-04-28 16:25:05 +03:00
|
|
|
incVersion();
|
2014-06-13 10:34:08 +04:00
|
|
|
mBitField &= aEnumSet.mBitField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Intersection
|
|
|
|
*/
|
|
|
|
EnumSet<T> operator&(const EnumSet<T> aEnumSet) const
|
|
|
|
{
|
|
|
|
EnumSet<T> result(*this);
|
|
|
|
result &= aEnumSet;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Equality
|
|
|
|
*/
|
|
|
|
bool operator==(const EnumSet<T> aEnumSet) const
|
|
|
|
{
|
|
|
|
return mBitField == aEnumSet.mBitField;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test is an element is contained in the set.
|
|
|
|
*/
|
|
|
|
bool contains(T aEnum) const
|
|
|
|
{
|
|
|
|
return mBitField & bitFor(aEnum);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the number of elements in the set.
|
|
|
|
*/
|
2016-04-28 16:25:05 +03:00
|
|
|
uint8_t size() const
|
2014-06-13 10:34:08 +04:00
|
|
|
{
|
|
|
|
uint8_t count = 0;
|
|
|
|
for (uint32_t bitField = mBitField; bitField; bitField >>= 1) {
|
|
|
|
if (bitField & 1) {
|
|
|
|
count++;
|
2012-11-07 03:23:13 +04:00
|
|
|
}
|
|
|
|
}
|
2014-06-13 10:34:08 +04:00
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isEmpty() const
|
|
|
|
{
|
|
|
|
return mBitField == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t serialize() const
|
|
|
|
{
|
|
|
|
return mBitField;
|
|
|
|
}
|
|
|
|
|
|
|
|
void deserialize(uint32_t aValue)
|
|
|
|
{
|
2016-04-28 16:25:05 +03:00
|
|
|
incVersion();
|
2014-06-13 10:34:08 +04:00
|
|
|
mBitField = aValue;
|
|
|
|
}
|
|
|
|
|
2016-04-28 16:25:05 +03:00
|
|
|
class ConstIterator
|
|
|
|
{
|
|
|
|
const EnumSet<T>* mSet;
|
|
|
|
uint32_t mPos;
|
|
|
|
#ifdef DEBUG
|
|
|
|
uint64_t mVersion;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void checkVersion() {
|
|
|
|
// Check that the set has not been modified while being iterated.
|
|
|
|
MOZ_ASSERT_IF(mSet, mSet->mVersion == mVersion);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
ConstIterator(const EnumSet<T>& aSet, uint32_t aPos)
|
|
|
|
: mSet(&aSet), mPos(aPos)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
mVersion = mSet->mVersion;
|
|
|
|
#endif
|
|
|
|
MOZ_ASSERT(aPos <= kMaxBits);
|
|
|
|
if (aPos != kMaxBits && !mSet->contains(T(mPos)))
|
|
|
|
++*this;
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator(const ConstIterator& aOther)
|
|
|
|
: mSet(aOther.mSet), mPos(aOther.mPos)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
mVersion = aOther.mVersion;
|
|
|
|
checkVersion();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator(ConstIterator&& aOther)
|
|
|
|
: mSet(aOther.mSet), mPos(aOther.mPos)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
mVersion = aOther.mVersion;
|
|
|
|
checkVersion();
|
|
|
|
#endif
|
|
|
|
aOther.mSet = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
~ConstIterator() {
|
|
|
|
checkVersion();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const ConstIterator& other) {
|
|
|
|
MOZ_ASSERT(mSet == other.mSet);
|
|
|
|
checkVersion();
|
|
|
|
return mPos == other.mPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const ConstIterator& other) {
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
T operator*() {
|
|
|
|
MOZ_ASSERT(mSet);
|
|
|
|
MOZ_ASSERT(mPos < kMaxBits);
|
|
|
|
MOZ_ASSERT(mSet->contains(T(mPos)));
|
|
|
|
checkVersion();
|
|
|
|
return T(mPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator& operator++() {
|
|
|
|
MOZ_ASSERT(mSet);
|
|
|
|
MOZ_ASSERT(mPos < kMaxBits);
|
|
|
|
checkVersion();
|
|
|
|
do {
|
|
|
|
mPos++;
|
|
|
|
} while (mPos < kMaxBits && !mSet->contains(T(mPos)));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
ConstIterator begin() const {
|
|
|
|
return ConstIterator(*this, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
ConstIterator end() const {
|
|
|
|
return ConstIterator(*this, kMaxBits);
|
|
|
|
}
|
|
|
|
|
2014-06-13 10:34:08 +04:00
|
|
|
private:
|
|
|
|
static uint32_t bitFor(T aEnum)
|
|
|
|
{
|
|
|
|
uint32_t bitNumber = (uint32_t)aEnum;
|
2016-04-28 16:25:05 +03:00
|
|
|
MOZ_ASSERT(bitNumber < kMaxBits);
|
2014-06-13 10:34:08 +04:00
|
|
|
return 1U << bitNumber;
|
|
|
|
}
|
|
|
|
|
2016-04-28 16:25:05 +03:00
|
|
|
void initVersion() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
mVersion = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void incVersion() {
|
|
|
|
#ifdef DEBUG
|
|
|
|
mVersion++;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static const size_t kMaxBits = 32;
|
2014-06-13 10:34:08 +04:00
|
|
|
uint32_t mBitField;
|
2016-04-28 16:25:05 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
uint64_t mVersion;
|
|
|
|
#endif
|
2012-11-07 03:23:13 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-24 11:41:39 +04:00
|
|
|
#endif /* mozilla_EnumSet_h_*/
|