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!
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!
View 5 previous comments
- Thank you very much +Paul TOTH for clarifying your previous comment. The compiler instrics usage can looks bad in an OOP perspective :)5d
- Thank you very much +Stefan Glienke. Very fast and amazing explanation :)5d
- The TTypeData type (from TypInfo unit) have the MinValue and MaxValue properties for tkInteger kind5d
- Thank you very much +Mike Shkolnik. I am going to try that as well :)5d
- +Malcon X Portela which solutions did you end up using?22h
- 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;15h - +Malcon X Portela no problem. Thanks for sharing.10h