How to save/load Set of Types?

后端 未结 9 1334
傲寒
傲寒 2020-12-06 00:47

I have this code

type
  TXSample = (xsType1, xsType2, xsType3, xsType4, xsType5, xsType6, xsType6, xsTyp7, xsType8); // up to FXSample30;  
..

private
  FX         


        
9条回答
  •  盖世英雄少女心
    2020-12-06 01:33

    Or we can make compiler forget about the types completly and then define what it should see (in case we know in compile-time what it sould see). This solution is so awful as it can be written on just one line.

    type
      // Controls.TCMMouseWheel relies on TShiftState not exceeding 2 bytes in size 
      TShiftState = set of (ssShift, ssAlt, ssCtrl,
                            ssLeft, ssRight, ssMiddle, 
                            ssDouble, ssTouch, ssPen, 
                            ssCommand, ssHorizontal); 
    
    var 
      Shifts : TShiftState;
      Value :  Integer;
    begin
      Shifts := TShiftState((Pointer(@Value))^):
    
      Value  := (PInteger(@Shifts))^;
    
      if ssShift in TShiftState((Pointer(@Value))^) then 
         Exit;
    end;
    

    It happens that unused (top) bits are set (or not) but it has no influence on set operations (in, =, +, -, * .. ).

    This line in Delphi:

    Shifts := TShiftState((Pointer(@Value))^);
    

    is like this in Assembler (Delphi XE6):

    lea eax,[ebp-$0c]
    mov ax,[eax]
    mov [ebp-$06],ax
    

    On Delphi 2007 (where is TShiftState is smaller so Byte can be used) this Assembler:

    movzx eax,[esi]
    mov [ebp-$01],al
    

提交回复
热议问题