CParticleElectricDataFactory: Make GetGeneratorDesc() return a unique_ptr

Same behavior, but with safer memory management.
This commit is contained in:
Lioncash 2020-03-12 14:20:15 -04:00
parent 59bbbdf41c
commit 1f42b9be93
2 changed files with 18 additions and 13 deletions

View File

@ -13,20 +13,24 @@ static logvisor::Module Log("urde::CParticleElectricDataFactory");
using CPF = CParticleDataFactory;
CElectricDescription* CParticleElectricDataFactory::GetGeneratorDesc(CInputStream& in, CSimplePool* resPool) {
std::unique_ptr<CElectricDescription> 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<CElectricDescription> 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<CElectricDescription>();
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<CSimplePool*>();
return TToken<CElectricDescription>::GetIObjObjectFor(
std::unique_ptr<CElectricDescription>(CParticleElectricDataFactory::GetGeneratorDesc(in, sp)));
return TToken<CElectricDescription>::GetIObjObjectFor(CParticleElectricDataFactory::GetGeneratorDesc(in, sp));
}
} // namespace urde

View File

@ -1,5 +1,7 @@
#pragma once
#include <memory>
#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<CElectricDescription> 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<CElectricDescription> GetGeneratorDesc(CInputStream& in, CSimplePool* resPool);
};
CFactoryFnReturn FParticleElectricDataFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms,