Bug 1225699 part 4 - Add iterator to EffectSet; r=heycam
authorBrian Birtles <birtles@gmail.com>
Thu, 26 Nov 2015 16:53:53 +0900
changeset 308434 cd3b7b2feb9d3d003267f93d67715298dcd61176
parent 308433 16e67512e6a6fd26e5f56bf461e5fda99493f36d
child 308435 4556e8a8fae20d1ef24872ebf52c81121163a7c0
push id5513
push userraliiev@mozilla.com
push dateMon, 25 Jan 2016 13:55:34 +0000
treeherdermozilla-beta@5ee97dd05b5c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersheycam
bugs1225699
milestone45.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1225699 part 4 - Add iterator to EffectSet; r=heycam
dom/animation/EffectSet.h
--- a/dom/animation/EffectSet.h
+++ b/dom/animation/EffectSet.h
@@ -50,16 +50,80 @@ public:
 
 private:
   // We store a raw (non-owning) pointer here because KeyframeEffectReadOnly
   // keeps a strong reference to the target element where this object is
   // stored. KeyframeEffectReadOnly is responsible for removing itself from
   // this set when it releases its reference to the target element.
   typedef nsTHashtable<nsPtrHashKey<dom::KeyframeEffectReadOnly>> EffectPtrSet;
 
+public:
+  // A simple iterator to support iterating over the effects in this object in
+  // range-based for loops.
+  //
+  // This allows us to avoid exposing mEffects directly and saves the
+  // caller from having to dereference hashtable iterators using
+  // the rather complicated: iter.Get()->GetKey().
+  class Iterator
+  {
+  public:
+    explicit Iterator(EffectPtrSet::Iterator&& aHashIterator)
+      : mHashIterator(mozilla::Move(aHashIterator))
+      , mIsEndIterator(false) { }
+    Iterator(Iterator&& aOther)
+      : mHashIterator(mozilla::Move(aOther.mHashIterator))
+      , mIsEndIterator(aOther.mIsEndIterator) { }
+
+    static Iterator EndIterator(EffectPtrSet::Iterator&& aHashIterator)
+    {
+      Iterator result(mozilla::Move(aHashIterator));
+      result.mIsEndIterator = true;
+      return result;
+    }
+
+    bool operator!=(const Iterator& aOther) const {
+      if (Done() || aOther.Done()) {
+        return Done() != aOther.Done();
+      }
+      return mHashIterator.Get() != aOther.mHashIterator.Get();
+    }
+
+    Iterator& operator++() {
+      MOZ_ASSERT(!Done());
+      mHashIterator.Next();
+      return *this;
+    }
+
+    dom::KeyframeEffectReadOnly* operator* ()
+    {
+      MOZ_ASSERT(!Done());
+      return mHashIterator.Get()->GetKey();
+    }
+
+  private:
+    Iterator() = delete;
+    Iterator(const Iterator&) = delete;
+    Iterator& operator=(const Iterator&) = delete;
+    Iterator& operator=(const Iterator&&) = delete;
+
+    bool Done() const {
+      return mIsEndIterator || mHashIterator.Done();
+    }
+
+    EffectPtrSet::Iterator mHashIterator;
+    bool mIsEndIterator;
+  };
+
+  Iterator begin() { return Iterator(mEffects.Iter()); }
+  Iterator end()
+  {
+    return Iterator::EndIterator(mEffects.Iter());
+  }
+
+private:
   static nsIAtom* GetEffectSetPropertyAtom(nsCSSPseudoElements::Type
                                              aPseudoType);
 
   EffectPtrSet mEffects;
 
 #ifdef DEBUG
   bool mCalledPropertyDtor;
 #endif