Bug 1874684 - Part 37: Fix unified compilation. r=allstarschh
[gecko.git] / layout / style / nsCSSPropertyIDSet.h
blob18488815eea943a2705e371069411432da23afd9
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 /* bit vectors for sets of CSS properties */
7 #ifndef nsCSSPropertyIDSet_h__
8 #define nsCSSPropertyIDSet_h__
10 #include <initializer_list>
11 #include <limits.h> // for CHAR_BIT
12 #include <ostream>
14 #include "mozilla/ArrayUtils.h"
15 // For COMPOSITOR_ANIMATABLE_PROPERTY_LIST and
16 // COMPOSITOR_ANIMATABLE_PROPERTY_LIST_LENGTH
17 #include "mozilla/AnimatedPropertyID.h"
18 #include "mozilla/CompositorAnimatableProperties.h"
19 #include "nsCSSProps.h" // For operator<< for nsCSSPropertyID
20 #include "nsCSSPropertyID.h"
22 /**
23 * nsCSSPropertyIDSet maintains a set of non-shorthand CSS properties. In
24 * other words, for each longhand CSS property we support, it has a bit
25 * for whether that property is in the set.
27 class nsCSSPropertyIDSet {
28 public:
29 constexpr nsCSSPropertyIDSet() : mProperties{0} {}
30 // auto-generated copy-constructor OK
32 explicit constexpr nsCSSPropertyIDSet(
33 std::initializer_list<nsCSSPropertyID> aProperties)
34 : mProperties{0} {
35 for (auto property : aProperties) {
36 size_t p = property;
37 mProperties[p / kBitsInChunk] |= property_set_type(1)
38 << (p % kBitsInChunk);
42 void AssertInSetRange(nsCSSPropertyID aProperty) const {
43 MOZ_DIAGNOSTIC_ASSERT(
44 0 <= aProperty && aProperty < eCSSProperty_COUNT_no_shorthands,
45 "out of bounds");
48 // Conversion of aProperty to |size_t| after AssertInSetRange
49 // lets the compiler generate significantly tighter code.
51 void AddProperty(nsCSSPropertyID aProperty) {
52 AssertInSetRange(aProperty);
53 size_t p = aProperty;
54 mProperties[p / kBitsInChunk] |= property_set_type(1) << (p % kBitsInChunk);
57 void RemoveProperty(nsCSSPropertyID aProperty) {
58 AssertInSetRange(aProperty);
59 size_t p = aProperty;
60 mProperties[p / kBitsInChunk] &=
61 ~(property_set_type(1) << (p % kBitsInChunk));
64 bool HasProperty(const mozilla::AnimatedPropertyID& aProperty) const {
65 return !aProperty.IsCustom() && HasProperty(aProperty.mID);
68 bool HasProperty(nsCSSPropertyID aProperty) const {
69 AssertInSetRange(aProperty);
70 size_t p = aProperty;
71 return (mProperties[p / kBitsInChunk] &
72 (property_set_type(1) << (p % kBitsInChunk))) != 0;
75 // Returns an nsCSSPropertyIDSet including all properties that can be run
76 // on the compositor.
77 static constexpr nsCSSPropertyIDSet CompositorAnimatables() {
78 return nsCSSPropertyIDSet(COMPOSITOR_ANIMATABLE_PROPERTY_LIST);
81 static constexpr size_t CompositorAnimatableCount() {
82 return COMPOSITOR_ANIMATABLE_PROPERTY_LIST_LENGTH;
85 static constexpr size_t CompositorAnimatableDisplayItemCount() {
86 // We have 3 individual transforms and 5 motion path properties, and they
87 // also use DisplayItemType::TYPE_TRANSFORM.
88 return COMPOSITOR_ANIMATABLE_PROPERTY_LIST_LENGTH - 8;
91 static constexpr nsCSSPropertyIDSet CSSTransformProperties() {
92 return nsCSSPropertyIDSet{eCSSProperty_transform, eCSSProperty_translate,
93 eCSSProperty_rotate, eCSSProperty_scale};
96 static constexpr nsCSSPropertyIDSet MotionPathProperties() {
97 return nsCSSPropertyIDSet{
98 eCSSProperty_offset_path, eCSSProperty_offset_distance,
99 eCSSProperty_offset_rotate, eCSSProperty_offset_anchor,
100 eCSSProperty_offset_position};
103 static constexpr nsCSSPropertyIDSet TransformLikeProperties() {
104 return nsCSSPropertyIDSet{
105 eCSSProperty_transform, eCSSProperty_translate,
106 eCSSProperty_rotate, eCSSProperty_scale,
107 eCSSProperty_offset_path, eCSSProperty_offset_distance,
108 eCSSProperty_offset_rotate, eCSSProperty_offset_anchor,
109 eCSSProperty_offset_position};
112 static constexpr nsCSSPropertyIDSet OpacityProperties() {
113 return nsCSSPropertyIDSet{eCSSProperty_opacity};
116 bool Intersects(const nsCSSPropertyIDSet& aOther) const {
117 for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
118 if (mProperties[i] & aOther.mProperties[i]) {
119 return true;
122 return false;
125 void Empty() { memset(mProperties, 0, sizeof(mProperties)); }
127 void AssertIsEmpty(const char* aText) const {
128 for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
129 NS_ASSERTION(mProperties[i] == 0, aText);
133 bool Equals(const nsCSSPropertyIDSet& aOther) const {
134 return mozilla::ArrayEqual(mProperties, aOther.mProperties);
137 bool IsEmpty() const {
138 for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
139 if (mProperties[i] != 0) {
140 return false;
143 return true;
146 bool IsSubsetOf(const nsCSSPropertyIDSet& aOther) const {
147 return this->Intersect(aOther).Equals(*this);
150 // Return a new nsCSSPropertyIDSet which is the inverse of this set.
151 nsCSSPropertyIDSet Inverse() const {
152 nsCSSPropertyIDSet result;
153 for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
154 result.mProperties[i] = ~mProperties[i];
156 return result;
159 // Returns a new nsCSSPropertyIDSet with all properties that are both in
160 // this set and |aOther|.
161 nsCSSPropertyIDSet Intersect(const nsCSSPropertyIDSet& aOther) const {
162 nsCSSPropertyIDSet result;
163 for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
164 result.mProperties[i] = mProperties[i] & aOther.mProperties[i];
166 return result;
169 // Return a new nsCSSPropertyIDSet with all properties that are in either
170 // this set or |aOther| but not both.
171 nsCSSPropertyIDSet Xor(const nsCSSPropertyIDSet& aOther) const {
172 nsCSSPropertyIDSet result;
173 for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
174 result.mProperties[i] = mProperties[i] ^ aOther.mProperties[i];
176 return result;
179 nsCSSPropertyIDSet& operator|=(const nsCSSPropertyIDSet& aOther) {
180 for (size_t i = 0; i < mozilla::ArrayLength(mProperties); ++i) {
181 mProperties[i] |= aOther.mProperties[i];
183 return *this;
186 private:
187 typedef unsigned long property_set_type;
189 public:
190 // number of bits in |property_set_type|.
191 static const size_t kBitsInChunk = sizeof(property_set_type) * CHAR_BIT;
192 // number of |property_set_type|s in the set
193 static const size_t kChunkCount =
194 (eCSSProperty_COUNT_no_shorthands + kBitsInChunk - 1) / kBitsInChunk;
197 * For fast enumeration of all the bits that are set, callers can
198 * check each chunk against zero (since in normal cases few bits are
199 * likely to be set).
201 bool HasPropertyInChunk(size_t aChunk) const {
202 return mProperties[aChunk] != 0;
204 bool HasPropertyAt(size_t aChunk, size_t aBit) const {
205 return (mProperties[aChunk] & (property_set_type(1) << aBit)) != 0;
207 static nsCSSPropertyID CSSPropertyAt(size_t aChunk, size_t aBit) {
208 return nsCSSPropertyID(aChunk * kBitsInChunk + aBit);
211 // Iterator for use in range-based for loops
212 class Iterator {
213 public:
214 Iterator(Iterator&& aOther)
215 : mPropertySet(aOther.mPropertySet),
216 mChunk(aOther.mChunk),
217 mBit(aOther.mBit) {}
219 static Iterator BeginIterator(const nsCSSPropertyIDSet& aPropertySet) {
220 Iterator result(aPropertySet);
222 // Search for the first property.
223 // Unsigned integer overflow is defined so the following is safe.
224 result.mBit = -1;
225 ++result;
227 return result;
230 static Iterator EndIterator(const nsCSSPropertyIDSet& aPropertySet) {
231 Iterator result(aPropertySet);
232 result.mChunk = kChunkCount;
233 result.mBit = 0;
234 return result;
237 bool operator!=(const Iterator& aOther) const {
238 return mChunk != aOther.mChunk || mBit != aOther.mBit;
241 Iterator& operator++() {
242 MOZ_ASSERT(mChunk < kChunkCount, "Should not iterate beyond end");
244 do {
245 mBit++;
246 } while (mBit < kBitsInChunk &&
247 !mPropertySet.HasPropertyAt(mChunk, mBit));
248 if (mBit != kBitsInChunk) {
249 return *this;
252 do {
253 mChunk++;
254 } while (mChunk < kChunkCount &&
255 !mPropertySet.HasPropertyInChunk(mChunk));
256 mBit = 0;
257 if (mChunk != kChunkCount) {
258 while (mBit < kBitsInChunk &&
259 !mPropertySet.HasPropertyAt(mChunk, mBit)) {
260 mBit++;
264 return *this;
267 nsCSSPropertyID operator*() {
268 MOZ_ASSERT(mChunk < kChunkCount, "Should not dereference beyond end");
269 return nsCSSPropertyIDSet::CSSPropertyAt(mChunk, mBit);
272 private:
273 explicit Iterator(const nsCSSPropertyIDSet& aPropertySet)
274 : mPropertySet(aPropertySet) {}
276 Iterator() = delete;
277 Iterator(const Iterator&) = delete;
278 Iterator& operator=(const Iterator&) = delete;
279 Iterator& operator=(const Iterator&&) = delete;
281 const nsCSSPropertyIDSet& mPropertySet;
282 size_t mChunk = 0;
283 size_t mBit = 0;
286 Iterator begin() const { return Iterator::BeginIterator(*this); }
287 Iterator end() const { return Iterator::EndIterator(*this); }
289 private:
290 property_set_type mProperties[kChunkCount];
293 // MOZ_DBG support
295 inline std::ostream& operator<<(std::ostream& aOut,
296 const nsCSSPropertyIDSet& aPropertySet) {
297 AutoTArray<nsCSSPropertyID, 16> properties;
298 for (nsCSSPropertyID property : aPropertySet) {
299 properties.AppendElement(property);
301 return aOut << properties;
304 #endif /* !defined(nsCSSPropertyIDSet_h__) */