This repository has been archived on 2024-11-16. You can view files and clone it, but cannot push or open issues or pull requests.
Unreal-AutoSave/Source/AutoSave/Public/SaveStruct.h
2020-12-13 23:31:57 +08:00

100 lines
2.1 KiB
C++

#pragma once
#include "CoreMinimal.h"
#include "AutoSaveSubsystem.h"
#include "Templates/UnrealTemplate.h"
struct FSaveStructInfo;
class UAutoSaveSubsystem;
template<typename SaveStructType>
class FSaveStructPtr : public FNoncopyable
{
public:
FORCEINLINE FSaveStructPtr()
: AutoSaveSubsystem(nullptr)
, Info(nullptr)
{
}
FORCEINLINE FSaveStructPtr(UAutoSaveSubsystem* InAutoSaveSubsystem, const FString& Filename)
: AutoSaveSubsystem(InAutoSaveSubsystem)
, Info(nullptr)
{
if (AutoSaveSubsystem->AddSaveStructRef(Filename, SaveStructType::StaticStruct()))
{
Info = AutoSaveSubsystem->StructInfos[Filename].Get();
}
}
FORCEINLINE FSaveStructPtr(UAutoSaveSubsystem* InAutoSaveSubsystem, const FString& Filename, FSaveStructLoadDelegate OnLoaded)
: AutoSaveSubsystem(InAutoSaveSubsystem)
, Info(nullptr)
{
if (AutoSaveSubsystem->AddSaveStructRef(Filename, SaveStructType::StaticStruct(), OnLoaded))
{
Info = AutoSaveSubsystem->StructInfos[Filename].Get();
}
}
FORCEINLINE FSaveStructPtr(UAutoSaveSubsystem* InAutoSaveSubsystem, const FString& Filename, FSaveStructLoadDynamicDelegate OnLoaded)
: AutoSaveSubsystem(InAutoSaveSubsystem)
, Info(nullptr)
{
if (AutoSaveSubsystem->AddSaveStructRef(Filename, SaveStructType::StaticStruct(), OnLoaded))
{
Info = AutoSaveSubsystem->StructInfos[Filename].Get();
}
}
FORCEINLINE ~FSaveStructPtr()
{
if (Info)
{
AutoSaveSubsystem->RemoveSaveStructRef(Info->Filename);
}
}
FORCEINLINE SaveStructType* Get() const
{
return Info ? (SaveStructType*)Info->Data.GetData() : nullptr;
}
FORCEINLINE explicit operator bool() const
{
return Info != nullptr;
}
FORCEINLINE const bool IsValid() const
{
return Info != nullptr;
}
FORCEINLINE const bool IsLoaded() const
{
check(IsValid());
return Info->State == ESaveStructState::Idle || Info->State == ESaveStructState::Saving;
}
FORCEINLINE SaveStructType& operator*() const
{
check(IsValid());
return *Get();
}
FORCEINLINE SaveStructType* operator->() const
{
check(IsValid());
return Get();
}
private:
UAutoSaveSubsystem* AutoSaveSubsystem;
FSaveStructInfo* Info;
};