Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Developer Bastian © 2024
- // License Creative Commons DEED 4.0 (https://creativecommons.org/licenses/by-sa/4.0/deed.en)
- // https://docs.unrealengine.com/5.3/en-US/API/Runtime/Core/Containers/FLockFreePointerListLIFOBase/
- #pragma once
- #include "CoreMinimal.h"
- #include "CoreTypes.h"
- #include "UObject/NoExportTypes.h"
- #include "Containers/LockFreeList.h"
- #include "TLIFOList.generated.h"
- /**
- * Struct to showcase the TCircularQueue.
- */
- USTRUCT(BlueprintType)
- struct FLIFOTestStruct
- {
- public:
- GENERATED_USTRUCT_BODY()
- UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test Struct")
- FString Name;
- UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test Struct")
- int32 Number;
- FLIFOTestStruct() : Name(""), Number(0)
- {
- }
- };
- /**
- * Delegates to indicate queue changes
- */
- DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRingBufferChanged, bool, changed);
- /**
- * Class to encapsulate TLockFreePointerListLIFO
- * Does create a Stack pattern (First-In Last-Out)
- */
- UCLASS(BlueprintType, Transient)
- class UTLIFOQueue : public UObject
- {
- GENERATED_BODY()
- public:
- UTLIFOQueue()
- {}
- ~UTLIFOQueue()
- {
- while (auto lifo = BA_LiFOQueue.Pop())
- {
- delete lifo;
- }
- }
- #pragma region Delegates
- UPROPERTY(BlueprintAssignable, Category = "BA Container - LIFO Stack"
- , meta = (ToolTip = "Delegate to indicate enqueue event on queue"))
- FOnRingBufferChanged OnRingBuffer_Enqueue_Delegate;
- UPROPERTY(BlueprintAssignable, Category = "BA Container - LIFO Stack"
- , meta = (ToolTip = "Delegate to indicate dequeue event on queue"))
- FOnRingBufferChanged OnRingBuffer_Dequeue_Delegate;
- #pragma endregion Delegates
- private:
- TLockFreePointerListLIFO<FLIFOTestStruct> BA_LiFOQueue;
- public:
- #pragma region Public Functions
- UFUNCTION(BlueprintCallable, Category = "BA Container - LIFO Stack"
- , meta = (CompactNodeTitle = "Push to Stack"
- , ToolTip = "Pushes one value to the stack"))
- FORCEINLINE void Push(UPARAM(ref) FLIFOTestStruct& Value, bool Broadcast)
- {
- BA_LiFOQueue.Push(&Value);
- if (Broadcast)
- this->OnRingBuffer_Enqueue_Delegate.Broadcast(true);
- }
- UFUNCTION(BlueprintCallable, Category = "BA Container - LIFO Stack"
- , meta = (CompactNodeTitle = "Pop from Stack"
- , ToolTip = "Receives one item from stack"))
- FORCEINLINE FLIFOTestStruct Pop(bool Broadcast)
- {
- FLIFOTestStruct* p = BA_LiFOQueue.Pop();
- if (Broadcast)
- this->OnRingBuffer_Dequeue_Delegate.Broadcast(true);
- return *p;
- }
- UFUNCTION(BlueprintCallable, Category = "BA Container - LIFO Stack"
- , meta = (CompactNodeTitle = "Pop all from Stack"
- , ToolTip = "Receives all items from stack"))
- FORCEINLINE TArray<FLIFOTestStruct> PopAll(bool Broadcast)
- {
- // retrieve all elements
- TArray<FLIFOTestStruct*> p;
- BA_LiFOQueue.PopAll(p);
- // de-reference all values into result array
- TArray<FLIFOTestStruct> result;
- for (auto Value : p)
- result.Push(*Value);
- // clean up p
- p.Empty();
- // return result
- return result;
- }
- UFUNCTION(BlueprintCallable, BlueprintPure, Category = "BA Container - LIFO Stack"
- , meta = (CompactNodeTitle = "Is Empty"
- , ToolTip = "Checks if stack is empty"))
- FORCEINLINE bool IsEmpty()
- {
- return BA_LiFOQueue.IsEmpty();
- }
- UFUNCTION(BlueprintCallable, Category = "BA Container - LIFO Stack"
- , meta = (CompactNodeTitle = "Reset"
- , ToolTip = "Delete all item son stack"))
- FORCEINLINE void Reset()
- {
- while (auto lifo = BA_LiFOQueue.Pop())
- {
- delete lifo;
- }
- }
- #pragma endregion Public Functions
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement