[fpc-pascal] variant part of a record

Vinzent Hoefler JeLlyFish.software at gmx.net
Wed Sep 20 12:41:14 CEST 2006

On Wednesday 20 September 2006 08:26, Пётр Косаревский wrote:

> 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.

You should also realign the data to their native boundaries then. :) 
Some processors just don't like data being accessed the wrong way, so 
either you trust fpc to workaround for you (and this could be rather 
slow) or you accept that your program may raise SIGBUS or similar 
processor exceptions on certain architectures.

> 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;

I don't know if it is possible to define a property accessing only a 
part of a structure, and I'm too lazy to check it, so maybe you have to 
use a function like "GetSomething" instead. But apart from that I don't 
understand your problem with the Pascal syntax of discrimants in a 
variant record. It would work precisely as you expect, even if the 
syntax is a little bit different than your expectation about it are.

> 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).

Yes, there is a language feature called object orientation. But still 
that means you would have to translate the binary structure into a 
class instance somehow.


More information about the fpc-pascal mailing list