1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_AnimatedPropertyIDSet_h
8 #define mozilla_AnimatedPropertyIDSet_h
10 #include "mozilla/ServoBindings.h"
12 #include "AnimatedPropertyID.h"
13 #include "nsCSSPropertyIDSet.h"
14 #include "nsTHashSet.h"
18 class AnimatedPropertyIDSet
{
20 void AddProperty(const AnimatedPropertyID
& aProperty
) {
21 if (aProperty
.IsCustom()) {
22 mCustomNames
.Insert(aProperty
.mCustomName
);
24 mIDs
.AddProperty(aProperty
.mID
);
28 void RemoveProperty(const AnimatedPropertyID
& aProperty
) {
29 if (aProperty
.IsCustom()) {
30 mCustomNames
.Remove(aProperty
.mCustomName
);
32 mIDs
.RemoveProperty(aProperty
.mID
);
36 bool HasProperty(const AnimatedPropertyID
& aProperty
) const {
37 if (aProperty
.IsCustom()) {
38 return mCustomNames
.Contains(aProperty
.mCustomName
);
40 return mIDs
.HasProperty(aProperty
.mID
);
43 bool Intersects(const nsCSSPropertyIDSet
& aIDs
) const {
44 return mIDs
.Intersects(aIDs
);
47 bool IsSubsetOf(const AnimatedPropertyIDSet
& aOther
) const {
48 if (!mIDs
.IsSubsetOf(aOther
.mIDs
) ||
49 mCustomNames
.Count() > aOther
.mCustomNames
.Count()) {
52 for (const auto& name
: mCustomNames
) {
53 if (!aOther
.mCustomNames
.Contains(name
)) {
60 void AddProperties(const AnimatedPropertyIDSet
& aOther
) {
62 for (const auto& name
: aOther
.mCustomNames
) {
63 mCustomNames
.Insert(name
);
67 using CustomNameSet
= nsTHashSet
<RefPtr
<nsAtom
>>;
69 // Iterator for use in range-based for loops
72 Iterator(Iterator
&& aOther
)
73 : mPropertySet(aOther
.mPropertySet
),
74 mIDIterator(std::move(aOther
.mIDIterator
)),
75 mCustomNameIterator(std::move(aOther
.mCustomNameIterator
)),
76 mPropertyID(eCSSProperty_UNKNOWN
) {}
78 Iterator(const Iterator
&) = delete;
79 Iterator
& operator=(const Iterator
&) = delete;
80 Iterator
& operator=(const Iterator
&&) = delete;
82 static Iterator
BeginIterator(const AnimatedPropertyIDSet
& aPropertySet
) {
83 return Iterator(aPropertySet
, aPropertySet
.mIDs
.begin(),
84 aPropertySet
.mCustomNames
.begin());
87 static Iterator
EndIterator(const AnimatedPropertyIDSet
& aPropertySet
) {
88 return Iterator(aPropertySet
, aPropertySet
.mIDs
.end(),
89 aPropertySet
.mCustomNames
.end());
92 bool operator!=(const Iterator
& aOther
) const {
93 return mIDIterator
!= aOther
.mIDIterator
||
94 mCustomNameIterator
!= aOther
.mCustomNameIterator
;
97 Iterator
& operator++() {
98 MOZ_ASSERT(mIDIterator
!= mPropertySet
.mIDs
.end() ||
99 mCustomNameIterator
!= mPropertySet
.mCustomNames
.end(),
100 "Should not iterate beyond end");
101 if (mIDIterator
!= mPropertySet
.mIDs
.end()) {
104 ++mCustomNameIterator
;
109 AnimatedPropertyID
operator*() {
110 if (mIDIterator
!= mPropertySet
.mIDs
.end()) {
111 mPropertyID
.mID
= *mIDIterator
;
112 mPropertyID
.mCustomName
= nullptr;
113 } else if (mCustomNameIterator
!= mPropertySet
.mCustomNames
.end()) {
114 mPropertyID
.mID
= eCSSPropertyExtra_variable
;
115 mPropertyID
.mCustomName
= *mCustomNameIterator
;
117 MOZ_ASSERT_UNREACHABLE("Should not dereference beyond end");
118 mPropertyID
.mID
= eCSSProperty_UNKNOWN
;
119 mPropertyID
.mCustomName
= nullptr;
125 Iterator(const AnimatedPropertyIDSet
& aPropertySet
,
126 nsCSSPropertyIDSet::Iterator aIDIterator
,
127 CustomNameSet::const_iterator aCustomNameIterator
)
128 : mPropertySet(aPropertySet
),
129 mIDIterator(std::move(aIDIterator
)),
130 mCustomNameIterator(std::move(aCustomNameIterator
)),
131 mPropertyID(eCSSProperty_UNKNOWN
) {}
133 const AnimatedPropertyIDSet
& mPropertySet
;
134 nsCSSPropertyIDSet::Iterator mIDIterator
;
135 CustomNameSet::const_iterator mCustomNameIterator
;
136 AnimatedPropertyID mPropertyID
;
139 Iterator
begin() const { return Iterator::BeginIterator(*this); }
140 Iterator
end() const { return Iterator::EndIterator(*this); }
143 nsCSSPropertyIDSet mIDs
;
144 CustomNameSet mCustomNames
;
147 } // namespace mozilla
149 #endif // mozilla_AnimatedPropertyIDSet_h