Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-12

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

3 messages · 1 person ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

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

Reply by email

silvioprog
to silvioprog

Oops...

On Fri, Dec 14, 2018 at 3:35 AM silvioprog wrote: [...]

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

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

Anyway, still a very ugly workaround.

Silvio Clécio

Reply by email

silvioprog
to 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

Reply by email