From 1f42b9be932ad57992faa5dfc21db4d1f001618c Mon Sep 17 00:00:00 2001 From: Lioncash Date: Thu, 12 Mar 2020 14:20:15 -0400 Subject: [PATCH] CParticleElectricDataFactory: Make GetGeneratorDesc() return a unique_ptr Same behavior, but with safer memory management. --- .../Particle/CParticleElectricDataFactory.cpp | 25 +++++++++++-------- .../Particle/CParticleElectricDataFactory.hpp | 6 +++-- 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/Runtime/Particle/CParticleElectricDataFactory.cpp b/Runtime/Particle/CParticleElectricDataFactory.cpp index c94bc768a..fe366b70a 100644 --- a/Runtime/Particle/CParticleElectricDataFactory.cpp +++ b/Runtime/Particle/CParticleElectricDataFactory.cpp @@ -13,20 +13,24 @@ static logvisor::Module Log("urde::CParticleElectricDataFactory"); using CPF = CParticleDataFactory; -CElectricDescription* CParticleElectricDataFactory::GetGeneratorDesc(CInputStream& in, CSimplePool* resPool) { +std::unique_ptr CParticleElectricDataFactory::GetGeneratorDesc(CInputStream& in, + CSimplePool* resPool) { return CreateElectricDescription(in, resPool); } -CElectricDescription* CParticleElectricDataFactory::CreateElectricDescription(CInputStream& in, CSimplePool* resPool) { - FourCC cid = CPF::GetClassID(in); - if (cid == FOURCC('ELSM')) { - CElectricDescription* desc = new CElectricDescription; - CreateELSM(desc, in, resPool); - LoadELSMTokens(desc); - return desc; +std::unique_ptr CParticleElectricDataFactory::CreateElectricDescription(CInputStream& in, + CSimplePool* resPool) { + const FourCC cid = CPF::GetClassID(in); + + if (cid != FOURCC('ELSM')) { + return nullptr; } - return nullptr; + auto desc = std::make_unique(); + CreateELSM(desc.get(), in, resPool); + LoadELSMTokens(desc.get()); + return desc; + } bool CParticleElectricDataFactory::CreateELSM(CElectricDescription* desc, CInputStream& in, CSimplePool* resPool) { @@ -124,7 +128,6 @@ void CParticleElectricDataFactory::LoadELSMTokens(CElectricDescription* desc) { CFactoryFnReturn FParticleElectricDataFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms, CObjectReference*) { CSimplePool* sp = vparms.GetOwnedObj(); - return TToken::GetIObjObjectFor( - std::unique_ptr(CParticleElectricDataFactory::GetGeneratorDesc(in, sp))); + return TToken::GetIObjObjectFor(CParticleElectricDataFactory::GetGeneratorDesc(in, sp)); } } // namespace urde diff --git a/Runtime/Particle/CParticleElectricDataFactory.hpp b/Runtime/Particle/CParticleElectricDataFactory.hpp index e71fc4609..7ec5e749e 100644 --- a/Runtime/Particle/CParticleElectricDataFactory.hpp +++ b/Runtime/Particle/CParticleElectricDataFactory.hpp @@ -1,5 +1,7 @@ #pragma once +#include + #include "Runtime/CFactoryMgr.hpp" #include "Runtime/CToken.hpp" #include "Runtime/IOStreams.hpp" @@ -10,12 +12,12 @@ namespace urde { class CElectricDescription; class CSimplePool; class CParticleElectricDataFactory { - static CElectricDescription* CreateElectricDescription(CInputStream& in, CSimplePool* resPool); + static std::unique_ptr CreateElectricDescription(CInputStream& in, CSimplePool* resPool); static bool CreateELSM(CElectricDescription* desc, CInputStream& in, CSimplePool* resPool); static void LoadELSMTokens(CElectricDescription* desc); public: - static CElectricDescription* GetGeneratorDesc(CInputStream& in, CSimplePool* resPool); + static std::unique_ptr GetGeneratorDesc(CInputStream& in, CSimplePool* resPool); }; CFactoryFnReturn FParticleElectricDataFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms,