Files
UnrealEngineUWP/Engine/Source/Runtime/InteractiveToolsFramework/Public/ContextObjectStore.h
brooke hubert f149e45233 Fixing a bug where creating temporary actors via typed elements would not be able to delete them again in single place tool
#preflight 61b22ba9c674eb9fc9d8ea9a
#Jira None
#rb julien.stjean ryan.schmidt

#ROBOMERGE-AUTHOR: brooke.hubert
#ROBOMERGE-SOURCE: CL 18420733 in //UE5/Release-5.0/... via CL 18422600
#ROBOMERGE-BOT: STARSHIP (Release-Engine-Staging -> Release-Engine-Test) (v897-18405271)

[CL 18422868 by brooke hubert in ue5-release-engine-test branch]
2021-12-09 14:51:37 -05:00

84 lines
2.5 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "UObject/Object.h"
#include "ContextObjectStore.generated.h"
/**
* A context object store allows tools to get access to arbitrary objects which expose data or APIs to enable additional functionality.
* Some example use cases of context objects:
* - A tool builder may disallow a particular tool if a needed API object is not present in the context store.
* - A tool may allow extra actions if it has access to a particular API object in the context store.
* - A tool may choose to initialize itself differently based on the presence of a selection-holding data object in the context store.
*/
UCLASS(Transient)
class INTERACTIVETOOLSFRAMEWORK_API UContextObjectStore : public UObject
{
GENERATED_BODY()
public:
/**
* Finds the first the context object of the given type. Can return a subclass of the given type.
* @returns the found context object, casted to the given type, or nullptr if none matches.
*/
template <typename TObjectType>
TObjectType* FindContext() const
{
for (UObject* ContextObject : ContextObjects)
{
if (TObjectType* CastedObject = Cast<TObjectType>(ContextObject))
{
return CastedObject;
}
}
if (UContextObjectStore* ParentStore = Cast<UContextObjectStore>(GetOuter()))
{
return ParentStore->FindContext<TObjectType>();
}
return nullptr;
}
/**
* Finds the first context object that derives from the given class.
* @returns the found context object, or nullptr if none matches.
*/
UObject* FindContextByClass(UClass* InClass) const;
/**
* Adds a data object to the tool manager's set of shared data objects.
* @returns true if the addition is successful.
*/
bool AddContextObject(UObject* InContextObject);
/**
* Removes a data object from the tool manager's set of shared data objects.
* @returns true if the removal is successful.
*/
bool RemoveContextObject(UObject* InContextObject);
/**
* Removes any data objects from the tool manager's set of shared data objects that are of type @param InClass
* @returns true if any objects are removed.
*/
bool RemoveContextObjectsOfType(const UClass* InClass);
template <class TObjectType>
bool RemoveContextObjectsOfType()
{
return RemoveContextObjectsOfType(TObjectType::StaticClass());
}
/**
* Shuts down the context object store, releasing hold on any stored content objects.
*/
void Shutdown();
protected:
UPROPERTY()
TArray<TObjectPtr<UObject>> ContextObjects;
};