Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsSMILValue.h" |
michael@0 | 7 | #include "nsDebug.h" |
michael@0 | 8 | #include <string.h> |
michael@0 | 9 | |
michael@0 | 10 | //---------------------------------------------------------------------- |
michael@0 | 11 | // Public methods |
michael@0 | 12 | |
michael@0 | 13 | nsSMILValue::nsSMILValue(const nsISMILType* aType) |
michael@0 | 14 | : mType(nsSMILNullType::Singleton()) |
michael@0 | 15 | { |
michael@0 | 16 | if (!aType) { |
michael@0 | 17 | NS_ERROR("Trying to construct nsSMILValue with null mType pointer"); |
michael@0 | 18 | return; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | InitAndCheckPostcondition(aType); |
michael@0 | 22 | } |
michael@0 | 23 | |
michael@0 | 24 | nsSMILValue::nsSMILValue(const nsSMILValue& aVal) |
michael@0 | 25 | : mType(nsSMILNullType::Singleton()) |
michael@0 | 26 | { |
michael@0 | 27 | InitAndCheckPostcondition(aVal.mType); |
michael@0 | 28 | mType->Assign(*this, aVal); |
michael@0 | 29 | } |
michael@0 | 30 | |
michael@0 | 31 | const nsSMILValue& |
michael@0 | 32 | nsSMILValue::operator=(const nsSMILValue& aVal) |
michael@0 | 33 | { |
michael@0 | 34 | if (&aVal == this) |
michael@0 | 35 | return *this; |
michael@0 | 36 | |
michael@0 | 37 | if (mType != aVal.mType) { |
michael@0 | 38 | DestroyAndReinit(aVal.mType); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | mType->Assign(*this, aVal); |
michael@0 | 42 | |
michael@0 | 43 | return *this; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | bool |
michael@0 | 47 | nsSMILValue::operator==(const nsSMILValue& aVal) const |
michael@0 | 48 | { |
michael@0 | 49 | if (&aVal == this) |
michael@0 | 50 | return true; |
michael@0 | 51 | |
michael@0 | 52 | return mType == aVal.mType && mType->IsEqual(*this, aVal); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | void |
michael@0 | 56 | nsSMILValue::Swap(nsSMILValue& aOther) |
michael@0 | 57 | { |
michael@0 | 58 | nsSMILValue tmp; |
michael@0 | 59 | memcpy(&tmp, &aOther, sizeof(nsSMILValue)); // tmp = aOther |
michael@0 | 60 | memcpy(&aOther, this, sizeof(nsSMILValue)); // aOther = this |
michael@0 | 61 | memcpy(this, &tmp, sizeof(nsSMILValue)); // this = tmp |
michael@0 | 62 | |
michael@0 | 63 | // |tmp| is about to die -- we need to clear its mType, so that its |
michael@0 | 64 | // destructor doesn't muck with the data we just transferred out of it. |
michael@0 | 65 | tmp.mType = nsSMILNullType::Singleton(); |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | nsresult |
michael@0 | 69 | nsSMILValue::Add(const nsSMILValue& aValueToAdd, uint32_t aCount) |
michael@0 | 70 | { |
michael@0 | 71 | if (aValueToAdd.mType != mType) { |
michael@0 | 72 | NS_ERROR("Trying to add incompatible types"); |
michael@0 | 73 | return NS_ERROR_FAILURE; |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | return mType->Add(*this, aValueToAdd, aCount); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | nsresult |
michael@0 | 80 | nsSMILValue::SandwichAdd(const nsSMILValue& aValueToAdd) |
michael@0 | 81 | { |
michael@0 | 82 | if (aValueToAdd.mType != mType) { |
michael@0 | 83 | NS_ERROR("Trying to add incompatible types"); |
michael@0 | 84 | return NS_ERROR_FAILURE; |
michael@0 | 85 | } |
michael@0 | 86 | |
michael@0 | 87 | return mType->SandwichAdd(*this, aValueToAdd); |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | nsresult |
michael@0 | 91 | nsSMILValue::ComputeDistance(const nsSMILValue& aTo, double& aDistance) const |
michael@0 | 92 | { |
michael@0 | 93 | if (aTo.mType != mType) { |
michael@0 | 94 | NS_ERROR("Trying to calculate distance between incompatible types"); |
michael@0 | 95 | return NS_ERROR_FAILURE; |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | return mType->ComputeDistance(*this, aTo, aDistance); |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | nsresult |
michael@0 | 102 | nsSMILValue::Interpolate(const nsSMILValue& aEndVal, |
michael@0 | 103 | double aUnitDistance, |
michael@0 | 104 | nsSMILValue& aResult) const |
michael@0 | 105 | { |
michael@0 | 106 | if (aEndVal.mType != mType) { |
michael@0 | 107 | NS_ERROR("Trying to interpolate between incompatible types"); |
michael@0 | 108 | return NS_ERROR_FAILURE; |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | if (aResult.mType != mType) { |
michael@0 | 112 | // Outparam has wrong type |
michael@0 | 113 | aResult.DestroyAndReinit(mType); |
michael@0 | 114 | } |
michael@0 | 115 | |
michael@0 | 116 | return mType->Interpolate(*this, aEndVal, aUnitDistance, aResult); |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | //---------------------------------------------------------------------- |
michael@0 | 120 | // Helper methods |
michael@0 | 121 | |
michael@0 | 122 | // Wrappers for nsISMILType::Init & ::Destroy that verify their postconditions |
michael@0 | 123 | void |
michael@0 | 124 | nsSMILValue::InitAndCheckPostcondition(const nsISMILType* aNewType) |
michael@0 | 125 | { |
michael@0 | 126 | aNewType->Init(*this); |
michael@0 | 127 | NS_ABORT_IF_FALSE(mType == aNewType, |
michael@0 | 128 | "Post-condition of Init failed. nsSMILValue is invalid"); |
michael@0 | 129 | } |
michael@0 | 130 | |
michael@0 | 131 | void |
michael@0 | 132 | nsSMILValue::DestroyAndCheckPostcondition() |
michael@0 | 133 | { |
michael@0 | 134 | mType->Destroy(*this); |
michael@0 | 135 | NS_ABORT_IF_FALSE(IsNull(), "Post-condition of Destroy failed. " |
michael@0 | 136 | "nsSMILValue not null after destroying"); |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | void |
michael@0 | 140 | nsSMILValue::DestroyAndReinit(const nsISMILType* aNewType) |
michael@0 | 141 | { |
michael@0 | 142 | DestroyAndCheckPostcondition(); |
michael@0 | 143 | InitAndCheckPostcondition(aNewType); |
michael@0 | 144 | } |