As a rule, I went around a lot of the classic design traps when using pointers using Const options (untyped), rather than hard-coded types. This gives me an edge in performing advanced graphics functions, leaving the technical details to the compiler. It also simplified the use of the same code in Delphi and Free Pascal with minimal changes. Recently, however, I began to question this because of the remarks of Embarcadero vauge about the evolution of Delphi and its dynamic security model.
For example, consider the following example:
Type TSomeDataProc = procedure (const aInput;var aOutput) of Object; (* Convert 8-bit pixel to 16-bit pixel *) Procedure TMyClass.ProcessSomeData08x565(Const aInput;var aOutput); var r,g,b: Byte; Begin FPalette.ExportTriplets(Byte(aInput),r,g,b); Word(aOutput):=(R SHR 3) SHL 11 or (G SHR 2) SHL 5 or (B SHR 3); End; (* Convert 16-bit pixel to 24-bit pixel *) Procedure TMyClass.ProcessSomeData565x888(Const aInput;var aOutput); Begin With TRGBTriple(aOutput) do Begin rgbtRed:=(((word(aInput) and $F800) shr 11) shl 3); rgbtGreen:= (((word(aInput) and $07E0) shr 5) shl 2); rgbtBlue:= ((word(aInput) and $001f) shl 3); end; End;
Now we have two procedures with identical declarations, but they handle pixeldata in very different ways. This gives us the advantage of using a lookup table to get the correct converter method. This should be done either in the constructor or where the raster image of the image is selected, for example:
Private FLookup: Array[pf8bit..pf32bit,pf8bit..pf32bit] of TSomeDataProc; Procedure TMyClass.Create; Begin Inherited; FLookup[pf8bit,pf16bit]:=ProcessSomeData08x565; FLookup[pf16bit,pf24Bit]:=ProcessSomeData565x888; end;
Whenever we need to convert pixels, we simply scan the correct method and use it. The syntax for all procedures remains unchanged - so we donβt need to worry about how each procedure works. As for our class, they all look the same.
Procedure TMyClass.ConvertTo(aFormat:TpixelFormat); Begin // Get function for the correct pixel converter FConvertProc:=FLookup[CurrentFormat,aFormat]; //Use the pixel converter FConvertProc(GetSourcePixelAddr(x,y),GetTargetPixelAddr(x,y)); end;
Question: Will this type of typginging (for example: Const by Byte or any particular type of record) survive up to 64 bits? I personally cannot understand why not, but Embarcadero was a little vague regarding the new security model and the use of pointers, so it's a little difficult for me to protect my code for the future.