mfbt/EnumSet.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /* A set abstraction for enumeration values. */
michael@0 8
michael@0 9 #ifndef mozilla_EnumSet_h
michael@0 10 #define mozilla_EnumSet_h
michael@0 11
michael@0 12 #include "mozilla/Assertions.h"
michael@0 13
michael@0 14 #include <stdint.h>
michael@0 15
michael@0 16 namespace mozilla {
michael@0 17
michael@0 18 /**
michael@0 19 * EnumSet<T> is a set of values defined by an enumeration. It is implemented
michael@0 20 * using a 32 bit mask for each value so it will only work for enums with an int
michael@0 21 * representation less than 32. It works both for enum and enum class types.
michael@0 22 */
michael@0 23 template<typename T>
michael@0 24 class EnumSet
michael@0 25 {
michael@0 26 public:
michael@0 27 EnumSet()
michael@0 28 : mBitField(0)
michael@0 29 { }
michael@0 30
michael@0 31 EnumSet(T aEnum)
michael@0 32 : mBitField(aEnum)
michael@0 33 { }
michael@0 34
michael@0 35 EnumSet(T aEnum1, T aEnum2)
michael@0 36 : mBitField(bitFor(aEnum1) |
michael@0 37 bitFor(aEnum2))
michael@0 38 { }
michael@0 39
michael@0 40 EnumSet(T aEnum1, T aEnum2, T aEnum3)
michael@0 41 : mBitField(bitFor(aEnum1) |
michael@0 42 bitFor(aEnum2) |
michael@0 43 bitFor(aEnum3))
michael@0 44 { }
michael@0 45
michael@0 46 EnumSet(T aEnum1, T aEnum2, T aEnum3, T aEnum4)
michael@0 47 : mBitField(bitFor(aEnum1) |
michael@0 48 bitFor(aEnum2) |
michael@0 49 bitFor(aEnum3) |
michael@0 50 bitFor(aEnum4))
michael@0 51 { }
michael@0 52
michael@0 53 EnumSet(const EnumSet& aEnumSet)
michael@0 54 : mBitField(aEnumSet.mBitField)
michael@0 55 { }
michael@0 56
michael@0 57 /**
michael@0 58 * Add an element
michael@0 59 */
michael@0 60 void operator+=(T aEnum) {
michael@0 61 mBitField |= bitFor(aEnum);
michael@0 62 }
michael@0 63
michael@0 64 /**
michael@0 65 * Add an element
michael@0 66 */
michael@0 67 EnumSet<T> operator+(T aEnum) const {
michael@0 68 EnumSet<T> result(*this);
michael@0 69 result += aEnum;
michael@0 70 return result;
michael@0 71 }
michael@0 72
michael@0 73 /**
michael@0 74 * Union
michael@0 75 */
michael@0 76 void operator+=(const EnumSet<T> aEnumSet) {
michael@0 77 mBitField |= aEnumSet.mBitField;
michael@0 78 }
michael@0 79
michael@0 80 /**
michael@0 81 * Union
michael@0 82 */
michael@0 83 EnumSet<T> operator+(const EnumSet<T> aEnumSet) const {
michael@0 84 EnumSet<T> result(*this);
michael@0 85 result += aEnumSet;
michael@0 86 return result;
michael@0 87 }
michael@0 88
michael@0 89 /**
michael@0 90 * Remove an element
michael@0 91 */
michael@0 92 void operator-=(T aEnum) {
michael@0 93 mBitField &= ~(bitFor(aEnum));
michael@0 94 }
michael@0 95
michael@0 96 /**
michael@0 97 * Remove an element
michael@0 98 */
michael@0 99 EnumSet<T> operator-(T aEnum) const {
michael@0 100 EnumSet<T> result(*this);
michael@0 101 result -= aEnum;
michael@0 102 return result;
michael@0 103 }
michael@0 104
michael@0 105 /**
michael@0 106 * Remove a set of elements
michael@0 107 */
michael@0 108 void operator-=(const EnumSet<T> aEnumSet) {
michael@0 109 mBitField &= ~(aEnumSet.mBitField);
michael@0 110 }
michael@0 111
michael@0 112 /**
michael@0 113 * Remove a set of elements
michael@0 114 */
michael@0 115 EnumSet<T> operator-(const EnumSet<T> aEnumSet) const {
michael@0 116 EnumSet<T> result(*this);
michael@0 117 result -= aEnumSet;
michael@0 118 return result;
michael@0 119 }
michael@0 120
michael@0 121 /**
michael@0 122 * Intersection
michael@0 123 */
michael@0 124 void operator&=(const EnumSet<T> aEnumSet) {
michael@0 125 mBitField &= aEnumSet.mBitField;
michael@0 126 }
michael@0 127
michael@0 128 /**
michael@0 129 * Intersection
michael@0 130 */
michael@0 131 EnumSet<T> operator&(const EnumSet<T> aEnumSet) const {
michael@0 132 EnumSet<T> result(*this);
michael@0 133 result &= aEnumSet;
michael@0 134 return result;
michael@0 135 }
michael@0 136
michael@0 137 /**
michael@0 138 * Equality
michael@0 139 */
michael@0 140
michael@0 141 bool operator==(const EnumSet<T> aEnumSet) const {
michael@0 142 return mBitField == aEnumSet.mBitField;
michael@0 143 }
michael@0 144
michael@0 145 /**
michael@0 146 * Test is an element is contained in the set
michael@0 147 */
michael@0 148 bool contains(T aEnum) const {
michael@0 149 return mBitField & bitFor(aEnum);
michael@0 150 }
michael@0 151
michael@0 152 /**
michael@0 153 * Return the number of elements in the set
michael@0 154 */
michael@0 155
michael@0 156 uint8_t size() {
michael@0 157 uint8_t count = 0;
michael@0 158 for (uint32_t bitField = mBitField; bitField; bitField >>= 1) {
michael@0 159 if (bitField & 1)
michael@0 160 count++;
michael@0 161 }
michael@0 162 return count;
michael@0 163 }
michael@0 164
michael@0 165 private:
michael@0 166 static uint32_t bitFor(T aEnum) {
michael@0 167 uint32_t bitNumber(aEnum);
michael@0 168 MOZ_ASSERT(bitNumber < 32);
michael@0 169 return 1U << bitNumber;
michael@0 170 }
michael@0 171
michael@0 172 uint32_t mBitField;
michael@0 173 };
michael@0 174
michael@0 175 } // namespace mozilla
michael@0 176
michael@0 177 #endif /* mozilla_EnumSet_h_*/

mercurial