For my link archive in case I ever need to do Delphi generic type matching on intrinsic types. This will be tricky as you can have typed types like [WayBack] type TDate = type TDateTime
since the early Delphi ages.
[WayBack] Hi, by using compiler intrinsics, is it possible to check if a generic type parameter is an unsigned integer? – Malcon X Portela – Google+
It will probably come down to fast intrinsic type mapping and slower typed type mapping.
The above WayBack link contains the most important bits of the Google+ post:
Hi, by using compiler intrinsics, is it possible to check if a generic type parameter is an unsigned integer? I have the following code:
function TChecker<T>.CheckIsUnsigned: Boolean;
begin
if GetTypeKind(T) = tkInteger then
begin
if SizeOf(T) = 4 then
begin
// TODO: Check if it is an unsigned 32-bit integer
Result := True;
end else if SizeOf(T) = 2 then
begin
// TODO: Check if it is an unsigned 16-bit integer
Result := True;
end else
begin
// TODO: Check if it is an unsigned 8-bit integer
Result := True;
end;
end else
begin
Result := False;
end;
end;
The code should return True only if the ‘T’ generic type parameter is an unsigned integer. I remember that +Stefan Glienke posted here some code that can do this trick, however, I am not able to find that now.
Thanks!
Hi +Jeroen Wiert Pluimers, my answer can be a bit disappointing, sorry :( Some time after writing this question here, I just realized which for the things I was originally trying to do, being signed or unsigned would not change anything on the final result hehehehe :D But from this amazing project https://github.com/d-mozulyov/Rapid.Generics (entire credits goes to Dmitry Mozulyov for the magic), it is possible to write the check to the generic type parameter in order to identify if it is a 32-bit/64-bit signed or unsigned number.
LTypeData := PTypeInfo(TypeInfo(T)).TypeData;
case GetTypeKind(T) of
tkInteger:
begin
{case LTypeData.OrdType of
otSLong: Writeln('32-bit signed');
otULong: Writeln('32-bit unsigned');
end;}
// the above code does the same thing
if LTypeData.MaxValue > LTypeData.MinValue then
begin
Writeln('32-bit signed');
end else
begin
Writeln('32-bit unsigned');
end;
end;
tkInt64:
begin
if LTypeData.MaxInt64Value > LTypeData.MinInt64Value then
begin
Writeln('64-bit signed');
end else
begin
Writeln('64-bit unsigned');
end;
end;
end;
The [WayBack] Rapid.Generics project is indeed cool, but unmaintained and has no unit tests. The main code is in some [Wayback] 30k lines Rapid.Generics.pas
unit with some cool ideas and implementations. Hopefully people will find time to integrate some of the ideas there into basically the only well maintained Delphi generics library Spring4D.
A big limitation might be that the code is Delphi XE8 and up only, whereas Spring4D supports older Delphi versions.
–jeroen
Like this:
Like Loading...