Created
September 5, 2025 14:38
-
-
Save hwi-middle/fae2622d6db2fc0ab5b0f84b1ac73b90 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #pragma once | |
| #include "CoreMinimal.h" | |
| /** | |
| * | |
| */ | |
| UENUM() | |
| enum class EAssertionLevel : uint8 | |
| { | |
| None, | |
| Ensure, | |
| Check, | |
| }; | |
| class PROJECTP_API FPPConstructorHelper | |
| { | |
| public: | |
| template <class T> | |
| static T* FindAndGetObject(const TCHAR* InName, const EAssertionLevel InAssertionLevel = EAssertionLevel::None, const uint32 InLoadFlags = LOAD_None) | |
| { | |
| static TMap<FName, UObject*> Cache; | |
| if (Cache.Contains(InName)) | |
| { | |
| return Cast<T>(Cache[InName]); | |
| } | |
| ConstructorHelpers::FObjectFinder<T> ObjectFinder(InName, InLoadFlags); | |
| AssertByAssertionLevel(ObjectFinder, InAssertionLevel); | |
| T* Object = ObjectFinder.Object; | |
| Cache.Add(InName, Object); | |
| return Object; | |
| } | |
| template <class T> | |
| static void FindObjectAndInitialize(const TCHAR* InName, TFunctionRef<void(T*)> Func, const EAssertionLevel InAssertionLevel = EAssertionLevel::None, uint32 InLoadFlags = LOAD_None) | |
| { | |
| if (T* Object = FindAndGetObject<T>(InName, InAssertionLevel, InLoadFlags)) | |
| { | |
| Func(Object); | |
| } | |
| } | |
| template <class T> | |
| static TSubclassOf<T> FindAndGetClass(const TCHAR* InName, const EAssertionLevel InAssertionLevel = EAssertionLevel::None) | |
| { | |
| static TMap<FName, TSubclassOf<T>> Cache; | |
| if (Cache.Contains(InName)) | |
| { | |
| return Cache[InName]; | |
| } | |
| ConstructorHelpers::FClassFinder<T> ClassFinder(InName); | |
| AssertByAssertionLevel(ClassFinder, InAssertionLevel); | |
| TSubclassOf<T> Class = ClassFinder.Class; | |
| Cache.Add(InName, Class); | |
| return Class; | |
| } | |
| template <class T> | |
| static void FindClassAndInitialize(const TCHAR* InName, TFunctionRef<void(TSubclassOf<T>)> Func, const EAssertionLevel InAssertionLevel = EAssertionLevel::None) | |
| { | |
| if (TSubclassOf<T> Class = FindAndGetClass<T>(InName, InAssertionLevel)) | |
| { | |
| Func(Class); | |
| } | |
| } | |
| private: | |
| template <class T> | |
| static void AssertByAssertionLevel(T& InFinder, const EAssertionLevel InAssertionLevel) | |
| { | |
| switch (InAssertionLevel) | |
| { | |
| case EAssertionLevel::None: | |
| break; | |
| case EAssertionLevel::Ensure: | |
| ensure(InFinder.Succeeded()); | |
| break; | |
| case EAssertionLevel::Check: | |
| check(InFinder.Succeeded()); | |
| break; | |
| default: | |
| checkNoEntry(); | |
| } | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment