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; using CPF = CParticleDataFactory;
CWeaponDescription* CProjectileWeaponDataFactory::GetGeneratorDesc(CInputStream& in, CSimplePool* resPool) { std::unique_ptr<CWeaponDescription> CProjectileWeaponDataFactory::GetGeneratorDesc(CInputStream& in,
CSimplePool* resPool) {
return CreateGeneratorDescription(in, resPool); return CreateGeneratorDescription(in, resPool);
} }
CWeaponDescription* CProjectileWeaponDataFactory::CreateGeneratorDescription(CInputStream& in, CSimplePool* resPool) { std::unique_ptr<CWeaponDescription> CProjectileWeaponDataFactory::CreateGeneratorDescription(CInputStream& in,
FourCC clsId = CPF::GetClassID(in); CSimplePool* resPool) {
if (clsId == FOURCC('WPSM')) { const FourCC clsId = CPF::GetClassID(in);
CWeaponDescription* desc = new CWeaponDescription; if (clsId != FOURCC('WPSM')) {
CreateWPSM(desc, in, resPool); return nullptr;
return desc;
} }
return nullptr; auto desc = std::make_unique<CWeaponDescription>();
CreateWPSM(desc.get(), in, resPool);
return desc;
} }
bool CProjectileWeaponDataFactory::CreateWPSM(CWeaponDescription* desc, CInputStream& in, CSimplePool* resPool) { 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, CFactoryFnReturn FProjectileWeaponDataFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms,
CObjectReference*) { CObjectReference*) {
CSimplePool* sp = vparms.GetOwnedObj<CSimplePool*>(); CSimplePool* sp = vparms.GetOwnedObj<CSimplePool*>();
return TToken<CWeaponDescription>::GetIObjObjectFor( return TToken<CWeaponDescription>::GetIObjObjectFor(CProjectileWeaponDataFactory::GetGeneratorDesc(in, sp));
std::unique_ptr<CWeaponDescription>(CProjectileWeaponDataFactory::GetGeneratorDesc(in, sp)));
} }
} // namespace urde } // namespace urde

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <memory>
#include "Runtime/CFactoryMgr.hpp" #include "Runtime/CFactoryMgr.hpp"
#include "Runtime/CToken.hpp" #include "Runtime/CToken.hpp"
#include "Runtime/IOStreams.hpp" #include "Runtime/IOStreams.hpp"
@ -7,14 +9,15 @@
#include "Runtime/RetroTypes.hpp" #include "Runtime/RetroTypes.hpp"
namespace urde { namespace urde {
class CWeaponDescription;
class CSimplePool; class CSimplePool;
class CWeaponDescription;
class CProjectileWeaponDataFactory { 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); static bool CreateWPSM(CWeaponDescription* desc, CInputStream& in, CSimplePool* resPool);
public: 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, CFactoryFnReturn FProjectileWeaponDataFactory(const SObjectTag& tag, CInputStream& in, const CVParamTransfer& vparms,