How do I take the float type of a field from a record?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

How do I take the float type of a field from a record?

silvioprog
Hi.

Please consider the following code:

uses TypInfo;

type
  TTestRecord = record
    Member1: LongInt;
    Member2: Double;
    Member3: string;
    Member4: Extended;
  end;

var
  F: PManagedField;
  R: TTestRecord;
  T, T2: PTypeData;
  I: Integer;
begin
  R := Default(TTestRecord);
  T := GetTypeData(TypeInfo(R));
  F := PManagedField(PByte(@T^.TotalFieldCount) + 4);
  T2 := GetTypeData(F^.TypeRef);
  for I := 0 to Pred(T^.TotalFieldCount) do
  begin
    if F^.TypeRef^.Kind = tkFloat then
      WriteLn(F^.TypeRef^.Kind, ':', T2^.FloatType);
    Inc(F);
  end;
end.

it prints:

tkFloat:ftCurr
tkFloat:ftCurr

is there any chance to print it as below using FloatType?:

tkFloat:ftDouble
tkFloat:ftExtended

I solved it temporally using the following workaround (removing the last four chars when the floating type is "Currency"):

WriteLn(F^.TypeRef^.Kind, ':tk', F^.TypeRef^.Name);

but I really would like to get the ordinal item instead of its name, avoiding string handling.

Thank you!

--
Silvio Clécio

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How do I take the float type of a field from a record?

silvioprog
Oops...

On Fri, Dec 14, 2018 at 3:35 AM silvioprog <[hidden email]> wrote:
[...]
WriteLn(F^.TypeRef^.Kind, ':tk', F^.TypeRef^.Name);

I meant "':ft', F^.TypeRef^.Name);".

Anyway, still a very ugly workaround.

--
Silvio Clécio

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: How do I take the float type of a field from a record?

silvioprog
Oh, please ignore and sorry for noise. I found the solution as soon as I sent the e-mail. -.-

var
  F: PManagedField;
  R: TTestRecord;
  T, T2: PTypeData;
  I: Integer;
begin
  R := Default(TTestRecord);
  T := GetTypeData(TypeInfo(R));
  F := PManagedField(PByte(@T^.TotalFieldCount) + 4);
  for I := 0 to Pred(T^.TotalFieldCount) do
  begin
    if F^.TypeRef^.Kind = tkFloat then
    begin
      T2 := GetTypeData(F^.TypeRef);
      WriteLn(F^.TypeRef^.Kind, ':', T2^.FloatType);
    end;
    Inc(F);
  end;
end;

time to go to bed.

--
Silvio Clécio

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal