Thema: Delphi Quelltext Optimierung

Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#4

AW: Quelltext Optimierung

  Alt 16. Mai 2013, 19:32
Delphi-Quellcode:
Type
  TDecodeProc = Procedure (Dump : TDump);
  
Const
  DecodeProc : Array [1..N] of TDecodeProc = (
   DecodeProc1, DecodeProc2...
...
);

Procedure CallIt;
Begin
  DecodeProc[Dmi0.Header.Type](Dump);
End;
So könnte das gehen.
Oder (ein wenig flexibler, was die Nummerierung und Erweiterung betrifft)
Delphi-Quellcode:
unit DecodeTable;

interface

uses
  Generics.Collections;

type
  TDump = class

  end;

  TDecodeProc = procedure( Dump : TDump );

  TDecodeTable = class
  private
    class var CFDecoderDict : TDictionary<Byte, TDecodeProc>;
  protected
    class constructor Create;
    class destructor Destroy;
  public
    class procedure RegisterDecoder( AType : Byte; ADecoder : TDecodeProc );
    class function Decode( AType : Byte ) : TDecodeProc; overload;
    class procedure Decode( AType : Byte; ADump : TDump ); overload;
  end;

implementation

{ TDecodeTable }

class constructor TDecodeTable.Create;
begin
  CFDecoderDict := TDictionary<Byte, TDecodeProc>.Create;
end;

class function TDecodeTable.Decode( AType : Byte ) : TDecodeProc;
begin
  Result := CFDecoderDict[AType];
end;

class procedure TDecodeTable.Decode( AType : Byte; ADump : TDump );
begin
  Decode( AType )( ADump );
end;

class destructor TDecodeTable.Destroy;
begin
  CFDecoderDict.Free;
end;

class procedure TDecodeTable.RegisterDecoder( AType : Byte; ADecoder : TDecodeProc );
begin
  CFDecoderDict.AddOrSetValue( AType, ADecoder );
end;

end.
Jetzt ein paar Units mit den Prozeduren
Delphi-Quellcode:
unit DecodeA;

interface

uses
  DecodeTable;

implementation

procedure DecodeTable0( Dump : TDump );
begin

end;

initialization

TDecodeTable.RegisterDecoder( 0, DecodeTable0 );

end.
und aufrufen
Delphi-Quellcode:
var
  MyDump : TDump;

// entweder
TDecodeTable.Decode( 0 )( MyDump );
// oder
TDecodeTable.Decode( 0, MyDump );
Ach ja, den direkten Aufruf der Prozeduren kann man so auch ganz nett unterbinden
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat