Bug 1420996 - EnumSet enhancement. r=botond,Waldo
☠☠ backed out by fe26ccb3ec3b ☠ ☠
authorDaniel Zielas <zielas.daniel@gmail.com>
Sun, 12 Aug 2018 15:05:24 +0200
changeset 482315 01ed229650de691ba2a555401d504fcb471e62fa
parent 482314 a8c76f3a18f9f6b57331aea619df59bc16552d24
child 482316 413bd039c1263f3b9cef04e19b538e8d4097b721
push id232
push userfmarier@mozilla.com
push dateWed, 05 Sep 2018 20:45:54 +0000
reviewersbotond, Waldo
bugs1420996
milestone63.0a1
Bug 1420996 - EnumSet enhancement. r=botond,Waldo
mfbt/EnumSet.h
--- a/mfbt/EnumSet.h
+++ b/mfbt/EnumSet.h
@@ -23,16 +23,17 @@ namespace mozilla {
  * 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
 {
 public:
   typedef uint32_t serializedType;
+  typedef T valueType;
 
   EnumSet()
     : mBitField(0)
   {
   }
 
   MOZ_IMPLICIT EnumSet(T aEnum)
     : mBitField(bitFor(aEnum))
@@ -62,16 +63,22 @@ public:
   MOZ_IMPLICIT EnumSet(std::initializer_list<T> list)
     : mBitField(0)
   {
     for (auto value : list) {
       (*this) += value;
     }
   }
 
+  void operator=(T aEnum)
+  {
+    incVersion();
+    mBitField = bitFor(aEnum);
+  }
+
   EnumSet(const EnumSet& aEnumSet)
     : mBitField(aEnumSet.mBitField)
   {
   }
 
   /**
    * Add an element
    */
@@ -89,26 +96,26 @@ public:
     EnumSet<T> result(*this);
     result += aEnum;
     return result;
   }
 
   /**
    * Union
    */
-  void operator+=(const EnumSet<T> aEnumSet)
+  void operator+=(const EnumSet<T>& aEnumSet)
   {
     incVersion();
     mBitField |= aEnumSet.mBitField;
   }
 
   /**
    * Union
    */
-  EnumSet<T> operator+(const EnumSet<T> aEnumSet) const
+  EnumSet<T> operator+(const EnumSet<T>& aEnumSet) const
   {
     EnumSet<T> result(*this);
     result += aEnumSet;
     return result;
   }
 
   /**
    * Remove an element
@@ -127,26 +134,26 @@ public:
     EnumSet<T> result(*this);
     result -= aEnum;
     return result;
   }
 
   /**
    * Remove a set of elements
    */
-  void operator-=(const EnumSet<T> aEnumSet)
+  void operator-=(const EnumSet<T>& aEnumSet)
   {
     incVersion();
     mBitField &= ~(aEnumSet.mBitField);
   }
 
   /**
    * Remove a set of elements
    */
-  EnumSet<T> operator-(const EnumSet<T> aEnumSet) const
+  EnumSet<T> operator-(const EnumSet<T>& aEnumSet) const
   {
     EnumSet<T> result(*this);
     result -= aEnumSet;
     return result;
   }
 
   /**
    * Clear
@@ -155,49 +162,81 @@ public:
   {
     incVersion();
     mBitField = 0;
   }
 
   /**
    * Intersection
    */
-  void operator&=(const EnumSet<T> aEnumSet)
+  void operator&=(const EnumSet<T>& aEnumSet)
   {
     incVersion();
     mBitField &= aEnumSet.mBitField;
   }
 
   /**
    * Intersection
    */
-  EnumSet<T> operator&(const EnumSet<T> aEnumSet) const
+  EnumSet<T> operator&(const EnumSet<T>& aEnumSet) const
   {
     EnumSet<T> result(*this);
     result &= aEnumSet;
     return result;
   }
 
   /**
    * Equality
    */
-  bool operator==(const EnumSet<T> aEnumSet) const
+  bool operator==(const EnumSet<T>& aEnumSet) const
   {
     return mBitField == aEnumSet.mBitField;
   }
 
   /**
-   * Test is an element is contained in the set.
+   * Equality
+   */
+  bool operator==(T aEnum) const
+  {
+    return mBitField == bitFor(aEnum);
+  }
+
+  /**
+   * Not equal
+   */
+  bool operator!=(const EnumSet<T>& aEnumSet) const
+  {
+    return !operator==(aEnumSet);
+  }
+
+  /**
+   * Not equal
+   */
+  bool operator!=(T aEnum) const
+  {
+    return !operator==(aEnum);
+  }
+
+  /**
+   * Test if an element is contained in the set.
    */
   bool contains(T aEnum) const
   {
     return mBitField & bitFor(aEnum);
   }
 
   /**
+   * Test if a set is contained in the set.
+   */
+  bool contains(const EnumSet<T>& aEnumSet) const
+  {
+    return (mBitField & aEnumSet.mBitField) == aEnumSet.mBitField;
+  }
+
+  /**
    * Return the number of elements in the set.
    */
   uint8_t size() const
   {
     uint8_t count = 0;
     for (uint32_t bitField = mBitField; bitField; bitField >>= 1) {
       if (bitField & 1) {
         count++;