From 6409e7828112ddf28ef7144b1a0a3af4180f3370 Mon Sep 17 00:00:00 2001 From: Daniel Holbert Date: Fri, 29 Jan 2010 12:18:50 -0800 Subject: [PATCH] Bug 542731, Patch A: Clean up nsSMILValue class. r=roc --- content/smil/nsSMILValue.cpp | 84 ++++++++++++++++++++++-------------- content/smil/nsSMILValue.h | 5 +++ 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/content/smil/nsSMILValue.cpp b/content/smil/nsSMILValue.cpp index e50da0cb4307..3266ccbd9b3d 100644 --- a/content/smil/nsSMILValue.cpp +++ b/content/smil/nsSMILValue.cpp @@ -39,26 +39,27 @@ #include "nsSMILValue.h" #include "nsDebug.h" -nsSMILValue::nsSMILValue(const nsISMILType* aType) -: mU(), - mType(&nsSMILNullType::sSingleton) -{ - if (!aType) return; +//---------------------------------------------------------------------- +// Public methods - nsresult rv = aType->Init(*this); - NS_POSTCONDITION(mType == aType || (NS_FAILED(rv) && IsNull()), - "Post-condition of Init failed. nsSMILValue is invalid"); +nsSMILValue::nsSMILValue(const nsISMILType* aType) + : mType(&nsSMILNullType::sSingleton) +{ + if (!aType) { + NS_ERROR("Trying to construct nsSMILValue with null mType pointer"); + return; + } + + InitAndCheckPostcondition(aType); } nsSMILValue::nsSMILValue(const nsSMILValue& aVal) -: - mU(), - mType(&nsSMILNullType::sSingleton) + : mType(&nsSMILNullType::sSingleton) { - nsresult rv = aVal.mType->Init(*this); - NS_POSTCONDITION(mType == aVal.mType || (NS_FAILED(rv) && IsNull()), - "Post-condition of Init failed. nsSMILValue is invalid"); - if (NS_FAILED(rv)) return; + nsresult rv = InitAndCheckPostcondition(aVal.mType); + if (NS_FAILED(rv)) + return; + mType->Assign(*this, aVal); } @@ -69,12 +70,9 @@ nsSMILValue::operator=(const nsSMILValue& aVal) return *this; if (mType != aVal.mType) { - mType->Destroy(*this); - NS_POSTCONDITION(IsNull(), "nsSMILValue not null after destroying"); - nsresult rv = aVal.mType->Init(*this); - NS_POSTCONDITION(mType == aVal.mType || (NS_FAILED(rv) && IsNull()), - "Post-condition of Init failed. nsSMILValue is invalid"); - if (NS_FAILED(rv)) return *this; + nsresult rv = DestroyAndReinit(aVal.mType); + if (NS_FAILED(rv)) + return *this; // Initialization failed; return early } mType->Assign(*this, aVal); @@ -85,8 +83,6 @@ nsSMILValue::operator=(const nsSMILValue& aVal) nsresult nsSMILValue::Add(const nsSMILValue& aValueToAdd, PRUint32 aCount) { - if (aValueToAdd.IsNull()) return NS_OK; - if (aValueToAdd.mType != mType) { NS_ERROR("Trying to add incompatible types"); return NS_ERROR_FAILURE; @@ -98,9 +94,6 @@ nsSMILValue::Add(const nsSMILValue& aValueToAdd, PRUint32 aCount) nsresult nsSMILValue::SandwichAdd(const nsSMILValue& aValueToAdd) { - if (aValueToAdd.IsNull()) - return NS_OK; - if (aValueToAdd.mType != mType) { NS_ERROR("Trying to add incompatible types"); return NS_ERROR_FAILURE; @@ -131,14 +124,39 @@ nsSMILValue::Interpolate(const nsSMILValue& aEndVal, } if (aResult.mType != mType) { - aResult.mType->Destroy(aResult); - NS_POSTCONDITION(aResult.IsNull(), "nsSMILValue not null after destroying"); - nsresult rv = mType->Init(aResult); - NS_POSTCONDITION(aResult.mType == mType - || (NS_FAILED(rv) && aResult.IsNull()), - "Post-condition of Init failed. nsSMILValue is invalid"); - if (NS_FAILED(rv)) return rv; + // Outparam has wrong type + nsresult rv = aResult.DestroyAndReinit(mType); + if (NS_FAILED(rv)) + return rv; } return mType->Interpolate(*this, aEndVal, aUnitDistance, aResult); } + +//---------------------------------------------------------------------- +// Helper methods + +// Wrappers for nsISMILType::Init & ::Destroy that verify their postconditions +nsresult +nsSMILValue::InitAndCheckPostcondition(const nsISMILType* aNewType) +{ + nsresult rv = aNewType->Init(*this); + NS_ABORT_IF_FALSE(mType == aNewType || (NS_FAILED(rv) && IsNull()), + "Post-condition of Init failed. nsSMILValue is invalid"); + return rv; +} + +void +nsSMILValue::DestroyAndCheckPostcondition() +{ + mType->Destroy(*this); + NS_ABORT_IF_FALSE(IsNull(), "Post-condition of Destroy failed. " + "nsSMILValue not null after destroying"); +} + +nsresult +nsSMILValue::DestroyAndReinit(const nsISMILType* aNewType) +{ + DestroyAndCheckPostcondition(); + return InitAndCheckPostcondition(aNewType); +} diff --git a/content/smil/nsSMILValue.h b/content/smil/nsSMILValue.h index 218e20226042..a2ab8e3144d1 100644 --- a/content/smil/nsSMILValue.h +++ b/content/smil/nsSMILValue.h @@ -76,6 +76,11 @@ public: void* mPtr; } mU; const nsISMILType* mType; + +protected: + nsresult InitAndCheckPostcondition(const nsISMILType* aNewType); + void DestroyAndCheckPostcondition(); + nsresult DestroyAndReinit(const nsISMILType* aNewType); }; #endif // NS_SMILVALUE_H_