Generics Collections TObjectQueue (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the usage of the generic TObjectQueue class.

Code

type
  { Declare a new object type. }
  TNewObject = class
  private
    FName: String;

  public
    constructor Create(const AName: String);
    destructor Destroy; override;
  end;

{ TNewObject }

constructor TNewObject.Create(const AName: String);
begin
  FName := AName;
end;

destructor TNewObject.Destroy;
begin
  { Show a message whenever an object is destroyed. }
  writeln('Object "' + FName + '" was destroyed!');
  inherited;
end;


var
  Queue: TObjectQueue<TNewObject>;

begin
  { Create a new Queue. }
  Queue := TObjectQueue<TNewObject>.Create;

  { Set the OwnsObjects to true--the Queue will free them automatically. }
  Queue.OwnsObjects := true;

  { Enqueue some items to the Queue. }
  Queue.Enqueue(TNewObject.Create('One'));
  Queue.Enqueue(TNewObject.Create('Two'));
  Queue.Enqueue(TNewObject.Create('Three'));

  {
    Dequeue an instance of the TNewObject class. Destructor
    should be called, because you have set the OwnsObjects
    to true.
  }
  Queue.Dequeue;

  { Destroy the Queue completely--more message boxes will be shown. }
  Queue.Free;
  readln;
end.

Uses

Personal tools
Assistance