File: AnimatedPropertyIDSet.h

package info (click to toggle)
firefox 141.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,550,588 kB
  • sloc: cpp: 7,426,506; javascript: 6,367,238; ansic: 3,707,351; python: 1,369,002; xml: 623,983; asm: 426,918; java: 184,324; sh: 64,488; makefile: 19,203; objc: 13,059; perl: 12,955; yacc: 4,583; cs: 3,846; pascal: 3,352; lex: 1,720; ruby: 1,071; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10
file content (226 lines) | stat: -rw-r--r-- 7,423 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */

#ifndef mozilla_AnimatedPropertyIDSet_h
#define mozilla_AnimatedPropertyIDSet_h

#include "mozilla/ServoBindings.h"

#include "AnimatedPropertyID.h"
#include "nsCSSPropertyIDSet.h"
#include "nsTHashSet.h"

namespace mozilla {

class AnimatedPropertyIDSet {
 public:
  using CustomNameSet = nsTHashSet<RefPtr<nsAtom>>;

  AnimatedPropertyIDSet() = default;
  AnimatedPropertyIDSet(AnimatedPropertyIDSet&& aOther) = default;
  AnimatedPropertyIDSet(nsCSSPropertyIDSet&& aIDs, CustomNameSet&& aNames)
      : mIDs(std::move(aIDs)), mCustomNames(std::move(aNames)) {}

  AnimatedPropertyIDSet& operator=(AnimatedPropertyIDSet&& aRhs) {
    MOZ_ASSERT(&aRhs != this);
    this->~AnimatedPropertyIDSet();
    new (this) AnimatedPropertyIDSet(std::move(aRhs));
    return *this;
  }

  void AddProperty(const AnimatedPropertyID& aProperty) {
    if (aProperty.IsCustom()) {
      mCustomNames.Insert(aProperty.mCustomName);
    } else {
      mIDs.AddProperty(aProperty.mID);
    }
  }

  void RemoveProperty(const AnimatedPropertyID& aProperty) {
    if (aProperty.IsCustom()) {
      mCustomNames.Remove(aProperty.mCustomName);
    } else {
      mIDs.RemoveProperty(aProperty.mID);
    }
  }

  bool HasProperty(const AnimatedPropertyID& aProperty) const {
    if (aProperty.IsCustom()) {
      return mCustomNames.Contains(aProperty.mCustomName);
    }
    return mIDs.HasProperty(aProperty.mID);
  }

  bool Intersects(const nsCSSPropertyIDSet& aIDs) const {
    return mIDs.Intersects(aIDs);
  }

  bool IsSubsetOf(const AnimatedPropertyIDSet& aOther) const {
    if (!mIDs.IsSubsetOf(aOther.mIDs) ||
        mCustomNames.Count() > aOther.mCustomNames.Count()) {
      return false;
    }
    for (const auto& name : mCustomNames) {
      if (!aOther.mCustomNames.Contains(name)) {
        return false;
      }
    }
    return true;
  }

  bool IsEmpty() const { return mIDs.IsEmpty() && mCustomNames.IsEmpty(); }

  void AddProperties(const AnimatedPropertyIDSet& aOther) {
    mIDs |= aOther.mIDs;
    for (const auto& name : aOther.mCustomNames) {
      mCustomNames.Insert(name);
    }
  }

  // Returns a new nsCSSPropertyIDSet with all properties that are both in this
  // set and |aOther|.
  nsCSSPropertyIDSet Intersect(const nsCSSPropertyIDSet& aOther) const {
    return mIDs.Intersect(aOther);
  }

  // Returns a new AnimatedPropertyIDSet with all properties that are both in
  // this set and |aOther|.
  AnimatedPropertyIDSet Intersect(const AnimatedPropertyIDSet& aOther) const {
    nsCSSPropertyIDSet ids = mIDs.Intersect(aOther.mIDs);
    CustomNameSet names;
    for (const auto& name : mCustomNames) {
      if (!aOther.mCustomNames.Contains(name)) {
        continue;
      }
      names.Insert(name);
    }
    return AnimatedPropertyIDSet(std::move(ids), std::move(names));
  }

  // Returns a new AnimatedPropertyIDSet with all properties that are in either
  // this set or |aOther| but not both.
  AnimatedPropertyIDSet Xor(const AnimatedPropertyIDSet& aOther) const {
    nsCSSPropertyIDSet ids = mIDs.Xor(aOther.mIDs);
    CustomNameSet names;
    for (const auto& name : mCustomNames) {
      if (aOther.mCustomNames.Contains(name)) {
        continue;
      }
      names.Insert(name);
    }

    for (const auto& name : aOther.mCustomNames) {
      if (mCustomNames.Contains(name)) {
        continue;
      }
      names.Insert(name);
    }
    return AnimatedPropertyIDSet(std::move(ids), std::move(names));
  }

