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_AnimationTarget_h
8 #define mozilla_AnimationTarget_h
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/RefPtr.h"
15 #include "nsCSSPseudoElements.h"
23 struct OwningAnimationTarget
{
24 OwningAnimationTarget() = default;
26 OwningAnimationTarget(dom::Element
* aElement
, PseudoStyleType aType
)
27 : mElement(aElement
), mPseudoType(aType
) {}
29 explicit OwningAnimationTarget(dom::Element
* aElement
) : mElement(aElement
) {}
31 bool operator==(const OwningAnimationTarget
& aOther
) const {
32 return mElement
== aOther
.mElement
&& mPseudoType
== aOther
.mPseudoType
;
35 explicit operator bool() const { return !!mElement
; }
37 // mElement represents the parent element of a pseudo-element, not the
38 // generated content element.
39 RefPtr
<dom::Element
> mElement
;
40 PseudoStyleType mPseudoType
= PseudoStyleType::NotPseudo
;
43 struct NonOwningAnimationTarget
{
44 NonOwningAnimationTarget() = default;
46 NonOwningAnimationTarget(dom::Element
* aElement
, PseudoStyleType aType
)
47 : mElement(aElement
), mPseudoType(aType
) {}
49 explicit NonOwningAnimationTarget(const OwningAnimationTarget
& aOther
)
50 : mElement(aOther
.mElement
), mPseudoType(aOther
.mPseudoType
) {}
52 bool operator==(const NonOwningAnimationTarget
& aOther
) const {
53 return mElement
== aOther
.mElement
&& mPseudoType
== aOther
.mPseudoType
;
56 NonOwningAnimationTarget
& operator=(const OwningAnimationTarget
& aOther
) {
57 mElement
= aOther
.mElement
;
58 mPseudoType
= aOther
.mPseudoType
;
62 explicit operator bool() const { return !!mElement
; }
64 // mElement represents the parent element of a pseudo-element, not the
65 // generated content element.
66 dom::Element
* MOZ_NON_OWNING_REF mElement
= nullptr;
67 PseudoStyleType mPseudoType
= PseudoStyleType::NotPseudo
;
70 // Helper functions for cycle-collecting Maybe<OwningAnimationTarget>
71 inline void ImplCycleCollectionTraverse(
72 nsCycleCollectionTraversalCallback
& aCallback
,
73 Maybe
<OwningAnimationTarget
>& aTarget
, const char* aName
,
74 uint32_t aFlags
= 0) {
76 ImplCycleCollectionTraverse(aCallback
, aTarget
->mElement
, aName
, aFlags
);
80 inline void ImplCycleCollectionUnlink(Maybe
<OwningAnimationTarget
>& aTarget
) {
82 ImplCycleCollectionUnlink(aTarget
->mElement
);
86 // A DefaultHasher specialization for OwningAnimationTarget.
88 struct DefaultHasher
<OwningAnimationTarget
> {
89 using Key
= OwningAnimationTarget
;
90 using Lookup
= OwningAnimationTarget
;
91 using PtrHasher
= PointerHasher
<dom::Element
*>;
93 static HashNumber
hash(const Lookup
& aLookup
) {
94 return AddToHash(PtrHasher::hash(aLookup
.mElement
.get()),
95 static_cast<uint8_t>(aLookup
.mPseudoType
));
98 static bool match(const Key
& aKey
, const Lookup
& aLookup
) {
99 return PtrHasher::match(aKey
.mElement
.get(), aLookup
.mElement
.get()) &&
100 aKey
.mPseudoType
== aLookup
.mPseudoType
;
103 static void rekey(Key
& aKey
, Key
&& aNewKey
) { aKey
= std::move(aNewKey
); }
106 } // namespace mozilla
108 #endif // mozilla_AnimationTarget_h