![]() |
AW: TObjectList als Result
Zitat:
|
AW: TObjectList als Result
Zitat:
Eine ![]()
Delphi-Quellcode:
.
IInterface
Eine
Delphi-Quellcode:
soll für Items vom Typ
TInterfacedObjectList
Delphi-Quellcode:
sein.
TObject
|
AW: TObjectList als Result
Zitat:
|
AW: TObjectList als Result
Zitat:
Nur dass sich diese bekannte
Delphi-Quellcode:
als Rückgabewert einer Funktion etwas ungeschickt ist (wegen der evtl. zu erwartenden Speicherlecks). Darum der (gute) Vorschlag von Stevie statt einer
TObjectList
Delphi-Quellcode:
ein
TObjectList
Delphi-Quellcode:
zurückzugeben, was die gleichen Eigenschaften, wie
Interface
Delphi-Quellcode:
hat, sich aber (weil es ja ein
TObjectList
Delphi-Quellcode:
ist) von selber wieder aus dem Speicher entfernt, sobald da niemand mehr den Finger drauf hat.
Interface
Da hinter einem
Delphi-Quellcode:
immer (mindestens) eine konkrete Klasse steht war der Vorschlag diese Klasse einfach
Interface
Delphi-Quellcode:
zu nennen.
TInterfacedObjectList
|
AW: TObjectList als Result
// Edit
Jetzt hat es bei mir Klick gemacht! Es geht darum einen Wrapper um TObjectList zu stricken, der per Interface ansprechbar ist. Der Weg, eine Classes.IInterfaceList zu benutzen und die betreffenden Klassen mit einen Interface zu versehen ginge aber auch. Wäre am Ende sogar weniger Code. |
AW: TObjectList als Result
Zitat:
Delphi-Quellcode:
Und dann im seinem Quelltext
unit Unit1;
interface uses Contnrs, Classes; type IObjectList = interface ['{056BC23C-2F12-40DF-A0F7-2A9AE55ADADB}'] function Add( AObject: TObject ): Integer; function Extract( Item: TObject ): TObject; function ExtractItem( Item: TObject; Direction: TList.TDirection ): TObject; function Remove( AObject: TObject ): Integer; overload; function RemoveItem( AObject: TObject; ADirection: TList.TDirection ): Integer; function IndexOf( AObject: TObject ): Integer; function IndexOfItem( AObject: TObject; ADirection: TList.TDirection ): Integer; function FindInstanceOf( AClass: TClass; AExact: Boolean = True; AStartAt: Integer = 0 ): Integer; procedure Insert( Index: Integer; AObject: TObject ); function First: TObject; function Last: TObject; function GetOwnsObjects: Boolean; procedure SetOwnsObjects( const Value: Boolean ); property OwnsObjects: Boolean read GetOwnsObjects write SetOwnsObjects; function GetItem( Index: Integer ): TObject; procedure SetItem( Index: Integer; AObject: TObject ); property Items[Index: Integer]: TObject read GetItem write SetItem; default; end; TInterfacedObjectList = class( TObjectList, IInterface, IObjectList ) private // IInterface FRefCount: Integer; function QueryInterface( const IID: TGUID; out Obj ): HRESULT; stdcall; function _AddRef: Integer; stdcall; function _Release: Integer; stdcall; private // IObjectList function GetOwnsObjects: Boolean; procedure SetOwnsObjects( const Value: Boolean ); end; implementation { TInterfacedObjectList } function TInterfacedObjectList.GetOwnsObjects: Boolean; begin Result := OwnsObjects; end; function TInterfacedObjectList.QueryInterface( const IID: TGUID; out Obj ): HRESULT; begin if GetInterface( IID, Obj ) then Result := 0 else Result := E_NOINTERFACE; end; procedure TInterfacedObjectList.SetOwnsObjects( const Value: Boolean ); begin OwnsObjects := Value; end; function TInterfacedObjectList._AddRef: Integer; begin Result := AtomicIncrement( FRefCount ); end; function TInterfacedObjectList._Release: Integer; begin Result := AtomicDecrement( FRefCount ); if Result = 0 then Destroy; end; end.
Delphi-Quellcode:
function TForm1.imageloader: IObjectList;
begin Result := TInterfacedObjectList.Create( False ); Result.Add(Image1); Result.Add(Image2); Result.Add(Image3); Result.Add(Image4); Result.Add(Image5); Result.Add(Image6); Result.Add(Image7); end; procedure TForm1.Button1Click(Sender: TObject); var MyList: IObjectlist; counter : integer; begin Mylist := ImageLoader; // Jetzt Interface, jetzt keine Speicherlecks mehr Allrounder := TImage(Imageloader.Items[5]); // <- da wird schon wieder eine erzeugt!!!, aber ist ja JETZT kein Problem mehr Allrounder.Picture.LoadFromFile('Unbenannt.jpg'); end; |
AW: TObjectList als Result
Zitat:
|
AW: TObjectList als Result
Wären dann aber nicht auch anschließend die TImages weg? Ich tendiere in diesem Fall eher zu himitsus Vorschlag mit dem dynamischen Array, das erfordert weniger Aufwand und sollte vollkommen ausreichend sein.
|
AW: TObjectList als Result
Zitat:
Delphi-Quellcode:
Das mit den Arrays kommt auf den Einsatzzweck an (hier bestimmt völlig ausreichend). Es macht aber nichts, so eine Interface-ObjectList in der Hinterhand zu haben, vor allem, wenn man die LifeTime der Elemente damit verwalten will. Keiner mehr Interesse an der Liste, dann alles ab in die Tonne :)
Result := TInterfacedObjectList.Create(
{OwnsObjects} False ); |
AW: TObjectList als Result
Schon lustig, dieser Thread :lol:
Der TE stellt eine Frage. Was er mit diesem Code will, ist garnicht wirklich klar. Er hat auch keine Antwort geschrieben. Es wird hier aber trotzdem weiter philosophiert ;-) Wie Ihr auf den ganzen Interface-Kram kommt, kann ich nicht wirklich nachvollziehen. Ist zwar interessant, hat aber mit dem Problem des TE nichts zu tun. Oder? Edit: Bis Beitrag #7 war es noch themenbezogen. |
Alle Zeitangaben in WEZ +1. Es ist jetzt 09:31 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz