Developer_Bastian

Expose TQueue to Blueprint

Jan 28th, 2024 (edited)
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 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. // Tutorial video how to use this at https://youtu.be/UJOrwTtWQBg
  4. // Part of an Unreal Basics video tutorial series at: https://bit.ly/Unreal_Basics_en
  5.  
  6. #pragma once
  7.  
  8. #include "CoreMinimal.h"
  9. #include "UObject/NoExportTypes.h"
  10. #include "Containers/Queue.h"
  11.  
  12. #include "TQueue.generated.h"
  13.  
  14. /**
  15.  * Struct to showcase the TQueue.
  16.  */
  17. USTRUCT(BlueprintType)
  18. struct FQueueTestStruct
  19. {
  20. public:
  21.     GENERATED_USTRUCT_BODY()
  22.  
  23.     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test Struct")
  24.     FString Name;
  25.  
  26.     UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Test Struct")
  27.     int32 Number;
  28.  
  29.     FQueueTestStruct() : Name(""), Number(0)
  30.     {
  31.     }
  32. };
  33.  
  34. /**
  35.  * Delegates to indicate queue changes
  36.  */
  37. DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnQueueChanged, bool, changed);
  38.  
  39.  
  40. /**
  41.  * Class to encapsulate TQueue
  42.  * TQueue implements an unbounded non-intrusive queue using a lock-free linked
  43.  * list that stores copies of the queued items. The template can operate in two modes:
  44.  * Multiple-producers single-consumer (MPSC) and Single-producer single-consumer (SPSC).
  45.  *
  46.  * The queue is thread-safe in both modes. The Dequeue() method ensures thread-safety by
  47.  * writing it in a way that does not depend on possible instruction reordering on the CPU.
  48.  * The Enqueue() method uses an atomic compare-and-swap in multiple-producers scenarios.
  49.  */
  50. UCLASS(BlueprintType, Transient)
  51. class UTQueue : public UObject
  52. {
  53.     GENERATED_BODY()
  54.  
  55. public:
  56.  
  57.     UTQueue()
  58.     {
  59.         BA_Queue.Empty();
  60.     }
  61.  
  62.     #pragma region Delegates
  63.  
  64.     UPROPERTY(BlueprintAssignable, Category = "BA Container - Queue"
  65.         , meta = (ToolTip = "Delegate to indicate enqueue event on queue"))
  66.     FOnQueueChanged OnQueue_Enqueue_Delegate;
  67.  
  68.     UPROPERTY(BlueprintAssignable, Category = "BA Container - Queue"
  69.         , meta = (ToolTip = "Delegate to indicate dequeue event on queue"))
  70.     FOnQueueChanged OnQueue_Dequeue_Delegate;
  71.  
  72. #pragma endregion Delegates
  73.  
  74. private:
  75.     // Standard is Multiple-producers single-consumer (MPSC)
  76.     TQueue<FQueueTestStruct, EQueueMode::Mpsc> BA_Queue;
  77.  
  78. public:
  79.  
  80.     #pragma region Public Functions
  81.  
  82.     UFUNCTION(BlueprintCallable, Category = "BA Container - Queue"
  83.         , meta = (CompactNodeTitle = "Enqueue to Queue"
  84.             , ToolTip = "Adds an item to the head of the queue"))
  85.     FORCEINLINE void Enqueue(UPARAM(ref) FQueueTestStruct& QueueItem)
  86.     {
  87.         this->BA_Queue.Enqueue(QueueItem);
  88.         this->OnQueue_Enqueue_Delegate.Broadcast(true);
  89.     }
  90.  
  91.     UFUNCTION(BlueprintCallable, Category = "BA Container - Queue"
  92.         , meta = (CompactNodeTitle = "Dequeue from Queue"
  93.             , ToolTip = "Removes and returns the item from the tail of the queue"))
  94.     FORCEINLINE FQueueTestStruct Dequeue()
  95.     {
  96.         FQueueTestStruct QueueItem;
  97.         this->BA_Queue.Dequeue(QueueItem);
  98.         this->OnQueue_Dequeue_Delegate.Broadcast(true);
  99.         return QueueItem;
  100.     }
  101.  
  102.     UFUNCTION(BlueprintCallable, BlueprintPure, Category = "BA Container - Queue"
  103.         , meta = (CompactNodeTitle = "Queue Empty?"
  104.             , ToolTip = "Checks whether the queue is empty"))
  105.     FORCEINLINE bool IsEmpty()
  106.     {
  107.         return this->BA_Queue.IsEmpty();
  108.     }
  109.  
  110.     UFUNCTION(BlueprintCallable, Category = "BA Container - Queue"
  111.         , meta = (CompactNodeTitle = "Peek Queue"
  112.             , ToolTip = "Peek at the queue's tail item without removing it"))
  113.     FORCEINLINE FQueueTestStruct Peek()
  114.     {
  115.         FQueueTestStruct QueueItem;
  116.         this->BA_Queue.Peek(QueueItem);
  117.         return QueueItem;
  118.     }
  119.  
  120.     UFUNCTION(BlueprintCallable, Category = "BA Container - Queue"
  121.         , meta = (CompactNodeTitle = "Pop Queue"
  122.             , ToolTip = "Removes the item from the tail of the queue. Returns true if a value was removed, false if the queue was empty"))
  123.     FORCEINLINE bool Pop()
  124.     {
  125.         return this->BA_Queue.Pop();
  126.     }
  127.  
  128. #pragma endregion Public Functions
  129. };
  130.  
Add Comment
Please, Sign In to add comment