SVGAnimatedNumberList.h (4487B)
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 DOM_SVG_SVGANIMATEDNUMBERLIST_H_ 8 #define DOM_SVG_SVGANIMATEDNUMBERLIST_H_ 9 10 #include "SVGNumberList.h" 11 #include "mozilla/SMILAttr.h" 12 #include "mozilla/UniquePtr.h" 13 14 namespace mozilla { 15 16 class SMILValue; 17 18 namespace dom { 19 class SVGAnimationElement; 20 class SVGElement; 21 } // namespace dom 22 23 /** 24 * Class SVGAnimatedNumberList 25 * 26 * This class is very different to the SVG DOM interface of the same name found 27 * in the SVG specification. This is a lightweight internal class - see 28 * DOMSVGAnimatedNumberList for the heavier DOM class that wraps instances of 29 * this class and implements the SVG specification's SVGAnimatedNumberList DOM 30 * interface. 31 * 32 * Except where noted otherwise, this class' methods take care of keeping the 33 * appropriate DOM wrappers in sync (see the comment in 34 * DOMSVGAnimatedNumberList::InternalBaseValListWillChangeTo) so that their 35 * consumers don't need to concern themselves with that. 36 */ 37 class SVGAnimatedNumberList { 38 // friends so that they can get write access to mBaseVal 39 friend class dom::DOMSVGNumber; 40 friend class dom::DOMSVGNumberList; 41 42 public: 43 SVGAnimatedNumberList() = default; 44 45 SVGAnimatedNumberList& operator=(const SVGAnimatedNumberList& aOther) { 46 mIsBaseSet = aOther.mIsBaseSet; 47 mBaseVal = aOther.mBaseVal; 48 if (aOther.mAnimVal) { 49 mAnimVal = MakeUnique<SVGNumberList>(*aOther.mAnimVal); 50 } 51 return *this; 52 } 53 54 /** 55 * Because it's so important that mBaseVal and its DOMSVGNumberList wrapper 56 * (if any) be kept in sync (see the comment in 57 * DOMSVGAnimatedNumberList::InternalBaseValListWillChangeTo), this method 58 * returns a const reference. Only our friend classes may get mutable 59 * references to mBaseVal. 60 */ 61 const SVGNumberList& GetBaseValue() const { return mBaseVal; } 62 63 nsresult SetBaseValueString(const nsAString& aValue); 64 65 void ClearBaseValue(uint32_t aAttrEnum); 66 67 const SVGNumberList& GetAnimValue() const { 68 return mAnimVal ? *mAnimVal : mBaseVal; 69 } 70 71 nsresult SetAnimValue(const SVGNumberList& aNewAnimValue, 72 dom::SVGElement* aElement, uint32_t aAttrEnum); 73 74 void ClearAnimValue(dom::SVGElement* aElement, uint32_t aAttrEnum); 75 76 // Returns true if the animated value of this list has been explicitly 77 // set (either by animation, or by taking on the base value which has been 78 // explicitly set by markup or a DOM call), false otherwise. 79 // If this returns false, the animated value is still valid, that is, 80 // usable, and represents the default base value of the attribute. 81 bool IsExplicitlySet() const { return !!mAnimVal || mIsBaseSet; } 82 83 bool IsAnimating() const { return !!mAnimVal; } 84 85 UniquePtr<SMILAttr> ToSMILAttr(dom::SVGElement* aSVGElement, 86 uint8_t aAttrEnum); 87 88 private: 89 // mAnimVal is a pointer to allow us to determine if we're being animated or 90 // not. Making it a non-pointer member and using mAnimVal.IsEmpty() to check 91 // if we're animating is not an option, since that would break animation *to* 92 // the empty string (<set to="">). 93 94 SVGNumberList mBaseVal; 95 UniquePtr<SVGNumberList> mAnimVal; 96 bool mIsBaseSet = false; 97 98 struct SMILAnimatedNumberList : public SMILAttr { 99 public: 100 SMILAnimatedNumberList(SVGAnimatedNumberList* aVal, 101 dom::SVGElement* aSVGElement, uint8_t aAttrEnum) 102 : mVal(aVal), mElement(aSVGElement), mAttrEnum(aAttrEnum) {} 103 104 // These will stay alive because a SMILAttr only lives as long 105 // as the Compositing step, and DOM elements don't get a chance to 106 // die during that. 107 SVGAnimatedNumberList* mVal; 108 dom::SVGElement* mElement; 109 uint8_t mAttrEnum; 110 111 // SMILAttr methods 112 nsresult ValueFromString(const nsAString& aStr, 113 const dom::SVGAnimationElement* aSrcElement, 114 SMILValue& aValue, 115 bool& aPreventCachingOfSandwich) const override; 116 SMILValue GetBaseValue() const override; 117 void ClearAnimValue() override; 118 nsresult SetAnimValue(const SMILValue& aValue) override; 119 }; 120 }; 121 122 } // namespace mozilla 123 124 #endif // DOM_SVG_SVGANIMATEDNUMBERLIST_H_