I don’t understand why you aren’t writing a simple type alias:
type
TMyOrdinalType = Byte;
because I would like to avoid to query type size at runtime and query at compilation time instead, something like this:
function TORDINALHELPER.HighestBitPos: cardinal;
begin
{$if SizeOf(TORDINALTYPE) = 1}
Result := BsrByte(byte(Self));
{$elseif SizeOf(TORDINALTYPE) = 2}
Result := BsrWord(word(Self));
{$elseif SizeOf(TORDINALTYPE) = 4}
Result := BsrDWord(dword(Self));
{$else} // SizeOf(TORDINALTYPE) = 8
Result := BsrQWord(qword(Self));
{$endif}
end;
For more info about TORDINALHELPER take a look at syshelpo.inc in RTL SysUtils.
workaround is not to use macros.
I have found a workaround to use one $define / $undef pair for each ordinal type size, but that code is ugly and duplicates more then needed so I am hoping for something more elegant.
May I ask you why is that?
Because from your sample it smells like C preprocessor meta-programming which is everything but elegant.
I have also said my self that it's not elegant and that's the reason why I would like to avoid it. Unfortunately {$if SizeOf(TMyOrdinalType) = 1} doesn't work although according to $IF docs I expected it to work. I discuss this case here to find out if I should file a bug report, or to find out a more elegant alternative.