Bug 542731, Patch A: Clean up nsSMILValue class. r=roc

This commit is contained in:
Daniel Holbert 2010-01-29 12:18:50 -08:00
Родитель 0b1058cb81
Коммит 6409e78281
2 изменённых файлов: 56 добавлений и 33 удалений

Просмотреть файл

@ -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);
}

Просмотреть файл

@ -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_