Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to write a stack class for Interfaces Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
19-May-03
Category
OO-related
Language
Delphi 2.x
Views
133
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

How to work with a stack of interfaces? Should I use TObjectStack or simply TStack?

Answer:

Using a storage class that uses elements typed as pointer or TObject is very risky 
with interfaces, since hard typecasts are needed, and they mess up the reference 
counting. So it is better to write your own stack class for interfaces, perhaps 
based on an internal TInterfaceList as storage mechanism. Something like this 
(untested!):

1   { ... }
2   type
3     TInterfaceStack = class
4     private
5       FList: TInterfacelist;
6       FCurrent: IInterface;
7       function GetTop: IInterface;
8     public
9       constructor Create; virtual;
10      destructor Destroy; override;
11      procedure Push(aIntf: IInterface);
12      procedure Pop;
13      function IsEmpty: boolean;
14      property Top: IInterface read GetTop;
15    end;
16  
17    { TInterfaceStack }
18  
19  constructor TInterfaceStack.Create;
20  begin
21    inherited;
22    FList := TInterfacelist.Create;
23    FList.Capacity := 32;
24  end;
25  
26  destructor TInterfaceStack.Destroy;
27  begin
28    FList.Free;
29    inherited;
30  end;
31  
32  function TInterfaceStack.GetTop: IInterface;
33  begin
34    Result := FCurrent;
35  end;
36  
37  function TInterfaceStack.IsEmpty: boolean;
38  begin
39    Result := not Assigned(FCurrent);
40  end;
41  
42  procedure TInterfaceStack.Pop;
43  begin
44    if Flist.Count > 0 then
45    begin
46      FCurrent := FList[FList.count - 1];
47      FList.Delete(Flist.Count - 1);
48    end
49    else
50      FCurrent := nil;
51  end;
52  
53  procedure TInterfaceStack.Push(aIntf: IInterface);
54  begin
55    if not IsEmpty then
56      FList.Add(FCurrent);
57    FCurrent := aIntf;
58  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC