AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

TList / Interfaces

Ein Thema von ConnorMcLeod · begonnen am 19. Jan 2023 · letzter Beitrag vom 20. Jan 2023
Antwort Antwort
Benutzerbild von ConnorMcLeod
ConnorMcLeod

Registriert seit: 13. Okt 2010
Ort: Bayern
490 Beiträge
 
Delphi 10.4 Sydney
 
#1

TList / Interfaces

  Alt 19. Jan 2023, 21:54
Kommt man da tatsächlich nicht weiter oder seh ich es einfach nicht?
Bitte keine guten Ratschläge zu TList/IList<T>/Spring4D und so weiter weil es sich um eine nicht diskutierbare Vorgabe handelt TList zu verwenden

Es sei ein Interface
Delphi-Quellcode:
  ISimpleInterface = interface
    function GetID: Integer;
    procedure SetID(AValue: Integer);
    property ID: Integer read GetID write SetID;
  end;
und eine Liste von diesen Dingern:
Delphi-Quellcode:
  TSimpleIntfList = class(TList<ISimpleInterface>)
  public
    function ItemByID(AiID: Integer): ISimpleInterface;
  end;
Eine Erweiterung vom Interface:
Delphi-Quellcode:
  IModule = interface(ISimpleInterface)
    function GetModNo: Integer;
    procedure SetModNo(AValue: Integer);
    property ModNo: Integer read GetModNo write SetModNo;
  end;
Nun hätte ich gerne eine Ableitung von TSimpleIntfList, also
Delphi-Quellcode:
  TModuleList = class(TSimpleInterfaceList<IModule>) // funkt nicht - schon klar
    function ModuleByModNo(AiModNo: Integer): IModule;
  end;
Aber ItemByID sollte trotzdem noch funktionieren ...
Geht das?
Nr.1 Delphi-Tool: [F7]
  Mit Zitat antworten Zitat
Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.132 Beiträge
 
Delphi 12 Athens
 
#2

AW: TList / Interfaces

  Alt 19. Jan 2023, 22:17
Nja wenn es ginge, dann höchstens so
Delphi-Quellcode:
 TSimpleIntfList<T> = class(TList<T>)
  public
    function ItemByID(AiID: Integer): T;
  end;
oder
Delphi-Quellcode:
 TSimpleIntfList<T> = class(TList<ISimpleInterface>)
  public
    function ItemByID(AiID: Integer): T;
  end;
hier im ItemByID dann ein cast von ISimpleInterface zu T (IModule) , weil die Liste natürlich ISimpleInterface speichert, aber auch den Nachfahren aufnehmen kann.
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests

Geändert von himitsu (19. Jan 2023 um 22:21 Uhr)
  Mit Zitat antworten Zitat
Benutzerbild von ConnorMcLeod
ConnorMcLeod

Registriert seit: 13. Okt 2010
Ort: Bayern
490 Beiträge
 
Delphi 10.4 Sydney
 
#3

AW: TList / Interfaces

  Alt 19. Jan 2023, 23:18
Mmh, Zweiteres bringt mich auf eine Idee. Das probier ich gleich nach dem Aufstehen, danke!
Nr.1 Delphi-Tool: [F7]
  Mit Zitat antworten Zitat
Benutzerbild von ConnorMcLeod
ConnorMcLeod

Registriert seit: 13. Okt 2010
Ort: Bayern
490 Beiträge
 
Delphi 10.4 Sydney
 
#4

AW: TList / Interfaces

  Alt 20. Jan 2023, 04:23
Meine Lösung:
Delphi-Quellcode:
  ISimpleInterface = interface
    function GetID: Integer;
    procedure SetID(AValue: Integer);
    property ID: Integer read GetID write SetID;
  end;
  IModule = interface(ISimpleInterface)
    function GetModNo: Integer;
    procedure SetModNo(AValue: Integer);
    property ModNo: Integer read GetModNo write SetModNo;
  end;
die Liste von diesen Dingern:
Delphi-Quellcode:
  TSimpleIntfList = class(TList<ISimpleInterface>)
  public
    function SimpleItemByID(AiID: Integer): ISimpleInterface;
  end;
eine generische Liste von (beinahe) beliebigem Nachfahrtyp:
Delphi-Quellcode:
  TCustomIntfList<T: ISimpleInterface> = class(TSimpleIntfList)
  strict private
    FrGuid: TGUID;
  public
    constructor Create;
  public
    function TypedItemAtIndex(AiIndex: Integer): T;
  end;
  
uses
    System.SysUtils
  , System.TypInfo
  ;
  
constructor TNntCustomIntfList<T>.Create;
begin
  inherited Create;
  FrGuid := GetTypeData(TypeInfo(T))^.Guid;
end;

function TCustomIntfList<T>.TypedItemAtIndex(AiIndex: Integer): T;
var
  LoItem: ISimpleInterface;
begin
  LoItem := Items[AiIndex];
  Supports(LoItem, FrGuid, Result);
end;
Eine konkrete Nachfahrliste:
Delphi-Quellcode:
  TModuleList = class(TCustomIntfList<IModule>)
    function ModuleByModNo(AiModNo: Integer): IModule;
  end;
  
function ModuleByModNo(AiModNo: Integer): IModule;
var
  LoItem : ISimpleInterface;
  LoModule: IModule;
begin
  for LoItem in Self do begin
    if Supports(LoItem, IModule, LoModule) then begin
      if LoModule.ModNo = AiModNo then begin
        Exit(LoModule);
      end;
    end;
  end;
  Result := nil;
end;
Nr.1 Delphi-Tool: [F7]

Geändert von ConnorMcLeod (20. Jan 2023 um 05:30 Uhr)
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 00:33 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz