Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / svg / SVGAnimatedNumber.h
blob29ddfca618d910f5c9a78827a4bbffc7b67cf0b1
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 DOM_SVG_SVGANIMATEDNUMBER_H_
8 #define DOM_SVG_SVGANIMATEDNUMBER_H_
10 #include "nsCycleCollectionParticipant.h"
11 #include "nsError.h"
12 #include "nsMathUtils.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/FloatingPoint.h"
15 #include "mozilla/SMILAttr.h"
16 #include "mozilla/UniquePtr.h"
17 #include "mozilla/dom/DOMSVGAnimatedNumber.h"
18 #include "mozilla/dom/SVGElement.h"
20 namespace mozilla {
22 class SMILValue;
24 namespace dom {
25 class SVGAnimationElement;
26 } // namespace dom
28 class SVGAnimatedNumber {
29 public:
30 friend class AutoChangeNumberNotifier;
31 using SVGElement = dom::SVGElement;
33 void Init(uint8_t aAttrEnum = 0xff, float aValue = 0) {
34 mAnimVal = mBaseVal = aValue;
35 mAttrEnum = aAttrEnum;
36 mIsAnimated = false;
37 mIsBaseSet = false;
40 nsresult SetBaseValueString(const nsAString& aValue, SVGElement* aSVGElement);
41 void GetBaseValueString(nsAString& aValue);
43 void SetBaseValue(float aValue, SVGElement* aSVGElement);
44 float GetBaseValue() const { return mBaseVal; }
45 void SetAnimValue(float aValue, SVGElement* aSVGElement);
46 float GetAnimValue() const { return mAnimVal; }
48 // Returns true if the animated value of this number has been explicitly
49 // set (either by animation, or by taking on the base value which has been
50 // explicitly set by markup or a DOM call), false otherwise.
51 // If this returns false, the animated value is still valid, that is,
52 // usable, and represents the default base value of the attribute.
53 bool IsExplicitlySet() const { return mIsAnimated || mIsBaseSet; }
55 already_AddRefed<dom::DOMSVGAnimatedNumber> ToDOMAnimatedNumber(
56 SVGElement* aSVGElement);
57 UniquePtr<SMILAttr> ToSMILAttr(SVGElement* aSVGElement);
59 private:
60 float mAnimVal;
61 float mBaseVal;
62 uint8_t mAttrEnum; // element specified tracking for attribute
63 bool mIsAnimated;
64 bool mIsBaseSet;
66 public:
67 // DOM wrapper class for the (DOM)SVGAnimatedNumber interface where the
68 // wrapped class is SVGAnimatedNumber.
69 struct DOMAnimatedNumber final : public dom::DOMSVGAnimatedNumber {
70 DOMAnimatedNumber(SVGAnimatedNumber* aVal, SVGElement* aSVGElement)
71 : dom::DOMSVGAnimatedNumber(aSVGElement), mVal(aVal) {}
72 virtual ~DOMAnimatedNumber();
74 SVGAnimatedNumber* mVal; // kept alive because it belongs to content
76 float BaseVal() override { return mVal->GetBaseValue(); }
77 void SetBaseVal(float aValue) override {
78 MOZ_ASSERT(std::isfinite(aValue));
79 mVal->SetBaseValue(aValue, mSVGElement);
82 // Script may have modified animation parameters or timeline -- DOM getters
83 // need to flush any resample requests to reflect these modifications.
84 float AnimVal() override {
85 mSVGElement->FlushAnimations();
86 return mVal->GetAnimValue();
90 struct SMILNumber : public SMILAttr {
91 public:
92 SMILNumber(SVGAnimatedNumber* aVal, SVGElement* aSVGElement)
93 : mVal(aVal), mSVGElement(aSVGElement) {}
95 // These will stay alive because a SMILAttr only lives as long
96 // as the Compositing step, and DOM elements don't get a chance to
97 // die during that.
98 SVGAnimatedNumber* mVal;
99 SVGElement* mSVGElement;
101 // SMILAttr methods
102 virtual nsresult ValueFromString(
103 const nsAString& aStr, const dom::SVGAnimationElement* aSrcElement,
104 SMILValue& aValue, bool& aPreventCachingOfSandwich) const override;
105 SMILValue GetBaseValue() const override;
106 void ClearAnimValue() override;
107 nsresult SetAnimValue(const SMILValue& aValue) override;
111 } // namespace mozilla
113 #endif // DOM_SVG_SVGANIMATEDNUMBER_H_