HemulGM
HemulGM

Reputation: 88

Class or record (<T>)

How can I determine whether a Delphi < T > is of a specific class and not for example record? I want to release an element of a list, if it is a class when cleaning.

procedure TTableData<T>.Delete(Index: Integer);
begin
  if Items[Index] is TClass then TClass(Items[Index]).Free;
  inherited Delete(Index);
end;

Upvotes: 1

Views: 300

Answers (1)

David Heffernan
David Heffernan

Reputation: 612954

You can use RTTI, perhaps like this:

uses
  System.TypInfo;

....

procedure TTableData<T>.Delete(Index: Integer);
var
  item: T;
begin
  if PTypeInfo(TypeInfo(T)).Kind = tkClass then
  begin
    item := Items[index];
    TObject((@item)^).Free;
  end;
  inherited Delete(Index);
end;

Upvotes: 2

Related Questions