CResource: Make BuildDependencyTree() return a unique_ptr

Makes the functions more memory safe in terms of freeing memory in
exceptional paths .
This commit is contained in:
Lioncash
2020-06-11 18:25:35 -04:00
parent eb8ca98a8a
commit ce315280c3
24 changed files with 82 additions and 93 deletions

View File

@@ -21,10 +21,10 @@ public:
{
}
CDependencyTree* BuildDependencyTree() const override
std::unique_ptr<CDependencyTree> BuildDependencyTree() const override
{
auto *pTree = new CDependencyTree();
AddDependenciesToTree(pTree);
auto pTree = std::make_unique<CDependencyTree>();
AddDependenciesToTree(pTree.get());
return pTree;
}

View File

@@ -128,14 +128,14 @@ public:
}
}
CDependencyTree* BuildDependencyTree() const
std::unique_ptr<CDependencyTree> BuildDependencyTree() const
{
CDependencyTree *pTree = new CDependencyTree();
auto pTree = std::make_unique<CDependencyTree>();
// Character dependencies
for (uint32 iChar = 0; iChar < mCharacters.size(); iChar++)
for (const auto& character : mCharacters)
{
CSetCharacterDependency *pCharTree = CSetCharacterDependency::BuildTree( mCharacters[iChar] );
CSetCharacterDependency *pCharTree = CSetCharacterDependency::BuildTree(character);
ASSERT(pCharTree);
pTree->AddChild(pCharTree);
}
@@ -150,42 +150,38 @@ public:
pTree->AddChild(pAnimTree);
}
}
else if (Game() <= EGame::Corruption)
{
const SSetCharacter& rkChar = mCharacters[0];
std::set<CAnimPrimitive> PrimitiveSet;
// Animations
for (uint32 iAnim = 0; iAnim < mAnimations.size(); iAnim++)
for (auto& anim : mAnimations)
{
const SAnimation& rkAnim = mAnimations[iAnim];
rkAnim.pMetaAnim->GetUniquePrimitives(PrimitiveSet);
anim.pMetaAnim->GetUniquePrimitives(PrimitiveSet);
}
CSourceAnimData *pAnimData = gpResourceStore->LoadResource<CSourceAnimData>(rkChar.AnimDataID);
if (pAnimData)
pAnimData->AddTransitionDependencies(pTree);
pAnimData->AddTransitionDependencies(pTree.get());
for (auto Iter = PrimitiveSet.begin(); Iter != PrimitiveSet.end(); Iter++)
for (auto& prim : PrimitiveSet)
{
const CAnimPrimitive& rkPrim = *Iter;
pTree->AddDependency(rkPrim.Animation());
pTree->AddDependency(prim.Animation());
}
// Event sounds
for (uint32 iSound = 0; iSound < rkChar.SoundEffects.size(); iSound++)
for (const auto& effect : rkChar.SoundEffects)
{
pTree->AddDependency(rkChar.SoundEffects[iSound]);
pTree->AddDependency(effect);
}
}
else
{
const SSetCharacter& rkChar = mCharacters[0];
for (uint32 iDep = 0; iDep < rkChar.DKDependencies.size(); iDep++)
pTree->AddDependency(rkChar.DKDependencies[iDep]);
for (const auto& dep : rkChar.DKDependencies)
pTree->AddDependency(dep);
}
return pTree;

View File

@@ -9,9 +9,9 @@ CAnimation::CAnimation(CResourceEntry *pEntry /*= 0*/)
{
}
CDependencyTree* CAnimation::BuildDependencyTree() const
std::unique_ptr<CDependencyTree> CAnimation::BuildDependencyTree() const
{
CDependencyTree *pTree = new CDependencyTree();
auto pTree = std::make_unique<CDependencyTree>();
pTree->AddDependency(mpEventData);
return pTree;
}

View File

@@ -37,8 +37,8 @@ class CAnimation : public CResource
TResPtr<CAnimEventData> mpEventData;
public:
CAnimation(CResourceEntry *pEntry = 0);
CDependencyTree* BuildDependencyTree() const override;
explicit CAnimation(CResourceEntry *pEntry = nullptr);
std::unique_ptr<CDependencyTree> BuildDependencyTree() const override;
void EvaluateTransform(float Time, uint32 BoneID, CVector3f *pOutTranslation, CQuaternion *pOutRotation, CVector3f *pOutScale) const;
bool HasTranslation(uint32 BoneID) const;

View File

@@ -43,13 +43,13 @@ public:
delete mpDefaultTransition;
}
CDependencyTree* BuildDependencyTree() const override
std::unique_ptr<CDependencyTree> BuildDependencyTree() const override
{
// SAND normally has dependencies from meta-transitions and events
// However, all of these can be character-specific. To simplify things, all SAND
// dependencies are being added to the CHAR dependency tree instead. Therefore the
// SAND dependency tree is left empty.
return new CDependencyTree();
return std::make_unique<CDependencyTree>();
}
void GetUniquePrimitives(std::set<CAnimPrimitive>& rPrimSet) const