在Delphi中实现ListType <T>的列表枚举器</t>

时间:2012-05-18 17:54:17

标签: delphi generics enumerable

我正在使用Delphi XE来实现一个枚举器,它允许按类型过滤列表中的元素。我已经快速组装了一个测试单元如下:

unit uTestList;

interface

uses Generics.Collections;

type
  TListItemBase = class(TObject)
  end; { TListItemBase }

  TListItemChild1 = class(TListItemBase)
  end;

  TListItemChild2 = class(TListItemBase)
  end;

  TTestList<T : TListItemBase> = class;

  TOfTypeEnumerator<T, TFilter> = class(TInterfacedObject, IEnumerator<TFilter>)
  private
    FTestList : TList<T>;
    FIndex : Integer;
  protected
    constructor Create(Owner : TList<T>); overload;

    function GetCurrent : TFilter;
    function MoveNext : Boolean;
    procedure Reset;

    function IEnumerator<TFilter>.GetCurrent = GetCurrent;
    function IEnumerator<TFilter>.MoveNext = MoveNext;
    procedure IEnumerator<TFilter>.Reset = Reset;
  end;

  TOfTypeEnumeratorFactory<T, TFilter> = class(TInterfacedObject, IEnumerable)
  private
    FTestList : TList<T>;
  public
    constructor Create(Owner : TList<T>); overload;
    function GetEnumerator : TOfTypeEnumerator<T, TFilter>;
  end;

  TTestList<T : TListItemBase> = class(TList<T>)
  public
    function OfType<TFilter : TListItemBase>() : IEnumerable;
  end; { TTestList }


implementation

{ TOfTypeEnumerator<T, TFilter> }

constructor TOfTypeEnumerator<T, TFilter>.Create(Owner: TList<T>);
begin
  inherited;
  FTestList := Owner;
  FIndex := -1;
end;

function TOfTypeEnumerator<T, TFilter>.GetCurrent: TFilter;
begin
  Result := TFilter(FTestList[FIndex]);
end;

function TOfTypeEnumerator<T, TFilter>.MoveNext: Boolean;
begin
  Inc(FIndex);
  while ((FIndex < FTestList.Count)
         and (not FTestList[FIndex].InheritsFrom(TFilter))) do
  begin
    Inc(FIndex);
  end; { while }
end;

{ TOfTypeEnumeratorFactory<T, TFilter> }

constructor TOfTypeEnumeratorFactory<T, TFilter>.Create(Owner: TList<T>);
begin
  inherited;
  FTestList := Owner;
end;

function TOfTypeEnumeratorFactory<T, TFilter>.GetEnumerator: TOfTypeEnumerator<T, TFilter>;
begin
  Result := TOfTypeEnumerator<T,TFilter>.Create(FTestList);
end;

{ TTestList<T> }

function TTestList<T>.OfType<TFilter>: IEnumerable;
begin
  Result := TOfTypeEnumeratorFactory<T,TFilter>.Create(self);
end;

end.

使用可怕的F2084内部错误:D7837编译此单元失败。我当然可以在没有枚举器的情况下做到这一点,但我宁愿有一个可用来使代码保持一致。尝试在Spring4D上实现它时,我遇到了类似的编译器问题,但我想在这里发布一个普通的,普通的Delphi问题。

有没有人有实际编译的替代实现?

感谢。

2 个答案:

答案 0 :(得分:32)

您不想使用IEnumerator&lt; T&gt;来自System.pas,相信我。那个东西带来了很多麻烦,因为它继承自IEnumerator,因此具有不同结果的GetCurrent方法(IEnumerator的TObject和IEnumerator的T&lt; T&gt;)。

更好地定义自己的IEnumerator&lt; T&gt;:

IEnumerator<T> = interface
  function GetCurrent: T;
  function MoveNext: Boolean;
  procedure Reset;
  property Current: T read GetCurrent;
end;

与IEnumerable相同。我想说定义你自己的IEnumerable&lt; T&gt;:

IEnumerable<T> = interface
  function GetEnumerator: IEnumerator<T>;
end;

如果您在TOfTypeEnumerator中使用它&lt; T,TFilter&gt;你可以删除导致ICE的方法解决条款。

当你这样做时,你将开始看到其他编译器错误E2008,E2089等等。

  • 在构造函数中继承的调用尝试在祖先类中使用相同的签名调用构造函数,该签名不存在。因此,将其更改为继承的Create。

  • 不要使用IEnumerable但使用IEnumerable&lt; TFilter&gt;因为那是你想要的枚举器

  • 不要使用仅允许对象的方法和强制转换,也不要在T和TFilter上指定类约束

  • MoveNext需要结果

这是编译单元。做了快速测试,似乎有效:

unit uTestList;

interface

uses
  Generics.Collections;

type
  IEnumerator<T> = interface
    function GetCurrent: T;
    function MoveNext: Boolean;
    property Current: T read GetCurrent;
  end;

  IEnumerable<T> = interface
    function GetEnumerator: IEnumerator<T>;
  end;

  TOfTypeEnumerator<T: class; TFilter: class> = class(TInterfacedObject, IEnumerator<TFilter>)
  private
    FTestList: TList<T>;
    FIndex: Integer;
  protected
    constructor Create(Owner: TList<T>); overload;

    function GetCurrent: TFilter;
    function MoveNext: Boolean;
  end;

  TOfTypeEnumeratorFactory<T: class; TFilter: class> = class(TInterfacedObject, IEnumerable<TFilter>)
  private
    FTestList: TList<T>;
  public
    constructor Create(Owner: TList<T>); overload;
    function GetEnumerator: IEnumerator<TFilter>;
  end;

  TTestList<T: class> = class(TList<T>)
  public
    function OfType<TFilter: class>: IEnumerable<TFilter>;
  end;

