我试图在TEnumerator中公开构建私有静态数组.
Delphi本身允许直接枚举静态数组(见下文),所以我怀疑Delphi在后台为静态数组创建一个枚举器,我希望我能够在GetEnumerator方法中创建和公开相同的枚举器.
(我使用的是Delphi XE2).
program Project6; {$APPTYPE CONSOLE} {$R *.res} uses System.SysUtils,System.Generics.Collections; type TMyEnum = (meA,meB); TMyClass = class private FItems: array[TMyEnum] of Integer; protected public function GetEnumerator: TEnumerator<Integer>; end; { TMyClass } function TMyClass.GetEnumerator: TEnumerator<Integer>; begin // What is the simplies way of creating this enumerator? end; var myObj: TMyClass; i: Integer; begin myObj := TMyClass.Create; try // This works but only in the same unit for i in myObj.FItems do WriteLn(i); for i in myObj do WriteLn(i); except on E: Exception do Writeln(E.ClassName,': ',E.Message); end; ReadLn; end.
请注意,我可以编写如下的自定义模拟器.但我试图避免这种情况并暴露内置的.
TStaticArrayEnumerator<T> = class(TEnumerator<T>) private FCurrent: Pointer; FElementAfterLast: Pointer; protected function DoGetCurrent: T; override; function DoMoveNext: Boolean; override; public constructor Create(aArray: Pointer; aCount: Integer); end; { TStaticArrayEnumerator<T> } constructor TStaticArrayEnumerator<T>.Create(aArray: Pointer; aCount: Integer); begin // need to point Current before the first element (see comment in DoMoveNext) FCurrent := Pointer(NativeInt(aArray) - SizeOf(T)); FElementSize := aElementSize; FElementAfterLast := Pointer(NativeInt(aArray) + aCount * SizeOf(T)) end; function TStaticArrayEnumerator<T>.DoGetCurrent: T; begin Result := T(FCurrent^); end; function TStaticArrayEnumerator<T>.DoMoveNext: Boolean; begin // This method gets called before DoGetCurrent gets called the first time FCurrent := Pointer(NativeInt(FCurrent) + SizeOf(T)); Result := not (FCurrent = FElementAfterLast); end;
解决方法
Note that I can write a custom emulator like below. But I am trying to avoid this and expose the built in one.
你不能.没有类型表示数组的枚举器.当您在数组的元素上编写for..in循环时,编译器通过内联经典的for循环来处理它.
考虑这个程序:
type TMyEnum = (enum1,enum2,enum3); var arr: array [TMyEnum] of Integer; i: Integer; begin for i in arr do Writeln(i); Readln; end.
Project1.dpr.13: for i in arr do 004060D7 BE9CAB4000 mov esi,$0040ab9c 004060DC 33DB xor ebx,ebx 004060DE 8B3C9E mov edi,[esi+ebx*4] Project1.dpr.14: Writeln(i); 004060E1 A110784000 mov eax,[$00407810] 004060E6 8BD7 mov edx,edi 004060E8 E823DCFFFF call @Write0Long 004060ED E8FEDEFFFF call @WriteLn 004060F2 E869CCFFFF call @_IOTest 004060F7 43 inc ebx Project1.dpr.13: for i in arr do 004060F8 83FB03 cmp ebx,$03 004060FB 75E1 jnz $004060de Project1.dpr.15: Readln; 004060FD A114784000 mov eax,[$00407814] 00406102 E8E5D7FFFF call @ReadLn 00406107 E854CCFFFF call @_IOTest
坦率地说,你能做的最好的就是你已经拥有的.您已经拥有的问题是堆分配.使用记录而不是类来编写枚举器,如下所示:
type TArrayEnumerator<T> = record strict private type P = ^T; strict private FArr: P; FIndex: Integer; FCount: Integer; public class function Initialize(const Arr: array of T): TArrayEnumerator<T>; static; function GetCurrent: T; function MoveNext: Boolean; property Current: T read GetCurrent; end; class function TArrayEnumerator<T>.Initialize(const Arr: array of T): TArrayEnumerator<T>; begin Result.FArr := @Arr[low(Arr)]; Result.FIndex := -1; Result.FCount := Length(Arr); end; function TArrayEnumerator<T>.MoveNext: Boolean; begin Result := FIndex < FCount-1; if Result then inc(FIndex); end; function TArrayEnumerator<T>.GetCurrent: T; var Ptr: P; begin Ptr := FArr; inc(Ptr,FIndex); Result := Ptr^; end;
然后你的GetEnumerator实现如下:
function TMyClass.GetEnumerator: TArrayEnumerator<Integer>; begin Result := TArrayEnumerator<Integer>.Initialize(FItems); end;