  // Iterator for use in range-based for loops
  class Iterator {
   public:
    Iterator(Iterator&& aOther)
        : mPropertySet(aOther.mPropertySet),
          mIDIterator(std::move(aOther.mIDIterator)),
          mCustomNameIterator(std::move(aOther.mCustomNameIterator)),
          mPropertyID(eCSSProperty_UNKNOWN) {}
    Iterator() = delete;
    Iterator(const Iterator&) = delete;
    Iterator& operator=(const Iterator&) = delete;
    Iterator& operator=(const Iterator&&) = delete;

    static Iterator BeginIterator(const AnimatedPropertyIDSet& aPropertySet) {
      return Iterator(aPropertySet, aPropertySet.mIDs.begin(),
                      aPropertySet.mCustomNames.begin());
    }

    static Iterator EndIterator(const AnimatedPropertyIDSet& aPropertySet) {
      return Iterator(aPropertySet, aPropertySet.mIDs.end(),
                      aPropertySet.mCustomNames.end());
    }

    bool operator!=(const Iterator& aOther) const {
      return mIDIterator != aOther.mIDIterator ||
             mCustomNameIterator != aOther.mCustomNameIterator;
    }

    Iterator& operator++() {
      MOZ_ASSERT(mIDIterator != mPropertySet.mIDs.end() ||
                     mCustomNameIterator != mPropertySet.mCustomNames.end(),
                 "Should not iterate beyond end");
      if (mIDIterator != mPropertySet.mIDs.end()) {
        ++mIDIterator;
      } else {
        ++mCustomNameIterator;
      }
      return *this;
    }

    AnimatedPropertyID operator*() {
      if (mIDIterator != mPropertySet.mIDs.end()) {
        mPropertyID.mID = *mIDIterator;
        mPropertyID.mCustomName = nullptr;
      } else if (mCustomNameIterator != mPropertySet.mCustomNames.end()) {
        mPropertyID.mID = eCSSPropertyExtra_variable;
        mPropertyID.mCustomName = *mCustomNameIterator;
      } else {
        MOZ_ASSERT_UNREACHABLE("Should not dereference beyond end");
        mPropertyID.mID = eCSSProperty_UNKNOWN;
        mPropertyID.mCustomName = nullptr;
      }
      return mPropertyID;
    }

   private:
    Iterator(const AnimatedPropertyIDSet& aPropertySet,
             nsCSSPropertyIDSet::Iterator aIDIterator,
             CustomNameSet::const_iterator aCustomNameIterator)
        : mPropertySet(aPropertySet),
          mIDIterator(std::move(aIDIterator)),
          mCustomNameIterator(std::move(aCustomNameIterator)),
          mPropertyID(eCSSProperty_UNKNOWN) {}

    const AnimatedPropertyIDSet& mPropertySet;
    nsCSSPropertyIDSet::Iterator mIDIterator;
    CustomNameSet::const_iterator mCustomNameIterator;
    AnimatedPropertyID mPropertyID;
  };

  Iterator begin() const { return Iterator::BeginIterator(*this); }
  Iterator end() const { return Iterator::EndIterator(*this); }

 private:
  AnimatedPropertyIDSet(const AnimatedPropertyIDSet&) = delete;
  AnimatedPropertyIDSet& operator=(const AnimatedPropertyIDSet&) = delete;

  nsCSSPropertyIDSet mIDs;
  CustomNameSet mCustomNames;
};

// A wrapper to support the inversion of AnimatedPropertyIDSet.
//
// We are using this struct (for convenience) to check if we should skip the
// AnimatedPropertyIDs when composing an animation rule, on either
// CascadeLevel::Animations or CascadeLevel::Tranistions.
struct InvertibleAnimatedPropertyIDSet {
  const AnimatedPropertyIDSet* mSet = nullptr;
  bool mIsInverted = false;

  void Setup(const AnimatedPropertyIDSet* aSet, bool aIsInverted) {
    mSet = aSet;
    mIsInverted = aIsInverted;
  }

  bool HasProperty(const AnimatedPropertyID& aProperty) const {
    return mSet && mIsInverted != mSet->HasProperty(aProperty);
  }
};

}  // namespace mozilla

#endif  // mozilla_AnimatedPropertyIDSet_h