Parcourir la source

compiler: allow 'enumerator MoveNext' for the interface function declaration + test

git-svn-id: branches/paul/features@13952 -
paul il y a 15 ans
Parent
commit
11cdaafea4
3 fichiers modifiés avec 70 ajouts et 1 suppressions
  1. 1 0
      .gitattributes
  2. 1 1
      compiler/pdecsub.pas
  3. 68 0
      tests/test/tforin9.pp

+ 1 - 0
.gitattributes

@@ -8217,6 +8217,7 @@ tests/test/tforin5.pp svneol=native#text/pascal
 tests/test/tforin6.pp svneol=native#text/pascal
 tests/test/tforin7.pp svneol=native#text/pascal
 tests/test/tforin8.pp svneol=native#text/pascal
+tests/test/tforin9.pp svneol=native#text/pascal
 tests/test/tfpu1.pp svneol=native#text/plain
 tests/test/tfpu2.pp svneol=native#text/plain
 tests/test/tfpu3.pp svneol=native#text/plain

+ 1 - 1
compiler/pdecsub.pas

@@ -2092,7 +2092,7 @@ const
       mutexclpo     : [po_public,po_exports,po_interrupt,po_assembler,po_inline]
     ),(
       idtok:_ENUMERATOR;
-      pd_flags : [pd_interface,pd_object,pd_notobjintf];
+      pd_flags : [pd_interface,pd_object];
       handler  : @pd_enumerator;
       pocall   : pocall_none;
       pooption : [po_enumerator_movenext];

+ 68 - 0
tests/test/tforin9.pp

@@ -0,0 +1,68 @@
+program tforin9;
+
+// Test interface with 'enumerator MoveNext', 'enumerator Current' directives
+
+{$mode objfpc}{$H+}
+{$APPTYPE CONSOLE}
+
+type
+
+  { IMyIterator }
+
+  IMyIterator = interface
+    function GetValue: TObject;
+    function StepForward: Boolean; enumerator MoveNext;
+    property Value: TObject read GetValue; enumerator Current;
+  end;
+  { TMyListEnumerator }
+
+  TMyListEnumerator = class(TInterfacedObject, IMyIterator)
+  private
+    FValue: Pointer;
+    function GetValue: TObject;
+  public
+    function StepForward: Boolean;
+    property Value: TObject read GetValue;
+  end;
+
+  TMyList = class
+  public
+    function GetIterator: IMyIterator;
+  end;
+
+operator Enumerator(AList: TMyList): IMyIterator;
+begin
+  Result := AList.GetIterator;
+end;
+
+{ TMyListEnumerator }
+
+
+function TMyListEnumerator.GetValue: TObject;
+begin
+  Result := TObject(FValue);
+end;
+
+function TMyListEnumerator.StepForward: Boolean;
+begin
+  inc(PByte(FValue));
+  Result := FValue <= Pointer(3);
+end;
+
+{ TMyList }
+
+function TMyList.GetIterator: IMyIterator;
+begin
+  Result := TMyListEnumerator.Create;
+end;
+
+var
+  List: TMyList;
+  i: TObject;
+begin
+  List := TMyList.Create;
+  for i in List do
+    WriteLn(PtrInt(i));
+  List.Free;
+end.
+