Re[2]: [fpc-pascal] variant part of a record

Пётр Косаревский ppkk at mail.ru
Wed Sep 20 10:26:10 CEST 2006


> Vinzent H?fler> Probably you should used a "packed record", though. And it's in no  
> Vinzent H?fler> way portable, of course.
> Also correct.
> Jonas

I use

{$A-}
abc= record
     something: byte;
      case byte of
       1: (a,b: byte);
       2: (c: word);
     end;
{$A+} // or even  a: byte; rsrvd1: array[1..3] of byte; b: word; ...
{$IF sizeof(abc)<>1234}
{$FATAL Неверный размер записи для заголовка (sizeof(abc)<>1234).} // I think, there is no need to translate:)
{$ENDIF}

because I read binaries generated by other programs. So it's mostly portable: if it was supposed to be really portable, I'd add endianness checking.

What am I thinking of is a construction like

xyz= object
     raw: abc;

     function GetA: byte;
     function GetB: byte;
     function GetC: word;

     property something: byte READ raw.something;
     property a: byte READ GetA;
     property b: byte READ GetB;
     property c: word READ GetC;

     procedure GetData(...); // obtain data for the first time, writes to the 'raw'
     end;

function xyz.GetA: byte;
  begin if something=1 then GetA:= raw.a else Show_Fatal_Error_Message(Fatal_Error_ID); end;

function xyz.GetB: byte;
  begin if something=1 then GetB:= raw.b else Show_Fatal_Error_Message(Fatal_Error_ID); end;

function xyz.GetC: word;
  begin if something=2 then GetC:= raw.c else Show_Fatal_Error_Message(Fatal_Error_ID); end;


But it's somewhat bulky (in the case of many types), so I hoped there is a language feature for my convenience (I don't know pascal very well and didn't understand corresponding documentation in the one way).



More information about the fpc-pascal mailing list