RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TQueue Class

Queue implemented over array, using wrapping.

Pascal
TQueue<T> = class(TEnumerable<T>);
C++
template <T>
class TQueue : public TEnumerable<T>;

TQueue is a queue implemented over array, using wrapping. 

You can add items to the end of the queue and remove them from the start or remove all the items. You can also peek at the top item without removing it. You can add nil objects to the queue. 

Count contains the number of items in the queue. 

An OnNotify event tells you when the queue has changed. 

The class TObjectQueue inherits from TQueue and provides an automatic mechanism for freeing objects removed from queues.  

Delphi Examples: 

 

{
This example demonstrates the usage of the generic TQueue class.
}
procedure TForm3.Button1Click(Sender: TObject);
var
  Queue: TQueue<String>;
begin
  { Create a new Queue }
  Queue := TQueue<String>.Create();

  { Register a notification call-back }
  Queue.OnNotify := QueueChanged;

  { Enqueue some items up the Queue }
  Queue.Enqueue('John');
  Queue.Enqueue('Mary');
  Queue.Enqueue('Bob');
  Queue.Enqueue('Anna');
  Queue.Enqueue('Erica');

  { Show the last enqueued element without modifying the Queue }
  MessageDlg('First enqueued element is: "' + Queue.Peek() + '".', mtInformation, [mbOK], 0);

  { Extract the top element: "Erica" }
  Queue.Extract();

  { Reduce the capacity }
  Queue.TrimExcess();

  { The remaining count of elements }
  MessageDlg('The queue contains ' + IntToStr(Queue.Count) + ' elements.', mtInformation, [mbOK], 0);

  { Show the last enqueued element by modifying the Queue }
  MessageDlg('First enqueued element is: "' + Queue.Dequeue() + '".', mtInformation, [mbOK], 0);

  { Clear the Queue }
  Queue.Clear();

  { Destroy the Queue completely }
  Queue.Free;
end;

procedure TForm3.QueueChanged(Sender: TObject; const Item: String; Action: TCollectionNotification);
begin
  { This method is called by the List everytime a change occurs }
  if Action = cnAdded then
     MessageDlg('Element added: ' + Item, mtInformation, [mbOK], 0)
  else if Action = cnRemoved then
     MessageDlg('Element removed: ' + Item, mtInformation, [mbOK], 0)
  else if Action = cnExtracted then
     MessageDlg('Element extracted: ' + Item, mtInformation, [mbOK], 0)
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!