AnimationTarget.h (4073B)
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/. */ 6 7 #ifndef mozilla_AnimationTarget_h 8 #define mozilla_AnimationTarget_h 9 10 #include "mozilla/Attributes.h" // For MOZ_NON_OWNING_REF 11 #include "mozilla/HashFunctions.h" // For HashNumber, AddToHash 12 #include "mozilla/HashTable.h" // For DefaultHasher, PointerHasher 13 #include "mozilla/Maybe.h" 14 #include "mozilla/PseudoStyleType.h" // For PseudoStyleRequest 15 #include "mozilla/RefPtr.h" 16 17 class nsAtom; 18 19 namespace mozilla { 20 21 namespace dom { 22 class Element; 23 } // namespace dom 24 25 struct OwningAnimationTarget { 26 OwningAnimationTarget() = default; 27 OwningAnimationTarget(dom::Element* aElement, 28 const PseudoStyleRequest& aRequest) 29 : mElement(aElement), mPseudoRequest(aRequest) {} 30 31 explicit OwningAnimationTarget(dom::Element* aElement) : mElement(aElement) {} 32 33 bool operator==(const OwningAnimationTarget& aOther) const { 34 return mElement == aOther.mElement && 35 mPseudoRequest == aOther.mPseudoRequest; 36 } 37 38 explicit operator bool() const { return !!mElement; } 39 40 // mElement represents the parent element of a pseudo-element, not the 41 // generated content element. 42 RefPtr<dom::Element> mElement; 43 PseudoStyleRequest mPseudoRequest; 44 }; 45 46 struct NonOwningAnimationTarget { 47 NonOwningAnimationTarget() = default; 48 NonOwningAnimationTarget(dom::Element* aElement, 49 const PseudoStyleRequest& aRequest) 50 : mElement(aElement), mPseudoRequest(aRequest) {} 51 52 explicit NonOwningAnimationTarget(const OwningAnimationTarget& aOther) 53 : mElement(aOther.mElement), mPseudoRequest(aOther.mPseudoRequest) {} 54 55 bool operator==(const NonOwningAnimationTarget& aOther) const { 56 return mElement == aOther.mElement && 57 mPseudoRequest == aOther.mPseudoRequest; 58 } 59 60 NonOwningAnimationTarget& operator=(const OwningAnimationTarget& aOther) { 61 mElement = aOther.mElement; 62 mPseudoRequest = aOther.mPseudoRequest; 63 return *this; 64 } 65 66 explicit operator bool() const { return !!mElement; } 67 68 // mElement represents the parent element of a pseudo-element, not the 69 // generated content element. 70 dom::Element* MOZ_NON_OWNING_REF mElement = nullptr; 71 PseudoStyleRequest mPseudoRequest; 72 }; 73 74 // Helper functions for cycle-collecting Maybe<OwningAnimationTarget> 75 inline void ImplCycleCollectionTraverse( 76 nsCycleCollectionTraversalCallback& aCallback, 77 Maybe<OwningAnimationTarget>& aTarget, const char* aName, 78 uint32_t aFlags = 0) { 79 if (aTarget) { 80 ImplCycleCollectionTraverse(aCallback, aTarget->mElement, aName, aFlags); 81 } 82 } 83 84 inline void ImplCycleCollectionUnlink(Maybe<OwningAnimationTarget>& aTarget) { 85 if (aTarget) { 86 ImplCycleCollectionUnlink(aTarget->mElement); 87 } 88 } 89 90 // A DefaultHasher specialization for OwningAnimationTarget. 91 template <> 92 struct DefaultHasher<OwningAnimationTarget> { 93 using Key = OwningAnimationTarget; 94 using Lookup = OwningAnimationTarget; 95 using PtrHasher = PointerHasher<dom::Element*>; 96 using AtomPtrHasher = DefaultHasher<nsAtom*>; 97 98 static HashNumber hash(const Lookup& aLookup) { 99 return AddToHash( 100 PtrHasher::hash(aLookup.mElement.get()), 101 static_cast<uint8_t>(aLookup.mPseudoRequest.mType), 102 AtomPtrHasher::hash(aLookup.mPseudoRequest.mIdentifier.get())); 103 } 104 105 static bool match(const Key& aKey, const Lookup& aLookup) { 106 return PtrHasher::match(aKey.mElement.get(), aLookup.mElement.get()) && 107 aKey.mPseudoRequest.mType == aLookup.mPseudoRequest.mType && 108 AtomPtrHasher::match(aKey.mPseudoRequest.mIdentifier.get(), 109 aLookup.mPseudoRequest.mIdentifier.get()); 110 } 111 112 static void rekey(Key& aKey, Key&& aNewKey) { aKey = std::move(aNewKey); } 113 }; 114 115 } // namespace mozilla 116 117 #endif // mozilla_AnimationTarget_h