Skip to content

Commit

Permalink
Added FindInPool overload to the Container struct to return Object Da…
Browse files Browse the repository at this point in the history
…ta by Handle
  • Loading branch information
JanSeliv committed Nov 3, 2023
1 parent c0ee9d3 commit dfdcb29
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
41 changes: 12 additions & 29 deletions Source/PoolManager/Private/PoolManagerSubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,23 +167,22 @@ bool UPoolManagerSubsystem::ReturnToPool_Implementation(UObject* Object)
// Alternative to ReturnToPool() to return object to the pool by its handle
bool UPoolManagerSubsystem::ReturnToPool(const FPoolObjectHandle& Handle)
{
if (UObject* PoolObject = FindPoolObjectByHandle(Handle))
const FPoolContainer* Pool = FindPool(Handle.GetObjectClass());
if (!Pool)
{
return ReturnToPool(PoolObject);
// Pool is not registered
return false;
}

// Object is not found, attempt to cancel spawn request if is in queue
for (const TTuple<TObjectPtr<const UClass>, TObjectPtr<UPoolFactory_UObject>>& It : AllFactoriesInternal)
if (const FPoolObjectData* ObjectData = Pool->FindInPool(Handle))
{
FSpawnRequest OutRequest;
if (It.Value && It.Value->DequeueSpawnRequestByHandle(Handle, OutRequest))
{
// Spawn request is found and removed from the queue
return true;
}
return ReturnToPool(ObjectData->PoolObject);
}

return false;
// It's exclusive feature of Handles:
// cancel spawn request if object returns to pool faster than it is spawned
FSpawnRequest OutRequest;
return Pool->GetFactoryChecked().DequeueSpawnRequestByHandle(Handle, OutRequest);
}

/*********************************************************************************************
Expand Down Expand Up @@ -529,25 +528,9 @@ int32 UPoolManagerSubsystem::GetRegisteredObjectsNum_Implementation(const UClass
// Returns the object associated with given handle
UObject* UPoolManagerSubsystem::FindPoolObjectByHandle(const FPoolObjectHandle& Handle) const
{
if (!Handle.IsValid())
{
// Handle is empty, nothing to find
return nullptr;
}

const FPoolContainer* Pool = FindPool(Handle.GetObjectClass());
if (!Pool)
{
// Pool is not registered
return nullptr;
}

const FPoolObjectData* FoundData = Pool->PoolObjects.FindByPredicate([&Handle](const FPoolObjectData& PoolObjectIt)
{
return PoolObjectIt.Handle == Handle;
});

return FoundData ? FoundData->PoolObject : nullptr;
const FPoolObjectData* PoolObject = Pool ? Pool->FindInPool(Handle) : nullptr;
return PoolObject ? PoolObject->PoolObject : nullptr;
}

/*********************************************************************************************
Expand Down
14 changes: 14 additions & 0 deletions Source/PoolManager/Private/PoolManagerTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ FPoolObjectData* FPoolContainer::FindInPool(const UObject& Object)
});
}

// Returns the pointer to the Pool element by specified handle
FPoolObjectData* FPoolContainer::FindInPool(const FPoolObjectHandle& Handle)
{
if (!ensureMsgf(Handle.IsValid(), TEXT("ASSERT: [%i] %s:\n'Handle' is not valid!"), __LINE__, *FString(__FUNCTION__)))
{
return nullptr;
}

return PoolObjects.FindByPredicate([&Handle](const FPoolObjectData& PoolObjectIt)
{
return PoolObjectIt.Handle == Handle;
});
}

// Returns factory or crashes as critical error if it is not set
UPoolFactory_UObject& FPoolContainer::GetFactoryChecked() const
{
Expand Down
4 changes: 4 additions & 0 deletions Source/PoolManager/Public/PoolManagerTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ struct POOLMANAGER_API FPoolContainer
FPoolObjectData* FindInPool(const UObject& Object);
const FORCEINLINE FPoolObjectData* FindInPool(const UObject& Object) const { return const_cast<FPoolContainer*>(this)->FindInPool(Object); }

/** Returns the pointer to the Pool element by specified handle. */
FPoolObjectData* FindInPool(const FPoolObjectHandle& Handle);
const FORCEINLINE FPoolObjectData* FindInPool(const FPoolObjectHandle& Handle) const { return const_cast<FPoolContainer*>(this)->FindInPool(Handle); }

/** Returns factory or crashes as critical error if it is not set. */
UPoolFactory_UObject& GetFactoryChecked() const;

Expand Down

0 comments on commit dfdcb29

Please sign in to comment.