CResourceFactory: Make use of unique_ptr

Makes the API more memory safe
This commit is contained in:
Lioncash
2020-06-11 18:57:09 -04:00
parent ce315280c3
commit 907f1270bd
44 changed files with 314 additions and 276 deletions

View File

@@ -89,10 +89,7 @@ CResourceEntry* CResourceEntry::BuildFromDirectory(CResourceStore *pStore, CResT
return pEntry;
}
CResourceEntry::~CResourceEntry()
{
if (mpResource) delete mpResource;
}
CResourceEntry::~CResourceEntry() = default;
bool CResourceEntry::LoadMetadata()
{
@@ -386,7 +383,8 @@ bool CResourceEntry::Cook()
CResource* CResourceEntry::Load()
{
// If the asset is already loaded then just return it immediately
if (mpResource) return mpResource;
if (mpResource)
return mpResource.get();
// Always try to load raw version as the raw version contains extra editor-only data.
// If there is no raw version (which will be the case for resource types that don't
@@ -406,8 +404,7 @@ CResource* CResourceEntry::Load()
if (!Reader.IsValid())
{
errorf("Failed to load raw resource; falling back on cooked. Raw path: %s", *RawAssetPath());
delete mpResource;
mpResource = nullptr;
mpResource.reset();
}
else
@@ -419,7 +416,7 @@ CResource* CResourceEntry::Load()
}
if (mpResource)
return mpResource;
return mpResource.get();
}
ASSERT(!mpResource);
@@ -446,8 +443,11 @@ CResource* CResourceEntry::Load()
CResource* CResourceEntry::LoadCooked(IInputStream& rInput)
{
// Overload to allow for load from an arbitrary input stream.
if (mpResource) return mpResource;
if (!rInput.IsValid()) return nullptr;
if (mpResource)
return mpResource.get();
if (!rInput.IsValid())
return nullptr;
// Set gpResourceStore to ensure the correct resource store is accessed by loader functions
CResourceStore *pOldStore = gpResourceStore;
@@ -458,15 +458,14 @@ CResource* CResourceEntry::LoadCooked(IInputStream& rInput)
mpStore->TrackLoadedResource(this);
gpResourceStore = pOldStore;
return mpResource;
return mpResource.get();
}
bool CResourceEntry::Unload()
{
ASSERT(mpResource != nullptr);
ASSERT(!mpResource->IsReferenced());
delete mpResource;
mpResource = nullptr;
mpResource.reset();
return true;
}

View File

@@ -27,7 +27,7 @@ DECLARE_FLAGS(EResEntryFlag, FResEntryFlags)
class CResourceEntry
{
CResource *mpResource;
std::unique_ptr<CResource> mpResource;
CResTypeInfo *mpTypeInfo;
CResourceStore *mpStore;
std::unique_ptr<CDependencyTree> mpDependencies;
@@ -97,7 +97,7 @@ public:
bool IsLoaded() const { return mpResource != nullptr; }
bool IsCategorized() const { return mpDirectory && !mpDirectory->FullPath().CaseInsensitiveCompare( mpStore->DefaultResourceDirPath() ); }
bool IsNamed() const { return mName != mID.ToString(); }
CResource* Resource() const { return mpResource; }
CResource* Resource() const { return mpResource.get(); }
CResTypeInfo* TypeInfo() const { return mpTypeInfo; }
CResourceStore* ResourceStore() const { return mpStore; }
CDependencyTree* Dependencies() const { return mpDependencies.get(); }