Marco Web Center

[an error occurred while processing this directive]

Home: Code Repository: Delphi 2009 Handbook

Project: GenericTypeFunc.dproj

Project Structure

GenericTypeFunc.dpr
program GenericTypeFunc;

uses
  Forms,
  GenericTypeFunc_MainForm in 'GenericTypeFunc_MainForm.pas' {Form30};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
  Application.CreateForm(TForm30, Form30);
  Application.Run;
end.
GenericTypeFunc_MainForm.pas
unit GenericTypeFunc_MainForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm30 = class(TForm)
    btnShowInfo: TButton;
    Memo1: TMemo;
    btnExperiments: TButton;
    procedure btnShowInfoClick(Sender: TObject);
    procedure btnExperimentsClick(Sender: TObject);
  private
    { Private declarations }
  public
    procedure Log (const strMsg: string);
  end;

var
  Form30: TForm30;

implementation

uses
  TypInfo;

{$R *.dfm}

type
  TSampleClass <T> = class
  private
    data: T;
  public
    procedure Zero;
    function GetDataSize: Integer;
    function GetDataName: string;
  end;

procedure TForm30.btnShowInfoClick(Sender: TObject);
var
  t1: TSampleClass<Integer>;
  t2: TSampleClass<string>;
  t3: TSampleClass<double>;
begin
  t1 := TSampleClass<Integer>.Create;
  t1.Zero;
  Log ('TSampleClass<Integer>');
  Log ('data: ' + IntToStr (t1.data));
  Log ('type: ' + t1.GetDataName);
  Log ('size: ' + IntToStr (t1.GetDataSize));

  t2 := TSampleClass<string>.Create;
  t2.Zero;
  Log ('TSampleClass<string>');
  Log ('data: ' + t2.data);
  Log ('type: ' + t2.GetDataName);
  Log ('size: ' + IntToStr (t2.GetDataSize));

  t3 := TSampleClass<double>.Create;
  t3.Zero;
  Log ('TSampleClass<double>');
  Log ('data: ' + FloatToStr (t3.data));
  Log ('type: ' + t3.GetDataName);
  Log ('size: ' + IntToStr (t3.GetDataSize));
end;

procedure TForm30.btnExperimentsClick(Sender: TObject);
var
  I: Integer;
  s: string;
begin
  I := Default (Integer);
  Log ('Default Integer: ' + IntToStr (I));

  s := Default (string);
  Log ('Default String: ' + s);

  Log ('TypeInfo String: ' + GetTypeName (TypeInfo (string)));
end;

procedure TForm30.Log(const strMsg: string);
begin
  Memo1.Lines.Add(strMsg);
end;

{ TSampleClass<T> }

function TSampleClass<T>.GetDataSize: Integer;
begin
  Result := SizeOf (T);
end;

function TSampleClass<T>.GetDataName: string;
begin
  Result := GetTypeName (TypeInfo (T));
end;

procedure TSampleClass<T>.Zero;
begin
  data := Default (T);
end;

end.
GenericTypeFunc_MainForm.pas.dfm
object Form30: TForm30
  Left = 0
  Top = 0
  Caption = 'GenericTypeFunc'
  ClientHeight = 315
  ClientWidth = 537
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object btnShowInfo: TButton
    Left = 8
    Top = 8
    Width = 97
    Height = 25
    Caption = 'btnShowInfo'
    TabOrder = 0
    OnClick = btnShowInfoClick
  end
  object Memo1: TMemo
    Left = 111
    Top = 8
    Width = 418
    Height = 299
    TabOrder = 1
  end
  object btnExperiments: TButton
    Left = 8
    Top = 39
    Width = 97
    Height = 25
    Caption = 'btnExperiments'
    TabOrder = 2
    OnClick = btnExperimentsClick
  end
end
HTML file generated by PasToWeb, a tool by Marco Cantù
Copyright 2008 Marco Cantù