Porting a packed variant record with bit fields
Posted on 2006-11-09
We've got a program to port which heavily uses a record with variant parts looking something like this:
TData = packed record
case integer of
1: (sword : word);
2: (byte0 : byte; byte1: byte);
3: (bits10_0 : 0..1023; bits 6_10 : 0..63);
In other words, the record is supposed to store a single word, but there are different methods of accessing that word - or parts of that word. Delphi creates this record as a word for each entry (SizeOf returns 16). Setting mydata.sword to $1234 and then reading mydata.byte0 returns 0 instead of $34.
My immediate reaction was to create a class with properties called bits10_0 and Getbits10_0 and Setbits10_0 as that would involve the minimum of changes. But, of course, classes are not variables like records are.
The only thing I can think of is to change every single read and write of these data items with a function or procedure instead. There are potentially 1000 of these - and a find/replace won't work. Is there a better and easier solution?