1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/smil/nsSMILValue.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,144 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsSMILValue.h" 1.10 +#include "nsDebug.h" 1.11 +#include <string.h> 1.12 + 1.13 +//---------------------------------------------------------------------- 1.14 +// Public methods 1.15 + 1.16 +nsSMILValue::nsSMILValue(const nsISMILType* aType) 1.17 + : mType(nsSMILNullType::Singleton()) 1.18 +{ 1.19 + if (!aType) { 1.20 + NS_ERROR("Trying to construct nsSMILValue with null mType pointer"); 1.21 + return; 1.22 + } 1.23 + 1.24 + InitAndCheckPostcondition(aType); 1.25 +} 1.26 + 1.27 +nsSMILValue::nsSMILValue(const nsSMILValue& aVal) 1.28 + : mType(nsSMILNullType::Singleton()) 1.29 +{ 1.30 + InitAndCheckPostcondition(aVal.mType); 1.31 + mType->Assign(*this, aVal); 1.32 +} 1.33 + 1.34 +const nsSMILValue& 1.35 +nsSMILValue::operator=(const nsSMILValue& aVal) 1.36 +{ 1.37 + if (&aVal == this) 1.38 + return *this; 1.39 + 1.40 + if (mType != aVal.mType) { 1.41 + DestroyAndReinit(aVal.mType); 1.42 + } 1.43 + 1.44 + mType->Assign(*this, aVal); 1.45 + 1.46 + return *this; 1.47 +} 1.48 + 1.49 +bool 1.50 +nsSMILValue::operator==(const nsSMILValue& aVal) const 1.51 +{ 1.52 + if (&aVal == this) 1.53 + return true; 1.54 + 1.55 + return mType == aVal.mType && mType->IsEqual(*this, aVal); 1.56 +} 1.57 + 1.58 +void 1.59 +nsSMILValue::Swap(nsSMILValue& aOther) 1.60 +{ 1.61 + nsSMILValue tmp; 1.62 + memcpy(&tmp, &aOther, sizeof(nsSMILValue)); // tmp = aOther 1.63 + memcpy(&aOther, this, sizeof(nsSMILValue)); // aOther = this 1.64 + memcpy(this, &tmp, sizeof(nsSMILValue)); // this = tmp 1.65 + 1.66 + // |tmp| is about to die -- we need to clear its mType, so that its 1.67 + // destructor doesn't muck with the data we just transferred out of it. 1.68 + tmp.mType = nsSMILNullType::Singleton(); 1.69 +} 1.70 + 1.71 +nsresult 1.72 +nsSMILValue::Add(const nsSMILValue& aValueToAdd, uint32_t aCount) 1.73 +{ 1.74 + if (aValueToAdd.mType != mType) { 1.75 + NS_ERROR("Trying to add incompatible types"); 1.76 + return NS_ERROR_FAILURE; 1.77 + } 1.78 + 1.79 + return mType->Add(*this, aValueToAdd, aCount); 1.80 +} 1.81 + 1.82 +nsresult 1.83 +nsSMILValue::SandwichAdd(const nsSMILValue& aValueToAdd) 1.84 +{ 1.85 + if (aValueToAdd.mType != mType) { 1.86 + NS_ERROR("Trying to add incompatible types"); 1.87 + return NS_ERROR_FAILURE; 1.88 + } 1.89 + 1.90 + return mType->SandwichAdd(*this, aValueToAdd); 1.91 +} 1.92 + 1.93 +nsresult 1.94 +nsSMILValue::ComputeDistance(const nsSMILValue& aTo, double& aDistance) const 1.95 +{ 1.96 + if (aTo.mType != mType) { 1.97 + NS_ERROR("Trying to calculate distance between incompatible types"); 1.98 + return NS_ERROR_FAILURE; 1.99 + } 1.100 + 1.101 + return mType->ComputeDistance(*this, aTo, aDistance); 1.102 +} 1.103 + 1.104 +nsresult 1.105 +nsSMILValue::Interpolate(const nsSMILValue& aEndVal, 1.106 + double aUnitDistance, 1.107 + nsSMILValue& aResult) const 1.108 +{ 1.109 + if (aEndVal.mType != mType) { 1.110 + NS_ERROR("Trying to interpolate between incompatible types"); 1.111 + return NS_ERROR_FAILURE; 1.112 + } 1.113 + 1.114 + if (aResult.mType != mType) { 1.115 + // Outparam has wrong type 1.116 + aResult.DestroyAndReinit(mType); 1.117 + } 1.118 + 1.119 + return mType->Interpolate(*this, aEndVal, aUnitDistance, aResult); 1.120 +} 1.121 + 1.122 +//---------------------------------------------------------------------- 1.123 +// Helper methods 1.124 + 1.125 +// Wrappers for nsISMILType::Init & ::Destroy that verify their postconditions 1.126 +void 1.127 +nsSMILValue::InitAndCheckPostcondition(const nsISMILType* aNewType) 1.128 +{ 1.129 + aNewType->Init(*this); 1.130 + NS_ABORT_IF_FALSE(mType == aNewType, 1.131 + "Post-condition of Init failed. nsSMILValue is invalid"); 1.132 +} 1.133 + 1.134 +void 1.135 +nsSMILValue::DestroyAndCheckPostcondition() 1.136 +{ 1.137 + mType->Destroy(*this); 1.138 + NS_ABORT_IF_FALSE(IsNull(), "Post-condition of Destroy failed. " 1.139 + "nsSMILValue not null after destroying"); 1.140 +} 1.141 + 1.142 +void 1.143 +nsSMILValue::DestroyAndReinit(const nsISMILType* aNewType) 1.144 +{ 1.145 + DestroyAndCheckPostcondition(); 1.146 + InitAndCheckPostcondition(aNewType); 1.147 +}