na fali entuzjazmu dla nowy ficzerow w Delphi XE2 (moze bylo to juz wczesniej)
postanowilem stworzyc iterator generyczny...
Zalozenie bylo takie ze na jednej liscie mam obiekty roznych klas,
a uzywajac tego iteratora mialbym dostep do wszystkich obiektow wybranej klasy.
TIterator<T> = class
private
fList: TList;
fIndex: integer;
public
constructor Create(aList: TList);
destructor Destroy; override;
function getCurrent: T;
function next: boolean;
function reset: boolean;
end;
implementation
constructor TIterator<T>.Create(aList: TList);
begin
fIndex := -1;
fList := TList.Create;
fList.assign(aList);
end;
destructor TIterator<T>.Destroy;
begin
fList.Free;
inherited;
end;
function TIterator<T>.getCurrent: T;
begin
result := T(fList[fIndex]);
end;
function TIterator<T>.next: boolean;
begin
repeat
inc(fIndex);
until (TObject(fList[fIndex]) is T) or (fIndex >= fList.Count);
result := (TObject(fList[fIndex]) is T) and (fIndex < fList.Count)
end;
function TIterator<T>.reset: boolean;
begin
fIndex := -1;
end;
Jednak moj entuzjazm szybko sie skonczyl, gdyż nie jestem w stanie zrobic nic z tym generycznie podawanym typem T...
zarowno rzutowanie elementow listy na T jak i sprawdzania czy obiekt jest klasy T, powoduje blad kompilacji...
Podejzewam ze wynika to z tego ze kompilator nie wie ze T jest klasa... i dlatego nie pozwala na takie operacje...
Macie pomysl jak to rozwiazac?
//edit: w C++ pisze sie TIterator<class T> zeby zaznaczyc ze T jest klasa... moze jakos podobnie w Delphi trzeba? tylko jak?