RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TypInfo.TFloatType Enumeration

Enumerates floating point number types in the TTypeData record.

Pascal
TFloatType = (
  ftSingle,
  ftDouble,
  ftExtended,
  ftComp,
  ftCurr
);
C++
enum TFloatType {
  ftSingle,
  ftDouble,
  ftExtended,
  ftComp,
  ftCurr
};

The GetTypeData routine utilises Delphi's RTTI (Run Time Type Information) to return a pointer to TTypeData record that describes a component property data type.  

TTypeData is a variant record : the layout depends on the property TTypeKind value. When it is tkFloat, the data is one of the TFloatType enumerated values.  

The TFloatType value meanings are as follows :

Value 
Meaning 
ftSingle  
Single data type. (-1.5 x 10^45 .. 3.4 x 10^38)  
ftDouble  
Double data type. (-5.0 x 10^324 .. 1.7 x 10^308)  
ftExtended  
Extended data type. (-3.6 x 10^4951 .. 1.1 x 10^4932)  
ftComp  
Comp data type. (-2^63+1 .. 2^63-1)  
ftCurr  
Currency data type. (20 significant digits, 4 decimal places)  

 

C++ Examples: 

/*
Add the Delphi source file that appears on this Help page into
a CPP Builder project that includes a CPP module containing the
following code.  An hpp file will be generated for the Delphi
code when you build the project.  Add the include line for
that hpp file at the top of the CPP module.  The FormCreates
for both forms will execute and the following generics code
will work.  Remember to give the forms different names!
*/

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  // Prints type info for string type
  TGenericClass__1<System::UnicodeString> *GString = new TGenericClass__1<System::UnicodeString>();
  GString->PrintTypeInfo(Memo1);

  // Prints type info for Byte type
  TGenericClass__1<Byte> *GByte = new TGenericClass__1<Byte>();
  GByte->PrintTypeInfo(Memo1);

  // Prints type info for Double type
  TGenericClass__1<Double> *GDouble = new TGenericClass__1<Double>();
  GDouble->PrintTypeInfo(Memo1);

  // Prints type info for "array of String" type
  // TGenericClass__1<array of string> *GStringArray = new TGenericClass__1<array of string>();
  // GStringArray->PrintTypeInfo(Memo1);
}

 

Delphi Examples: 

{
This example demostrates the methods to retreive of
Run-time Type Information (RTTI) information.
This example will print useful information about any given
type including it's name, size, kind and sub-kind.
}

type
  TForm2 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
{ Defining a generic type that contains a single class (static)
  method that will print information about the type it operates on.
}
  TGenericClass<T> = class(TList<T>)  // TList is a class in Generics.Collections
  public
    procedure PrintTypeInfo(memo: TMemo);
  end;
  TGenericString = class(TGenericClass<UnicodeString>)
  end;
  TGenericByte = class(TGenericClass<Byte>)
  end;
  TGenericDouble = class(TGenericClass<Double>)
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

{ Method defintition }
procedure TGenericClass<T>.PrintTypeInfo(memo: TMemo);
var
  Info     : PTypeInfo;
  Data     : PTypeData;
  KindName : String;
  SubName  : String;
begin
  { Get type info for the "yet unknown type" }
  Info := System.TypeInfo(T);

  { There is no RTTI attached for some types like Records for example. }
  if Info <> nil then
  begin
    memo.Lines.Add('Type name: ' + Info^.Name);

    { Find out the name of an enum item from it's ordinal value }
    KindName := TypInfo.GetEnumName(System.TypeInfo(TTypeKind), Ord(Info^.Kind));

    memo.Lines.Add('Type kind: ' + KindName);

    Data := GetTypeData(Info);

    if Info^.Kind = tkInteger then
    begin
      { In case of integer let's see the actual sub-type name }
      SubName := TypInfo.GetEnumName(System.TypeInfo(TOrdType), Ord(Data^.OrdType));
      memo.Lines.Add('Integer kind: ' + SubName);
    end;

    if Info^.Kind = tkFloat then
    begin
      { In case of float let's see the actual sub-type name }
      SubName := TypInfo.GetEnumName(System.TypeInfo(TFloatType), Ord(Data^.FloatType));
      memo.Lines.Add('Float kind: ' + SubName);
    end;

    if Info^.Kind = tkDynArray then
    begin
      { Let's check out the element size  }
      memo.Lines.Add('Array element type name: ' + Data^.elType^^.Name);
      memo.Lines.Add('Array element type size: ' + IntToStr(Data^.elSize));
    end;

  end;
  memo.Lines.Add('Size of type: ' + IntToStr(SizeOf(T)));
end;


procedure TForm2.FormCreate(Sender: TObject);
var
  GenericString : TGenericClass<UnicodeString>;
  GenericByte : TGenericClass<Byte>;
  GenericDouble : TGenericClass<Double>;
//  GenericStringArray : TGenericClass<array of string>;
begin
  { Prints type info for string type}

  GenericString := TGenericClass<UnicodeString>.Create;
  GenericString.PrintTypeInfo(Memo1);

  { Prints type info for Byte type}
  GenericByte := TGenericClass<Byte>.Create;
  GenericByte.PrintTypeInfo(Memo1);

  { Prints type info for Double type}
  GenericDouble := TGenericClass<Double>.Create;
  GenericDouble.PrintTypeInfo(Memo1);
end;

{
Expected results are:

Type name: string
Type kind: tkUString
Size of type: 4

Type name: Byte
Type kind: tkInteger
Integer kind: otUByte
Size of type: 1

Type name: Double
Type kind: tkFloat
Float kind: ftDouble
Size of type: 8

Type name: .1
Type kind: tkDynArray
Array element type name: string
Array element type size: 4
Size of type: 4
}

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!