CProjectileWeaponDataFactory: Make GetGeneratorDesc() return a unique_ptr

Same behavior, but with safer memory management.
This commit is contained in:
Lioncash 2020-03-12 13:59:53 -04:00
parent d93df18a37
commit 93121c38f7
2 changed files with 18 additions and 13 deletions

View File

@ -14,19 +14,22 @@ static logvisor::Module Log("urde::CProjectileWeaponDataFactory");
using CPF = CParticleDataFactory;
CWeaponDescription* CProjectileWeaponDataFactory::GetGeneratorDesc(CInputStream& in, CSimplePool* resPool) {
std::unique_ptr<CWeaponDescription> CProjectileWeaponDataFactory::GetGeneratorDesc(CInputStream& in,
CSimplePool* resPool) {
return CreateGeneratorDescription(in, resPool);
}
CWeaponDescription* CProjectileWeaponDataFactory::CreateGeneratorDescription(CInputStream& in, CSimplePool* resPool) {
FourCC clsId = CPF::GetClassID(in);
if (clsId == FOURCC('WPSM')) {
CWeaponDescription* desc = new CWeaponDescription;
CreateWPSM(desc, in, resPool);
return desc;
std::unique_ptr<CWeaponDescription> CProjectileWeaponDataFactory::CreateGeneratorDescription(CInputStream& in,
CSimplePool* resPool) {
const FourCC clsId = CPF::GetClassID(in);
if (clsId != FOURCC('WPSM')) {
return nullptr;
}
return nullptr;
auto desc = std::make_unique<CWeaponDescription>();
CreateWPSM(desc.get(), in, resPool);
return desc;
}
bool CProjectileWeaponDataFactory::CreateWPSM(CWeaponDescription* desc, CInputStream& in, CSimplePool* resPool) {
@ -159,7 +162,6 @@ bool CProjectileWeaponDataFactory::CreateWPSM(CWeaponDescription* desc, CInputSt
CFactoryFnReturn FProjectileWeaponDataFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms,
CObjectReference*) {
CSimplePool* sp = vparms.GetOwnedObj<CSimplePool*>();
return TToken<CWeaponDescription>::GetIObjObjectFor(
std::unique_ptr<CWeaponDescription>(CProjectileWeaponDataFactory::GetGeneratorDesc(in, sp)));
return TToken<CWeaponDescription>::GetIObjObjectFor(CProjectileWeaponDataFactory::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"
@ -7,14 +9,15 @@
#include "Runtime/RetroTypes.hpp"
namespace urde {
class CWeaponDescription;
class CSimplePool;
class CWeaponDescription;
class CProjectileWeaponDataFactory {
static CWeaponDescription* CreateGeneratorDescription(CInputStream& in, CSimplePool* resPool);
static std::unique_ptr<CWeaponDescription> CreateGeneratorDescription(CInputStream& in, CSimplePool* resPool);
static bool CreateWPSM(CWeaponDescription* desc, CInputStream& in, CSimplePool* resPool);
public:
static CWeaponDescription* GetGeneratorDesc(CInputStream& in, CSimplePool* resPool);
static std::unique_ptr<CWeaponDescription> GetGeneratorDesc(CInputStream& in, CSimplePool* resPool);
};
CFactoryFnReturn FProjectileWeaponDataFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms,