AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren

Wieder generische Functions results

Ein Thema von QuickAndDirty · begonnen am 14. Mai 2019 · letzter Beitrag vom 15. Mai 2019
Antwort Antwort
Seite 2 von 2     12
QuickAndDirty

Registriert seit: 13. Jan 2004
Ort: Hamm(Westf)
1.882 Beiträge
 
Delphi 12 Athens
 
#11

AW: Wieder generische Functions results

  Alt 15. Mai 2019, 12:52
OK ich habs!
Kann man das irgendwie ohne RTTI hinbekommen?

Delphi-Quellcode:
Class Function TWSClient.GetClient<T>(Addr: string):T;
var RIO: THTTPRIO;
    aGuid:TGuid;
begin
  Result := nil;
  RIO := GetLocalRio;
  try
    //Result := (RIO as T);//[dcc32 Fehler] E2015 Operator ist auf diesen Operandentyp nicht anwendbar

    //Result := T(RIO);//Das geht Aber es produziert zugriffsverletzungen... dank Refcount;

    aGuid := TRttiInterfaceType(TRttiContext.Create.GetType(TypeInfo(T))).GUID ;

    if not Supports(RIO,aGuid,Result) then
      Result := nil;

    RIO.URL := Addr

  finally
    if (Result = nil) then
      RIO.Free;
  end;
End;
Andreas
Monads? Wtf are Monads?
  Mit Zitat antworten Zitat
Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#12

AW: Wieder generische Functions results

  Alt 15. Mai 2019, 12:59
So kompilierts:

Edit: Oh du hast auch grad gepostet..
Ohne RTTI fällt mir auch keine Lösung ein...

Ist auch RTTI aber mit weniger overhead vielleicht?
Delphi-Quellcode:
uses
  TypInfo;

guid := PTypeInfo(TypeInfo(T))^.TypeData^.GUID;
Delphi-Quellcode:

uses
  System.RTTI;

class function TForm3.GetClient<T>(Addr: string): T;
var RIO: THTTPRIO;
    rtti: TRttiContext;
    intfType: TRttiInterfaceType;
begin
  Result := nil;
  RIO := nil;
  try
    rtti := TRttiContext.Create;
    try
      intfType := rtti.GetType(TypeInfo(T)) as TRttiInterfaceType;
      if Supports(RIO, intfType.GUID, Result) then
        RIO.URL := Addr
      else
        Result := nil;
    finally
      rtti.Free;
    end;
  finally
    if (Result = nil) then
      RIO.Free;
  end;
end;
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."

Geändert von Neutral General (15. Mai 2019 um 13:04 Uhr)
  Mit Zitat antworten Zitat
QuickAndDirty

Registriert seit: 13. Jan 2004
Ort: Hamm(Westf)
1.882 Beiträge
 
Delphi 12 Athens
 
#13

AW: Wieder generische Functions results

  Alt 15. Mai 2019, 14:37
Ich fürchte einfach das die unit Typinfo irgendwann weg ist....
Aber danke für deine Lösung, das verschafft mir doch nochmal ein sicheres Gefühl das der Ansatz nicht zu unorthodox ist.
Andreas
Monads? Wtf are Monads?
  Mit Zitat antworten Zitat
Benutzerbild von Stevie
Stevie

Registriert seit: 12. Aug 2003
Ort: Soest
4.007 Beiträge
 
Delphi 10.1 Berlin Enterprise
 
#14

AW: Wieder generische Functions results

  Alt 15. Mai 2019, 14:48
Ich mag ja Generics aber das hier erscheint nen bisschen unnötig.

Warum nicht einfach:

Delphi-Quellcode:
function GetClient(Addr: string): IInterface;
var
  RIO: THTTPRIO;
begin
  RIO := GetLocalRio;
  RIO.URL := Addr;
  Result := RIO;
end;

if Supports(GetClient('...'), IBlaBlubb, LBlaBlubb) then
Denn mit deiner Implementierung kommst du auch um einen Check nicht drumherum (obs Result von GetClient<T> assigned ist)
Stefan
“Simplicity, carried to the extreme, becomes elegance.” Jon Franklin

Delphi Sorcery - DSharp - Spring4D - TestInsight
  Mit Zitat antworten Zitat
Benutzerbild von Neutral General
Neutral General

Registriert seit: 16. Jan 2004
Ort: Bendorf
5.219 Beiträge
 
Delphi 10.2 Tokyo Professional
 
#15

AW: Wieder generische Functions results

  Alt 15. Mai 2019, 15:38
Ich fürchte einfach das die unit Typinfo irgendwann weg ist....
Aber danke für deine Lösung, das verschafft mir doch nochmal ein sicheres Gefühl das der Ansatz nicht zu unorthodox ist.
System.RTTI und die Records/Klassen darin kapseln die System.TypInfo einfach nur.
Solange es die RTTI gibt, wird es auch die Unit TypInfo geben.
Michael
"Programmers talk about software development on weekends, vacations, and over meals not because they lack imagination,
but because their imagination reveals worlds that others cannot see."
  Mit Zitat antworten Zitat
Themen-Optionen Thema durchsuchen
Thema durchsuchen:

Erweiterte Suche
Ansicht

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 13:17 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