Bug 1874684 - Part 4: Prefer const references instead of copying Instant values....
[gecko.git] / dom / svg / SVGLengthSMILType.cpp
blob45650a602e57f50d77a982faaea684b00e3c332a
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 #include "SVGLengthSMILType.h"
9 #include "mozilla/SMILValue.h"
10 #include "SVGAnimatedLengthList.h"
11 #include "nsDebug.h"
12 #include <math.h>
14 namespace mozilla {
16 using namespace dom::SVGLength_Binding;
18 void SVGLengthSMILType::Init(SMILValue& aValue) const {
19 MOZ_ASSERT(aValue.IsNull(), "Unexpected value type");
21 aValue.mU.mPtr = new SVGLengthAndInfo();
22 aValue.mType = this;
25 void SVGLengthSMILType::Destroy(SMILValue& aValue) const {
26 MOZ_ASSERT(aValue.mType == this, "Unexpected SMIL value");
27 delete static_cast<SVGLengthAndInfo*>(aValue.mU.mPtr);
28 aValue.mU.mPtr = nullptr;
29 aValue.mType = SMILNullType::Singleton();
32 nsresult SVGLengthSMILType::Assign(SMILValue& aDest,
33 const SMILValue& aSrc) const {
34 MOZ_ASSERT(aDest.mType == aSrc.mType, "Incompatible SMIL types");
35 MOZ_ASSERT(aDest.mType == this, "Unexpected SMIL value");
37 auto* src = static_cast<const SVGLengthAndInfo*>(aSrc.mU.mPtr);
38 auto* dest = static_cast<SVGLengthAndInfo*>(aDest.mU.mPtr);
39 dest->CopyFrom(*src);
41 return NS_OK;
44 bool SVGLengthSMILType::IsEqual(const SMILValue& aLeft,
45 const SMILValue& aRight) const {
46 MOZ_ASSERT(aLeft.mType == aRight.mType, "Incompatible SMIL types");
47 MOZ_ASSERT(aLeft.mType == this, "Unexpected type for SMIL value");
49 return *static_cast<const SVGLengthAndInfo*>(aLeft.mU.mPtr) ==
50 *static_cast<const SVGLengthAndInfo*>(aRight.mU.mPtr);
53 nsresult SVGLengthSMILType::Add(SMILValue& aDest, const SMILValue& aValueToAdd,
54 uint32_t aCount) const {
55 MOZ_ASSERT(aValueToAdd.mType == aDest.mType, "Trying to add invalid types");
56 MOZ_ASSERT(aValueToAdd.mType == this, "Unexpected source type");
58 auto& dest = *static_cast<SVGLengthAndInfo*>(aDest.mU.mPtr);
59 const auto& valueToAdd =
60 *static_cast<const SVGLengthAndInfo*>(aValueToAdd.mU.mPtr);
62 dest.Add(valueToAdd, aCount);
63 return NS_OK;
66 nsresult SVGLengthSMILType::ComputeDistance(const SMILValue& aFrom,
67 const SMILValue& aTo,
68 double& aDistance) const {
69 MOZ_ASSERT(aFrom.mType == aTo.mType, "Trying to compare different types");
70 MOZ_ASSERT(aFrom.mType == this, "Unexpected source type");
72 const auto& from = *static_cast<SVGLengthAndInfo*>(aFrom.mU.mPtr);
73 const auto& to = *static_cast<const SVGLengthAndInfo*>(aTo.mU.mPtr);
75 MOZ_ASSERT(from.Element() == to.Element());
77 dom::SVGElementMetrics metrics(from.Element());
79 // Normalize both to pixels in case they're different units:
80 aDistance = fabs(to.ValueInPixels(metrics) - from.ValueInPixels(metrics));
82 return NS_OK;
85 nsresult SVGLengthSMILType::Interpolate(const SMILValue& aStartVal,
86 const SMILValue& aEndVal,
87 double aUnitDistance,
88 SMILValue& aResult) const {
89 MOZ_ASSERT(aStartVal.mType == aEndVal.mType,
90 "Trying to interpolate different types");
91 MOZ_ASSERT(aStartVal.mType == this, "Unexpected types for interpolation");
92 MOZ_ASSERT(aResult.mType == this, "Unexpected result type");
94 const auto& start = *static_cast<SVGLengthAndInfo*>(aStartVal.mU.mPtr);
95 const auto& end = *static_cast<const SVGLengthAndInfo*>(aEndVal.mU.mPtr);
96 auto& result = *static_cast<SVGLengthAndInfo*>(aResult.mU.mPtr);
98 SVGLengthAndInfo::Interpolate(start, end, aUnitDistance, result);
100 return NS_OK;
103 } // namespace mozilla