Advertisement
Developer_Bastian

Expose TLockFreePointerListLIFO to Blueprint

Feb 6th, 2024
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.55 KB | Gaming | 0 0
  1. // Developer Bastian © 2024
  2. // License Creative Commons DEED 4.0 (https://creativecommons.org/licenses/by-sa/4.0/deed.en)
  3. // https://docs.unrealengine.com/5.3/en-US/API/Runtime/Core/Containers/FLockFreePointerListLIFOBase/
  4.  
  5.  
  6. #pragma once
  7.  
  8. #include "CoreMinimal.h"
  9. #include "CoreTypes.h"
  10. #include "UObject/NoExportTypes.h"
  11. #include "Containers/LockFreeList.h"
  12.  
  13. #include "TLIFOList.generated.h"
  14.  
  15. /**
  16.  * Struct to showcase the TCircularQueue.
  17.  */
  18. USTRUCT(BlueprintType)
  19. struct FLIFOTestStruct
  20. {
  21. public:
  22.     GENERATED_USTRUCT_BODY()
  23.  
  24.     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test Struct")
  25.     FString Name;
  26.  
  27.     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test Struct")
  28.     int32 Number;
  29.  
  30.     FLIFOTestStruct() : Name(""), Number(0)
  31.     {
  32.     }
  33. };
  34.  
  35. /**
  36.  * Delegates to indicate queue changes
  37.  */
  38. DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnRingBufferChanged, bool, changed);
  39.  
  40.  
  41. /**
  42.  * Class to encapsulate TLockFreePointerListLIFO
  43.  * Does create a Stack pattern (First-In Last-Out)
  44.  */
  45. UCLASS(BlueprintType, Transient)
  46. class UTLIFOQueue : public UObject
  47. {
  48.     GENERATED_BODY()
  49.  
  50. public:
  51.  
  52.     UTLIFOQueue()
  53.     {}
  54.  
  55.     ~UTLIFOQueue()
  56.     {
  57.         while (auto lifo = BA_LiFOQueue.Pop())
  58.         {
  59.             delete lifo;
  60.         }
  61.     }
  62.  
  63.     #pragma region Delegates
  64.  
  65.     UPROPERTY(BlueprintAssignable, Category = "BA Container - LIFO Stack"
  66.         , meta = (ToolTip = "Delegate to indicate enqueue event on queue"))
  67.     FOnRingBufferChanged OnRingBuffer_Enqueue_Delegate;
  68.  
  69.     UPROPERTY(BlueprintAssignable, Category = "BA Container - LIFO Stack"
  70.         , meta = (ToolTip = "Delegate to indicate dequeue event on queue"))
  71.     FOnRingBufferChanged OnRingBuffer_Dequeue_Delegate;
  72.  
  73. #pragma endregion Delegates
  74.  
  75. private:
  76.     TLockFreePointerListLIFO<FLIFOTestStruct> BA_LiFOQueue;
  77.  
  78. public:
  79.  
  80.     #pragma region Public Functions
  81.  
  82.     UFUNCTION(BlueprintCallable, Category = "BA Container - LIFO Stack"
  83.         , meta = (CompactNodeTitle = "Push to Stack"
  84.             , ToolTip = "Pushes one value to the stack"))
  85.     FORCEINLINE void Push(UPARAM(ref) FLIFOTestStruct& Value, bool Broadcast)
  86.     {
  87.         BA_LiFOQueue.Push(&Value);
  88.         if (Broadcast)
  89.             this->OnRingBuffer_Enqueue_Delegate.Broadcast(true);
  90.     }
  91.  
  92.     UFUNCTION(BlueprintCallable, Category = "BA Container - LIFO Stack"
  93.         , meta = (CompactNodeTitle = "Pop from Stack"
  94.             , ToolTip = "Receives one item from stack"))
  95.     FORCEINLINE FLIFOTestStruct Pop(bool Broadcast)
  96.     {
  97.         FLIFOTestStruct* p = BA_LiFOQueue.Pop();
  98.         if (Broadcast)
  99.             this->OnRingBuffer_Dequeue_Delegate.Broadcast(true);
  100.         return *p;
  101.     }
  102.  
  103.     UFUNCTION(BlueprintCallable, Category = "BA Container - LIFO Stack"
  104.         , meta = (CompactNodeTitle = "Pop all from Stack"
  105.             , ToolTip = "Receives all items from stack"))
  106.     FORCEINLINE TArray<FLIFOTestStruct> PopAll(bool Broadcast)
  107.     {
  108.         // retrieve all elements
  109.         TArray<FLIFOTestStruct*> p;
  110.         BA_LiFOQueue.PopAll(p);
  111.         // de-reference all values into result array
  112.         TArray<FLIFOTestStruct> result;
  113.         for (auto Value : p)
  114.             result.Push(*Value);
  115.         // clean up p
  116.         p.Empty();
  117.         // return result
  118.         return result;
  119.     }
  120.  
  121.     UFUNCTION(BlueprintCallable, BlueprintPure, Category = "BA Container - LIFO Stack"
  122.         , meta = (CompactNodeTitle = "Is Empty"
  123.             , ToolTip = "Checks if stack is empty"))
  124.     FORCEINLINE bool IsEmpty()
  125.     {
  126.         return BA_LiFOQueue.IsEmpty();
  127.     }
  128.  
  129.     UFUNCTION(BlueprintCallable, Category = "BA Container - LIFO Stack"
  130.         , meta = (CompactNodeTitle = "Reset"
  131.             , ToolTip = "Delete all item son stack"))
  132.     FORCEINLINE void Reset()
  133.     {
  134.         while (auto lifo = BA_LiFOQueue.Pop())
  135.         {
  136.             delete lifo;
  137.         }
  138.     }
  139.  
  140. #pragma endregion Public Functions
  141. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement