RAD Studio VCL Reference
|
Queue of objects.
TObjectQueue<T: class> = class(TQueue<T>);
template <T: class> class TObjectQueue : public TQueue<T>;
TObjectQueue represents a queue of objects.
TObjectQueue is a TQueue with the capability of automatically freeing object entries when they are removed from the queue. When a TObjectQueue is instantiated, an AOwnsObjects parameter specifies whether the queue owns the queue entries. If the entry is owned, when the entry object is removed from the queue, the entry object is freed.
The OwnsObjects property gets or sets the object ownership.
Delphi Examples:
{ This example demonstrates the usage of the generic TObjectQueue class. } 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 } MessageDlg('Object "' + FName + '" was destroyed!', mtInformation, [mbOK], 0); inherited; end; procedure TForm3.Button1Click(Sender: TObject); var Queue: TObjectQueue<TNewObject>; Button: TButton; 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 up the Queue } Queue.Enqueue(TNewObject.Create('One')); Queue.Enqueue(TNewObject.Create('Two')); Queue.Enqueue(TNewObject.Create('Three')); { Dequeue an instance of TNewObject class. Destructor show be called because we have set the OwnsObjects to true! } Queue.Dequeue(); { Destroy the Queue completely - more messageboxes will be shown } Queue.Free; end;
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
|
What do you think about this topic? Send feedback!
|