![]() |
Delphi-Version: 2010
Quelltext Optimierung
bekomm ich das Irgendwie Kleiner geregelt?
DecodeTableX sind dabei Funktionen in verschiedenen Units
Delphi-Quellcode:
case Dmi0.Header.Type_ of
// BIOS Information (Type 0) 0: DecodeTable0(Dump); // System Information (Type 1) 1: DecodeTable1(Dump); // Baseboard (or Module) Information (Type 2) 2: DecodeTable2(Dump); // System Enclosure or Chassis (Type 3) 3: DecodeTable3(Dump); // Processor Information (Type 4) 4: DecodeTable4(Dump); // Cache Information (Type 7) 7: DecodeTable7(Dump); // Port Connector Information (Type 8) 8: DecodeTable8(Dump); // System Slots (Type 9) 9: DecodeTable9(Dump); // BIOS Language Information (Type 10) 10: DecodeTable9(Dump); // OEM Strings (Type 11) 11: DecodeTable11(Dump); // BIOS Language Information (Type 13) 13: DecodeTable13(Dump); // Memory Device (Type 17) 17: DecodeTable17(Dump); // 32-Bit Memory Error Information (Type 18) 18: DecodeTable18(Dump); // Portable Battery (Type 22) 22: DecodeTable22(Dump); // Hardware Security (Type 24) 24: DecodeTable24(Dump); // Cooling Device (Type 26) 26: DecodeTable26(Dump); // Cooling Device (Type 27) 27: DecodeTable27(Dump); // Cooling Device (Type 28) 28: DecodeTable28(Dump); // Electrical Current Probe (Type 29) 29: DecodeTable29(Dump); // System Boot Information (Type 32) 32: DecodeTable32(Dump); // 64-Bit Memory Error Information (Type 33) 33: DecodeTable33(Dump); // Management Device (Type 34) 34: DecodeTable34(Dump); // Management Device (Type 39) 39: DecodeTable39(Dump); // Onboard Devices Extended Information (Type 41) 41: DecodeTable41(Dump); // Inactive (Type 126) Structure 126: //ShowMessage('Inactive Table ' + IntToStr(Dmi0.Header.Length)) end; |
AW: Quelltext Optimierung
Delphi-Quellcode:
So könnte das gehen.
Type
TDecodeProc = Procedure (Dump : TDump); Const DecodeProc : Array [1..N] of TDecodeProc = ( DecodeProc1, DecodeProc2... ... ); Procedure CallIt; Begin DecodeProc[Dmi0.Header.Type](Dump); End; |
AW: Quelltext Optimierung
Du kannst den Header-Type und den Zeiger auf die Dump-Funktion in einem Record ablegen.
Delphi-Quellcode:
In deiner Aufgabenverteilerfunktion gibt es dann ein Konstantenarray:
type
TDumpProc = procedure(dump:String); TDumpRecord = record htype : Integer; p : TDumpProc; end;
Delphi-Quellcode:
In einer Schleife suchst du den passenden htype raus und rufst die Procedur über den Zeiger auf.
const
dumplist:array[0..20] of TDumpRecord = ( (htype:0; p;DecodeTable0), (htype:1; p;DecodeTable1), ); begin Das lohnt aber nur, wenn du sehr viele Prozeduren hast. |
AW: Quelltext Optimierung
Zitat:
Delphi-Quellcode:
Jetzt ein paar Units mit den Prozeduren
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.
Delphi-Quellcode:
und aufrufen
unit DecodeA;
interface uses DecodeTable; implementation procedure DecodeTable0( Dump : TDump ); begin end; initialization TDecodeTable.RegisterDecoder( 0, DecodeTable0 ); end.
Delphi-Quellcode:
Ach ja, den direkten Aufruf der Prozeduren kann man so auch ganz nett unterbinden :)
var
MyDump : TDump; // entweder TDecodeTable.Decode( 0 )( MyDump ); // oder TDecodeTable.Decode( 0, MyDump ); |
Alle Zeitangaben in WEZ +1. Es ist jetzt 12:13 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