implementation

{ TOfTypeEnumerator<T, TFilter> }

constructor TOfTypeEnumerator<T, TFilter>.Create(Owner: TList<T>);
begin
  inherited Create;
  FTestList := Owner;
  FIndex := -1;
end;

function TOfTypeEnumerator<T, TFilter>.GetCurrent: TFilter;
begin
  Result := TFilter(TObject(FTestList[FIndex]));
end;

function TOfTypeEnumerator<T, TFilter>.MoveNext: Boolean;
begin
  repeat
    Inc(FIndex);
  until (FIndex >= FTestList.Count) or FTestList[FIndex].InheritsFrom(TFilter);
  Result := FIndex < FTestList.Count;
end;

{ TOfTypeEnumeratorFactory<T, TFilter> }

constructor TOfTypeEnumeratorFactory<T, TFilter>.Create(Owner: TList<T>);
begin
  inherited Create;
  FTestList := Owner;
end;

function TOfTypeEnumeratorFactory<T, TFilter>.GetEnumerator: IEnumerator<TFilter>;
begin
  Result := TOfTypeEnumerator<T, TFilter>.Create(FTestList);
end;

{ TTestList<T> }

function TTestList<T>.OfType<TFilter>: IEnumerable<TFilter>;
begin
  Result := TOfTypeEnumeratorFactory<T,TFilter>.Create(self);
end;

end.

答案 1 :(得分:1)

使用system.IEnumerable<T>system.IEnumerator<T>

的工作版本
unit uTestList;

interface

uses Generics.Collections;

type
  TListItemBase = class(TObject)
  end; { TListItemBase }

  TListItemChild1 = class(TListItemBase)
  end;

  TListItemChild2 = class(TListItemBase)
  end;

  TTestList<T : TListItemBase> = class;

  TOfTypeEnumerator<T : class; TFilter : class> = class(TInterfacedObject, IEnumerator<TFilter>, IEnumerator)
  private
    FTestList : TList<T>;
    FIndex : Integer;
  protected
    constructor Create(Owner : TList<T>); overload;

    function GetCurrent: TObject;
    function GenericGetCurrent : TFilter;
    function MoveNext : Boolean;
    procedure Reset;

    function IEnumerator<TFilter>.GetCurrent = GenericGetCurrent;
  end;

  TOfTypeEnumeratorFactory<T : class; TFilter : class> = class(TInterfacedObject, IEnumerable<TFilter>, IEnumerable)
  private
    FTestList : TList<T>;
  public
    constructor Create(Owner : TList<T>); overload;
    function GetEnumerator : IEnumerator;
    function GenericGetEnumerator : IEnumerator<TFilter>;
    function IEnumerable<TFilter>.GetEnumerator = GenericGetEnumerator;
  end;

  TTestList<T : TListItemBase> = class(TList<T>)
  public
    function OfType<TFilter : TListItemBase>() : IEnumerable<TFilter>;
  end; { TTestList }


implementation

{ TOfTypeEnumerator<T, TFilter> }

constructor TOfTypeEnumerator<T, TFilter>.Create(Owner: TList<T>);
begin
  inherited Create;
  FTestList := Owner;
  FIndex := -1;
end;

function TOfTypeEnumerator<T, TFilter>.GenericGetCurrent: TFilter;
begin
  Result := TFilter(TObject(FTestList[FIndex]));
end;

function TOfTypeEnumerator<T, TFilter>.GetCurrent: TObject;
begin
  Result := TObject( FTestList[FIndex] );
end;

function TOfTypeEnumerator<T, TFilter>.MoveNext: Boolean;
begin
  repeat
    Inc(FIndex);
  until (FIndex >= FTestList.Count) or FTestList[FIndex].InheritsFrom(TFilter);
  Result := FIndex < FTestList.Count;
end;

procedure TOfTypeEnumerator<T, TFilter>.Reset;
begin
  FIndex := -1;
end;

{ TOfTypeEnumeratorFactory<T, TFilter> }

constructor TOfTypeEnumeratorFactory<T, TFilter>.Create(Owner: TList<T>);
begin
  inherited Create;
  FTestList := Owner;
end;

function TOfTypeEnumeratorFactory<T, TFilter>.GetEnumerator: IEnumerator;
begin
  Result := GenericGetEnumerator;
end;

function TOfTypeEnumeratorFactory<T, TFilter>.GenericGetEnumerator: IEnumerator<TFilter>;
begin
  Result := TOfTypeEnumerator<T,TFilter>.Create(FTestList);
end;

{ TTestList<T> }

function TTestList<T>.OfType<TFilter>: IEnumerable<TFilter>;
begin
  Result := TOfTypeEnumeratorFactory<T,TFilter>.Create(self);
end;

end.

测试程序:

var
  MyElem: TListItemBase;
  MyElem1: TListItemChild1;
  MyElem2: TListItemChild2;
begin
  Memo1.Clear;
  for MyElem in FTestList.OfType<TListItemBase>() do
  begin
    Memo1.Lines.Add('----------');
  end;
  for MyElem1 in FTestList.OfType<TListItemChild1>() do
  begin
    Memo1.Lines.Add('==========');
  end;
  for MyElem2 in FTestList.OfType<TListItemChild2>() do
  begin
    Memo1.Lines.Add('++++++++++');
  end;