Einzelnen Beitrag anzeigen

a.def
(Gast)

n/a Beiträge
 
#3

AW: Codezeile in Funktion umwandeln mit Schwierigkeiten

  Alt 16. Apr 2017, 10:49
Soweit war ich tatsächlich noch nicht. Aber mit dem ReadFromIni<T> hatte ich dann ja wohl schon recht.
Nur habe ich es wieder gelöscht, da der Editor es rot unterstreicht, obwohl Generics.Collections in der Uses-Klausel steht.

Edit
Fehlerquelle gefunden. Ich wusste nicht, dass solche Funktionen und Prozeduren eine Deklaration im Unit-Kopf verlangen.

Mein Resultat in meiner eigenen ini-Klasse
Delphi-Quellcode:
type
 TMemIniFile = class(IniFiles.TMemIniFile)
 ...
 function ReadEnum<T>(const Section: string; const Ident: TIniSettings; const Default: T): T;
 procedure WriteEnum<T>(const Section: string; const Ident: TIniSettings; Value: T);
 ...

function TMemIniFile.ReadEnum<T>(const Section: string; const Ident: TIniSettings; const Default: T): T;
begin
 Result := TEnumFunctions.GetValue<T>(ReadString(Section, TEnumFunctions.GetName(Ident), TEnumFunctions.GetName<T>(Default)));
end;

procedure TMemIniFile.WriteEnum<T>(const Section: string; const Ident: TIniSettings; Value: T);
begin
 WriteString(Section, TEnumFunctions.GetName(Ident), TEnumFunctions.GetName<T>(Value));
end;
TEnumFunctions
Delphi-Quellcode:
type
 TEnumFunctions = record
  ...
  class function GetName<T>(AValue: T): string; static;
  class function GetValue<T>(AValue: string): T; static;
  ...

class function TEnumFunctions.GetName<T>(AValue: T): string;
begin
 Result := System.Rtti.TRttiEnumerationType.GetName(AValue);
end;

class function TEnumFunctions.GetValue<T>(AValue: string): T;
var
 Temp: Integer;
 PTemp: Pointer;
begin
 Temp := GetEnumValue(TypeInfo(T), AValue);
 if Temp < 0 then
  Temp := 0; // gebe 0 statt 255 zurück im Falle, dass AValue nicht existiert
 PTemp := @Temp;
 Result := T(PTemp^);

 // Result := System.Rtti.TRttiEnumerationType.GetValue<T>(AValue);
end;

Geändert von a.def (16. Apr 2017 um 13:49 Uhr)
  Mit Zitat antworten Zitat